toXMLString from DOM

A general discussion forum.
Posts: 3
Joined: 13 Jul 2009, 23:52

toXMLString from DOM

Postby Foxhunter » 13 Jul 2009, 23:55

I'm having difficulty figuring out how to generate an XMLString from my Document object. Can someone point me at some sample code for processing DOM objects into strings?

Posts: 1266
Joined: 11 Jul 2006, 16:27
Location: Austria

Re: toXMLString from DOM

Postby guenter » 14 Jul 2009, 06:28

Use Poco::XML::DOMWriter and write to a string stream. As in:

Code: Select all

AutoPtr<Document> pDoc = new Document;

AutoPtr<Element> pRoot = pDoc->createElement("root");

AutoPtr<Element> pChild1 = pDoc->createElement("child1");
AutoPtr<Text> pText1 = pDoc->createTextNode("text1");

AutoPtr<Element> pChild2 = pDoc->createElement("child2");
AutoPtr<Text> pText2 = pDoc->createTextNode("text2");

DOMWriter writer;
std::ostringstream ostr;
writer.writeNode(ostr, pDoc);
std::string xml = ostr.str();

Posts: 3
Joined: 13 Jul 2009, 23:52

Re: toXMLString from DOM

Postby Foxhunter » 14 Jul 2009, 17:58

Thank you. This works and is exactly what I was looking for.

For some reason, I had tried this before and my XML wasn't completely loading into the string (it stopped loading the string after the first whitespace). I believe it is because I used a stringstream. In any case, this now does the job. Thank you again for getting back so quickly.

Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 1 guest