Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Access XML nodes



  • Hi all,

    I have an XML of following structure:

    <?xml version="1.0"?>
    <subject_details>
     <Name>Name2</Name>
     <Surname>Surname2</Surname>
     <Patient_ID>PID02</Patient_ID>
     <Date>01/01/2000 00:00</Date>
     <Clinician_Note>note </Clinician_Note>
     <Settings>
      <Current>12</Current>
      <PW>120</PW>
      <Freq>30</Freq>
     </Settings>
    </subject_details>
    

    I'm using QDomDocument to read.
    I can get individual node value by tagname, for ex:

    root.elementsByTagName("Current").at(0).firstChild().nodeValue();
    

    This gives me the Current node value 12

    What's the method to get all node values under Settings node?

    Thank you


  • Lifetime Qt Champion

    Hi
    Often by simply enumerating the elements

    QDomNode n = docElem.firstChild();
    while(!n.isNull()) {
        QDomElement e = n.toElement(); // try to convert the node to an element.
        if(!e.isNull()) {
            cout << qPrintable(e.tagName()) << endl; // the node really is an element.
        }
        n = n.nextSibling();
    }
    

    https://doc.qt.io/qt-5/qdomdocument.html



  • @russjohn834
    As @mrjj has said, QDomDocument basically wants you to walk the tree as necessary to visit things.

    There is https://doc.qt.io/qt-5/qdomnode.html#childNodes to give you the list of children in one go instead of calling firstChild()/nextSibling() etc. But you'll still have to iterate through them if you want to see what's inside. You won't find a single "get all node values under node".



  • Thank you @mrjj , @JonB


Log in to reply