Global Installation
The POCO C++ Libraries are powerful cross-platform libraries designed for building network and internet-based applications that run on desktop, server, mobile, IoT, and embedded systems.
By default, the installation location is /usr/local/ on Linux and macOS, and C:\Program Files (x64)\ on Windows. You can override this by setting the CMAKE_INSTALL_PREFIX CMake variable.
$ git clone -b master https://github.com/pocoproject/poco.git
$ cd poco
$ mkdir cmake-build
$ cd cmake-build
$ cmake ..
$ cmake --build . --config Release
sudo cmake --build . --target install // Installation
sudo cmake --build . --target uninstall // Uninstallation
CMakeLists.txt Configuration Example
cmake_minimum_required(VERSION 3.22)
project(xml_processor)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Configure POCO paths and libraries
set(POCO_BASE "/usr/local") # Directory containing "include" and "lib"
set(POCO_INCLUDE_PATH "${POCO_BASE}/include")
set(POCO_LIBRARY_PATH "${POCO_BASE}/lib")
set(POCO_LIBRARIES
"${POCO_LIBRARY_PATH}/libPocoNet.dylib"
"${POCO_LIBRARY_PATH}/libPocoUtil.dylib"
"${POCO_LIBRARY_PATH}/libPocoFoundation.dylib"
"${POCO_LIBRARY_PATH}/libPocoXML.dylib")
link_directories(${POCO_LIBRARY_PATH}) # Add non-standard shared library search path
add_executable(${PROJECT_NAME}
main.cpp
)
include_directories(${POCO_INCLUDE_PATH})
target_link_libraries(${PROJECT_NAME} ${POCO_LIBRARIES}) # Link target files with libraries
XML Processing Examples
#include <string>
#include <streambuf>
#include <sstream>
#include <iostream>
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
void parse_xml_document() {
Poco::XML::DOMParser parser;
Poco::AutoPtr document = parser.parse("./data.xml");
Poco::XML::NodeIterator iterator(document, Poco::XML::NodeFilter::SHOW_ALL);
Poco::XML::Node* current_node = iterator.nextNode();
int node_count = 0;
while (current_node) {
if (current_node->nodeType() != Poco::XML::Node::ELEMENT_NODE) {
current_node = iterator.nextNode();
continue;
}
if (current_node->nodeName() == "#text") {
current_node = iterator.nextNode();
continue;
}
if (current_node->nodeName() == "#cdata-section") {
current_node = iterator.nextNode();
continue;
}
std::cout << "Node:" << node_count << ":" << current_node->nodeName()
<< ":" << current_node->nodeValue() << std::endl;
Poco::XML::NamedNodeMap* attributes = current_node->attributes();
if (attributes) {
for (size_t i = 0; i < attributes->length(); ++i) {
Poco::XML::Node* attribute = attributes->item(i);
std::string attr_name = attribute->nodeName();
std::string attr_value = attribute->nodeValue();
std::cout << "Attribute:" << attr_name << ":" << attr_value << std::endl;
}
}
current_node = iterator.nextNode();
}
}
void create_xml_document() {
Poco::AutoPtr document = new Poco::XML::Document;
// Create XML declaration
Poco::AutoPtr declaration =
document->createProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
// Add comment
Poco::AutoPtr comment = document->createComment("Generated document for demonstration");
// Create root element
Poco::AutoPtr root_element = document->createElement("root");
// Create child elements
Poco::AutoPtr first_child = document->createElement("item");
first_child->setAttribute("id", "001");
first_child->setAttribute("category", "electronics");
Poco::AutoPtr second_child = document->createElement("item");
second_child->setAttribute("id", "002");
second_child->setAttribute("category", "books");
// Create text and CDATA sections
Poco::AutoPtr text_content = document->createTextNode("Regular text content");
Poco::AutoPtr cdata_content =
document->createCDATASection("<special> & characters that shouldn't be parsed</special>");
// Build document structure
document->appendChild(declaration);
document->appendChild(comment);
document->appendChild(root_element);
root_element->appendChild(first_child);
root_element->appendChild(second_child);
root_element->appendChild(text_content);
root_element->appendChild(cdata_content);
// Write to file
Poco::XML::DOMWriter writer;
writer.setOptions(Poco::XML::XMLWriter::PRETTY_PRINT);
writer.writeNode("./output.xml", document);
// Write to string for testing
std::stringstream xml_stream;
writer.writeNode(xml_stream, document);
std::string xml_output = xml_stream.str();
std::cout << xml_output << std::endl;
}
int main(int argc, char *argv[]) {
create_xml_document();
//parse_xml_document();
return 0;
}