Overview
Features
Download
Documentation
Community
Add-Ons & Services

toXMLString from DOM

A general discussion forum.

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?
Foxhunter
 
Posts: 3
Joined: 13 Jul 2009, 23:52

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");
pDoc->appendChild(pRoot);

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

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

DOMWriter writer;
writer.setNewLine("\n");
writer.setOptions(XMLWriter::PRETTY_PRINT);
std::ostringstream ostr;
writer.writeNode(ostr, pDoc);
std::string xml = ostr.str();
guenter
 
Posts: 1112
Joined: 11 Jul 2006, 16:27
Location: Austria

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.
Foxhunter
 
Posts: 3
Joined: 13 Jul 2009, 23:52


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest

cron