Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

failed to get values of XML elements in pugixml using node.value()

Tags:

c++

xml

pugixml

I have below simple XML template in my C++ source code. Within below code block I need to get values for <scannerID> and <subscannerID>. both elements are children of pugixml document root.

xml_document doc;
xml_parse_result r;
std::string sXml = "<inArgs><scannerID>1</scannerID><subScannerID>2</subScannerID></inArgs>";


r = doc.load_buffer(sXml.c_str(), sXml.length());
if (!r) {
    return false;
}


xml_node root = doc.child("inArgs");

if (!root) {
    return false;
}

std::cout << "root = " << root.name() << std::endl;

xml_node scanner_node = root.child("scannerID");
if (scanner_node) {
    std::cout << "scannerID = " << scanner_node.name() << std::endl;
    std::cout << "scannerID = " << scanner_node.value() << std::endl;
}

xml_node sub_scanner_node = root.child("subscannerID");
if (scanner_node) {
    std::cout << "sub_scanner_node = " << sub_scanner_node.name() << std::endl;
    std::cout << "sub_scanner_node = " << sub_scanner_node.value() << std::endl;
}

this code portion giving an output like below. I can get the node's names correctly but failed to retrieve the values.

Out put: values are empty strings.

root = inArgs
scannerID = scannerID
scannerID = 
subscannerID = subscannerID
subscannerID = 

Edited to add modification for the approach in the answer

node = root.child("scannerID");
if (!node) {
    return false;
}

std::cout << "nodeName = %s" << node.name() << std::endl;
std::cout << "text value: " << node.child_value() << std::endl;

but still the output is the same. I saw something different while reading the documents in

like image 760
Dig The Code Avatar asked Oct 29 '25 14:10

Dig The Code


1 Answers

The data is in the pcdata child of your element_nodes.

Try scanner_node.child_value()

see the Getting node data section for further examples and explanation.

see node_element

see node_pcdata

like image 168
randooom Avatar answered Nov 01 '25 06:11

randooom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!