| 1 | // |
|---|---|
| 2 | // DOMWriter.cpp |
| 3 | // |
| 4 | // This sample demonstrates the DOMWriter class and how to |
| 5 | // build DOM documents in memory. |
| 6 | // |
| 7 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
| 8 | // and Contributors. |
| 9 | // |
| 10 | // SPDX-License-Identifier: BSL-1.0 |
| 11 | // |
| 12 | |
| 13 | |
| 14 | #include "Poco/DOM/Document.h" |
| 15 | #include "Poco/DOM/Element.h" |
| 16 | #include "Poco/DOM/Text.h" |
| 17 | #include "Poco/DOM/AutoPtr.h" |
| 18 | #include "Poco/DOM/DOMWriter.h" |
| 19 | #include "Poco/XML/XMLWriter.h" |
| 20 | #include <iostream> |
| 21 | |
| 22 | |
| 23 | using Poco::XML::Document; |
| 24 | using Poco::XML::Element; |
| 25 | using Poco::XML::Text; |
| 26 | using Poco::XML::AutoPtr; |
| 27 | using Poco::XML::DOMWriter; |
| 28 | using Poco::XML::XMLWriter; |
| 29 | |
| 30 | |
| 31 | int main(int argc, char** argv) |
| 32 | { |
| 33 | // build a DOM document and write it to standard output. |
| 34 | |
| 35 | AutoPtr<Document> pDoc = new Document; |
| 36 | |
| 37 | AutoPtr<Element> pRoot = pDoc->createElement("root"); |
| 38 | pDoc->appendChild(pRoot); |
| 39 | |
| 40 | AutoPtr<Element> pChild1 = pDoc->createElement("child1"); |
| 41 | AutoPtr<Text> pText1 = pDoc->createTextNode("text1"); |
| 42 | pChild1->appendChild(pText1); |
| 43 | pRoot->appendChild(pChild1); |
| 44 | |
| 45 | AutoPtr<Element> pChild2 = pDoc->createElement("child2"); |
| 46 | AutoPtr<Text> pText2 = pDoc->createTextNode("text2"); |
| 47 | pChild2->appendChild(pText2); |
| 48 | pRoot->appendChild(pChild2); |
| 49 | |
| 50 | DOMWriter writer; |
| 51 | writer.setNewLine("\n"); |
| 52 | writer.setOptions(XMLWriter::PRETTY_PRINT); |
| 53 | writer.writeNode(std::cout, pDoc); |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 |