Commit f5e52fe2 authored by Norby, Tom's avatar Norby, Tom
Browse files

Add XML parse / write examples and test.

parent da8ea6e7
Pipeline #117336 failed with stages
in 8 minutes and 22 seconds
......@@ -3,6 +3,16 @@ TRIBITS_ADD_EXECUTABLE(pocoapp
SOURCES pocoapp.cpp
)
TRIBITS_ADD_EXECUTABLE(pocodomparser
NOEXEPREFIX
SOURCES pocodomparser.cpp
)
TRIBITS_ADD_EXECUTABLE(pocodomwriter
NOEXEPREFIX
SOURCES pocodomwriter.cpp
)
TRIBITS_ADD_EXECUTABLE(pocotcpclient
NOEXEPREFIX
SOURCES pocotcpclient.cpp
......@@ -14,5 +24,7 @@ TRIBITS_ADD_EXECUTABLE(pocotcpserver
)
TARGET_LINK_LIBRARIES(pocoapp Threads::Threads)
TARGET_LINK_LIBRARIES(pocodomparser Threads::Threads)
TARGET_LINK_LIBRARIES(pocodomwriter Threads::Threads)
TARGET_LINK_LIBRARIES(pocotcpclient Threads::Threads)
TARGET_LINK_LIBRARIES(pocotcpserver Threads::Threads)
//
// DOMParser.cpp
//
// This sample demonstrates the DOMParser, AutoPtr and
// NodeIterator classes.
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include <iostream>
#include "Poco/DOM/AutoPtr.h"
#include "Poco/DOM/DOMParser.h"
#include "Poco/DOM/Document.h"
#include "Poco/DOM/NodeFilter.h"
#include "Poco/DOM/NodeIterator.h"
#include "Poco/Exception.h"
#include "Poco/SAX/InputSource.h"
using Poco::Exception;
using Poco::XML::AutoPtr;
using Poco::XML::Document;
using Poco::XML::DOMParser;
using Poco::XML::InputSource;
using Poco::XML::Node;
using Poco::XML::NodeFilter;
using Poco::XML::NodeIterator;
int main(int argc, char** argv)
{
// Parse an XML document from standard input
// and use a NodeIterator to print out all nodes.
InputSource src(std::cin);
try
{
DOMParser parser;
AutoPtr<Document> pDoc = parser.parse(&src);
NodeIterator it(pDoc, NodeFilter::SHOW_ALL);
Node* pNode = it.nextNode();
while (pNode)
{
std::cout << pNode->nodeName() << ":" << pNode->nodeValue() << std::endl;
pNode = it.nextNode();
}
}
catch (Exception& exc)
{
std::cerr << exc.displayText() << std::endl;
}
return 0;
}
//
// DOMWriter.cpp
//
// This sample demonstrates the DOMWriter class and how to
// build DOM documents in memory.
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include <iostream>
#include "Poco/DOM/AutoPtr.h"
#include "Poco/DOM/DOMWriter.h"
#include "Poco/DOM/Document.h"
#include "Poco/DOM/Element.h"
#include "Poco/DOM/Text.h"
#include "Poco/XML/XMLWriter.h"
using Poco::XML::AutoPtr;
using Poco::XML::Document;
using Poco::XML::DOMWriter;
using Poco::XML::Element;
using Poco::XML::Text;
using Poco::XML::XMLWriter;
int main(int argc, char** argv)
{
// build a DOM document and write it to standard output.
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);
writer.writeNode(std::cout, pDoc);
return 0;
}
INCLUDE(GoogleTest)
ADD_GOOGLE_TEST(DocumentTest.cpp NP 1)
ADD_GOOGLE_TEST(TCPServerTest.cpp NP 1)
//
// DocumentTest.cpp
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "gtest/gtest.h"
#include "Poco/DOM/AutoPtr.h"
#include "Poco/DOM/DOMException.h"
#include "Poco/DOM/Document.h"
#include "Poco/DOM/Element.h"
#include "Poco/DOM/NodeList.h"
#include "Poco/DOM/Text.h"
using Poco::XML::AutoPtr;
using Poco::XML::Document;
using Poco::XML::DOMException;
using Poco::XML::Element;
using Poco::XML::Node;
using Poco::XML::NodeList;
using Poco::XML::Text;
using Poco::XML::XMLString;
TEST(DocumentTest, testDocumentElement)
{
AutoPtr<Document> pDoc = new Document;
AutoPtr<Element> pRoot = pDoc->createElement("root");
ASSERT_TRUE(pDoc->documentElement() == 0);
pDoc->appendChild(pRoot);
ASSERT_TRUE(pDoc->documentElement() == pRoot);
AutoPtr<Text> pText = pDoc->createTextNode(" ");
pDoc->insertBefore(pText, pRoot);
ASSERT_TRUE(pDoc->documentElement() == pRoot);
}
TEST(DocumentTest, testImport)
{
AutoPtr<Document> pDoc1 = new Document;
AutoPtr<Element> pRoot1 = pDoc1->createElement("root");
AutoPtr<Document> pDoc2 = new Document;
try
{
pDoc2->appendChild(pRoot1);
FAIL() << "wrong document - must throw exception";
}
catch (DOMException&)
{
}
AutoPtr<Element> pRoot2 =
static_cast<Element*>(pDoc2->importNode(pRoot1, false));
ASSERT_TRUE(pRoot2->ownerDocument() == pDoc2);
ASSERT_TRUE(pRoot1->ownerDocument() == pDoc1);
pDoc2->appendChild(pRoot2);
}
TEST(DocumentTest, testImportDeep)
{
AutoPtr<Document> pDoc1 = new Document;
AutoPtr<Element> pRoot1 = pDoc1->createElement("root");
AutoPtr<Element> pElem1 = pDoc1->createElement("elem");
AutoPtr<Text> pText1 = pDoc1->createTextNode("text");
pElem1->appendChild(pText1);
pRoot1->appendChild(pElem1);
pRoot1->setAttribute("a1", "v1");
pRoot1->setAttribute("a2", "v2");
AutoPtr<Document> pDoc2 = new Document;
try
{
pDoc2->appendChild(pRoot1);
FAIL() << "wrong document - must throw exception";
}
catch (DOMException&)
{
}
AutoPtr<Element> pRoot2 =
static_cast<Element*>(pDoc2->importNode(pRoot1, true));
ASSERT_TRUE(pRoot2->ownerDocument() == pDoc2);
ASSERT_TRUE(pRoot2->firstChild()->ownerDocument() == pDoc2);
ASSERT_TRUE(pRoot2->firstChild()->firstChild()->ownerDocument() == pDoc2);
ASSERT_TRUE(pRoot1->ownerDocument() == pDoc1);
ASSERT_TRUE(pRoot1->firstChild()->ownerDocument() == pDoc1);
ASSERT_TRUE(pRoot1->firstChild()->firstChild()->ownerDocument() == pDoc1);
pDoc2->appendChild(pRoot2);
}
TEST(DocumentTest, testElementsByTagName)
{
AutoPtr<Document> pDoc = new Document;
AutoPtr<Element> pRoot = pDoc->createElement("root");
pDoc->appendChild(pRoot);
AutoPtr<NodeList> pNL1 = pDoc->getElementsByTagName("*");
AutoPtr<NodeList> pNL2 = pDoc->getElementsByTagName("elem");
ASSERT_TRUE(pNL1->length() == 1);
ASSERT_TRUE(pNL1->item(0) == pRoot);
ASSERT_TRUE(pNL2->length() == 0);
AutoPtr<Element> pElem1 = pDoc->createElement("elem");
pRoot->appendChild(pElem1);
ASSERT_TRUE(pNL1->length() == 2);
ASSERT_TRUE(pNL2->length() == 1);
ASSERT_TRUE(pNL1->item(0) == pRoot);
ASSERT_TRUE(pNL1->item(1) == pElem1);
ASSERT_TRUE(pNL2->item(0) == pElem1);
AutoPtr<Element> pElem2 = pDoc->createElement("Elem");
pRoot->appendChild(pElem2);
ASSERT_TRUE(pNL1->length() == 3);
ASSERT_TRUE(pNL2->length() == 1);
ASSERT_TRUE(pNL1->item(0) == pRoot);
ASSERT_TRUE(pNL1->item(1) == pElem1);
ASSERT_TRUE(pNL1->item(2) == pElem2);
ASSERT_TRUE(pNL2->item(0) == pElem1);
}
TEST(DocumentTest, testElementsByTagNameNS)
{
AutoPtr<Document> pDoc = new Document;
AutoPtr<Element> pRoot = pDoc->createElementNS("urn:ns1", "root");
pDoc->appendChild(pRoot);
AutoPtr<NodeList> pNL1 = pDoc->getElementsByTagNameNS("*", "*");
AutoPtr<NodeList> pNL2 = pDoc->getElementsByTagNameNS("*", "elem");
ASSERT_TRUE(pNL1->length() == 1);
ASSERT_TRUE(pNL1->item(0) == pRoot);
ASSERT_TRUE(pNL2->length() == 0);
AutoPtr<Element> pElem1 = pDoc->createElementNS("urn:ns1", "elem");
pRoot->appendChild(pElem1);
ASSERT_TRUE(pNL1->length() == 2);
ASSERT_TRUE(pNL2->length() == 1);
ASSERT_TRUE(pNL1->item(0) == pRoot);
ASSERT_TRUE(pNL1->item(1) == pElem1);
ASSERT_TRUE(pNL2->item(0) == pElem1);
AutoPtr<Element> pElem2 = pDoc->createElementNS("urn:ns1", "Elem");
pRoot->appendChild(pElem2);
ASSERT_TRUE(pNL1->length() == 3);
ASSERT_TRUE(pNL2->length() == 1);
ASSERT_TRUE(pNL1->item(0) == pRoot);
ASSERT_TRUE(pNL1->item(1) == pElem1);
ASSERT_TRUE(pNL1->item(2) == pElem2);
ASSERT_TRUE(pNL2->item(0) == pElem1);
}
TEST(DocumentTest, testElementById)
{
AutoPtr<Document> pDoc = new Document;
AutoPtr<Element> pRoot = pDoc->createElement("root");
pRoot->setAttribute("id", "0");
AutoPtr<Element> pElem1 = pDoc->createElement("elem");
pElem1->setAttribute("id", "1");
AutoPtr<Text> pText1 = pDoc->createTextNode("text");
AutoPtr<Element> pElem2 = pDoc->createElement("elem");
pElem2->setAttribute("id", "2");
AutoPtr<Element> pElem3 = pDoc->createElement("elem");
pElem3->setAttribute("id", "3");
pElem1->appendChild(pText1);
pElem1->appendChild(pElem2);
pRoot->appendChild(pElem1);
pRoot->appendChild(pElem3);
pDoc->appendChild(pRoot);
Element* pFound = pDoc->getElementById("0", "id");
ASSERT_TRUE(pFound == pRoot);
pFound = pDoc->getElementById("1", "id");
ASSERT_TRUE(pFound == pElem1);
pFound = pDoc->getElementById("2", "id");
ASSERT_TRUE(pFound == pElem2);
pFound = pDoc->getElementById("3", "id");
ASSERT_TRUE(pFound == pElem3);
pFound = pDoc->getElementById("4", "id");
ASSERT_TRUE(pFound == 0);
pFound = pDoc->getElementById("0", "ID");
ASSERT_TRUE(pFound == 0);
}
TEST(DocumentTest, testElementByIdNS)
{
AutoPtr<Document> pDoc = new Document;
AutoPtr<Element> pRoot = pDoc->createElementNS("urn:ns1", "root");
pRoot->setAttributeNS("urn:ns1", "id", "0");
AutoPtr<Element> pElem1 = pDoc->createElementNS("urn:ns1", "elem");
pElem1->setAttributeNS("urn:ns1", "id", "1");
AutoPtr<Text> pText1 = pDoc->createTextNode("text");
AutoPtr<Element> pElem2 = pDoc->createElementNS("urn:ns1", "elem");
pElem2->setAttributeNS("urn:ns1", "id", "2");
AutoPtr<Element> pElem3 = pDoc->createElementNS("urn:ns1", "elem");
pElem3->setAttributeNS("urn:ns1", "id", "3");
pElem1->appendChild(pText1);
pElem1->appendChild(pElem2);
pRoot->appendChild(pElem1);
pRoot->appendChild(pElem3);
pDoc->appendChild(pRoot);
Element* pFound = pDoc->getElementByIdNS("0", "urn:ns1", "id");
ASSERT_TRUE(pFound == pRoot);
pFound = pDoc->getElementByIdNS("1", "urn:ns1", "id");
ASSERT_TRUE(pFound == pElem1);
pFound = pDoc->getElementByIdNS("2", "urn:ns1", "id");
ASSERT_TRUE(pFound == pElem2);
pFound = pDoc->getElementByIdNS("3", "urn:ns1", "id");
ASSERT_TRUE(pFound == pElem3);
pFound = pDoc->getElementByIdNS("4", "urn:ns1", "id");
ASSERT_TRUE(pFound == 0);
pFound = pDoc->getElementByIdNS("0", "urn:ns1", "ID");
ASSERT_TRUE(pFound == 0);
pFound = pDoc->getElementByIdNS("0", "urn:ns2", "id");
ASSERT_TRUE(pFound == 0);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment