#include#include using namespace Poco::XML; // Create an XmlDocument instance XmlDocument xmlDoc; // Create a root node Element* pRoot = xmlDoc.createElement("root"); // Add the root node to the document xmlDoc.appendChild(pRoot); // Create a child node Element* pChild = xmlDoc.createElement("node"); // Add an attribute to the child node pChild->setAttribute("attribute", "value"); // Add the child node to the root node pRoot->appendChild(pChild);
#includeIn this example, we first create a sample XML file as a string. We then parse the XML file into an XmlDocument and get the root node. We then traverse the child nodes and output their values. We output the node name, child node name, and any associated text.#include #include using namespace Poco::XML; int main() { // Create a sample XML file const std::string xml = " \ \ "; // Parse the XML file into an XmlDocument XmlDocument xmlDoc; xmlDoc.parseString(xml); // Get the root node Element* pRoot = xmlDoc.documentElement(); // Traverse the child nodes and output their values for (auto pNode = pRoot->firstChild(); pNode; pNode = pNode->nextSibling()) { if (pNode->nodeType() == Node::ELEMENT_NODE) { Element* pElement = dynamic_cast\ \\ \\ Text \ \(pNode); std::cout << "Node name: " << pElement->nodeName() << std::endl; for (auto pChild = pElement->firstChild(); pChild; pChild = pChild->nextSibling()) { if (pChild->nodeType() == Node::ELEMENT_NODE) { Element* pChildElement = dynamic_cast (pChild); std::cout << "Child node name: " << pChildElement->nodeName() << std::endl; if (auto pText = pChildElement->firstChild()) { std::cout << "Text: " << pText->nodeValue() << std::endl; } } } } } return 0; }