Poco library for c++, declare namespace for custom element

Posted by Mikhail on Stack Overflow See other posts from Stack Overflow or by Mikhail
Published on 2012-10-05T15:36:16Z Indexed on 2012/10/05 15:37 UTC
Read the original article Hit count: 268

Filed under:
|
|

I want to create an XML document by building a DOM document from scratch, with syntax like:

AutoPtr<Document> doc = new Document;
AutoPtr<Element> root = doc->createElement("root");
doc->appendChild(root);
AutoPtr<Element> element1 = doc->createElementNS("http://ns1", "ns1:element1");
root->appendChild(element1);
AutoPtr<Element> element2 = doc->createElementNS("http://ns1", "ns1:element2");
root->appendChild(element2);

DOMWriter writer;
writer.setNewLine("\n");
writer.setOptions(XMLWriter::PRETTY_PRINT);
writer.writeNode(std::cout, doc);

But, when I write it, I get next result:

<root>
   <ns1:element1 xmlns:ns1="http://ns1"/>
   <ns1:element2 xmlns:ns1="http://ns1"/>
</root>

So namespace ns1 declared two times, and I want to declare it inside "root" element. Is there way to get next representation:

<root xmlns:ns1="http://ns1"/>
   <ns1:element1/>
   <ns1:element2/>
</root> 

© Stack Overflow or respective owner

Related posts about c++

Related posts about POCO