[Interest] XML parsing

Christoph Feck cfeck at kde.org
Mon Jul 17 21:50:57 CEST 2017


On 17.07.2017 21:25, Francisco Ares wrote:
> Hi, all.
>
> I'm pretty new to programming, in special regarding XML generation and
> parsing.
>
> Could anyone help me parse the following file?
>
>
> <?xml version="1.0" ?>
> <Settings Firmware="00.01.52.11715 " Name="Mako" Model="Mako G-125C (5488)">
> <Integer Name="AcquisitionFrameCount">1</Integer>
> <Float Name="AcquisitionFrameRateAbs">3.0</Float>
> <Enumeration Name="AcquisitionMode">SingleFrame</Enumeration>
> </Settings>
>
> Meaning 3 settings in a device, one on each line that shows first the type
> of the setting, the name to which this device's API would respond to, then
> the new value for that setting.
>
> For now, I have the following, mostly from samples:
>
> QDomDocument doc("mydocument");
> QFile file("myfile.xml");
> if (!file.open(QIODevice::ReadOnly))
> return;
> if (!doc.setContent(&file)) {
> file.close();
> return;
> }
> file.close();
>
> QDomElement docElem = doc.documentElement();
>
> QDomNode n = docElem.firstChild();
> while(!n.isNull()) {
> QDomElement e = n.toElement(); // try to convert the node to an element.
> if( ( !e.isNull() ) && n.isElement() ) {
> QString aType = e.nodeName();
> QString aValue = e.text();
> }
> n = n.nextSibling();
> }
>
>
> This returns me the the type and the value, but I could not find a way
> to obtain the name of the setting (for instance, "AcquisitionMode" on
> line 3.
>
> Any help will be highly appretiated!
>
> Thanks,
> Francisco

Use QDomElement::attribute() or QDomElement::attributeNode().



More information about the Interest mailing list