Store PLCnext CommunityPLCnext on LinkedInPLCnext on Instagram  PLCnext on YouTube Github PLCnext CommunityStore PLCnext Community


 How to create a Blog Entry

pugixml a light-weight XML processing library with XPATH support


In this article I will explain how to use pugixml to process XML data. With this processing library you can use a DOM parser to read and manipulate nodes and the XPATH notation to query nodes.

How to build

To build the sources, you have to download a release from Now you can crosscompile the source with the PLCnext toolchain given by Phoenix Contact. The following code-snippet will show the cmake script to crosscompile.

    #Please set the environment variables to your needs 

    cmake \
    -G "Unix Malkefiles" \
    -D CMAKE_BUILD_TYPE=Release \

How to use

First you have to integrate the C++ header and the library in your project environment. When you’re done you can use the pugixml processing.

Loading a document

    pugi::xml_document doc;
    pugi::xml_parse_result result = doc.load_file("data.xml");


Traversing subnodes from a given node

You can traverse via DOM tree and you can also manipulate the nodes.

    pugi::xpath_node_set tools = doc.select_nodes("/Profile/Tools/Tool");

    //traversing subnodes
    for (pugi::xml_node tool: tools)
        std::cout << " " <<;

        //getting attributes
        for (pugi::xml_attribute attr: tool.attributes())
            std::cout << " " << << "=" << attr.value();


XPATH gives you the freedom to access nodes and attributes very easy.

    //accessing node
    pugi::xpath_node_set tools = doc.select_nodes("/Profile/Tools/Tool");
    pugi::xpath_node_set all_tools = doc.select_nodes("//Tool");

    //accessing attibutes
    auto docIdNode = doc.select_node("@ID");
    auto docIdNode_One = doc.select_node("//Tool[@ID==1]");

Saving XML document

You can save the XML document.


More Information

If you are interested in getting more information about pugixml you can check the folowing links:


The library is published under MIT License