Parsing an XML file using C ++ & Qt

I am trying to parse an XML file with the following structure:

<I> <C c="test1"> <H><Pd pd="123"/> <fp="789" r="456"/> </H> <M m="test2"> <H><Pd pd="3456"/><R r="678"/> </H> </M> </C> <T t="0"> <T2>123</T2> <T3>2345</T3> </T> <T t="1"> <T1>23456</T1> <T2>23</T2> <T3>123</T3> <T4>456</T4> </T> </I> 

I have a list of numbers, for example. 0 and 1 and a search pattern, for example. '23' Now I want to search for an XML file for all T-nodes with t = "number from my list", where one of the child nodes (T1, T2, T3) contains a search pattern.

Can someone help me get started with this problem? I want to use the Qt functions, but I donโ€™t know how to start.

I am glad to every hint!

+6
source share
3 answers

Unconfirmed, but this is the way I have already used Qt to scan in a very simple XML file. Perhaps this may give you a hint on how to use it here:

 QDomElement docElem; QDomDocument xmldoc; xmldoc.setContent(YOUR_XML_DATA); docElem=xmldoc.documentElement(); if (docElem.nodeName().compare("T")==0) { QDomNode node=docElem.firstChild(); while (!node.isNull()) { quint32 number = node.toElement().attribute("t").toUInt(); //or whatever you want to find here.. //do something node = node.nextSibling(); } } 
+6
source

For XML things, it was suggested to use QXmlStreamReader and QXmlStreamWriter from the QtCore module only because QDom and QSax things were inactive for some time.

http://doc.qt.digia.com/4.7/qxmlstreamreader.html

http://doc.qt.digia.com/4.7/qxmlstreamwriter.html

I will not copy and paste the sample code from qt docs here. I hope you understand them well. And you can also check the examples / xml directory in qt 4.x.

+2
source

you can use QXmlQuery. It acts like XQuery (I think the syntax is the same). And you can parse your XML file with the big advantage of XQuery flexibility. You can start with code like this:

 QByteArray myDocument; QBuffer buffer(&myDocument); // This is a QIODevice. buffer.open(QIODevice::ReadOnly); QXmlQuery query; query.bindVariable("myDocument", &buffer); query.setQuery("doc($myDocument)"); 

The setQuery method allows you to define your search pattern. It can be based on an element identifier, attribute, etc ... like in XQuery. This is the QXmlQuery document page: link

+1
source

Source: https://habr.com/ru/post/927834/


All Articles