6

I try to parse a XML-file with the following structure:

<I>
  <C c="test1">
     <H><Pd pd="123"/>
        <f p="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 e.g. 0 and 1 and a search pattern e.g. '23' Now i want to search the XML-file for all T-nodes with t="a number from my list" where one of the child nodes(T1, T2,T3) contain the search pattern.

Can anybody help me getting started with this problem? I want to use the Qt functions but do not really know how to begin.

I'm happy about every hint!

5
  • Try building a tree at first recursively. Then search the tree for query. It would be better if you could provide exact structure of XML file. Commented Oct 16, 2012 at 11:51
  • First make sure that your XML follows a specific schema, then take a look at XQuery. Commented Oct 16, 2012 at 11:55
  • @Md. Taufique Hussaiun The XML-structure is like i pasted it except there are much more <T> and <C> nodes. Besides from that that's the exact structure. Commented Oct 16, 2012 at 11:56
  • @Component10 The XML follows a specific shema(I->T->T1). and thank you I'll see what XQuery can do for me :) Commented Oct 16, 2012 at 11:59
  • Take a look on QXmlStreamReader - doc.qt.digia.com/qt/qxmlstreamreader.html Commented Oct 16, 2012 at 13:39

3 Answers 3

6

Untested, but this is a way I already used Qt to scan in a very simply XML file. Maybe this can give you a hint 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();
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Just to be safe, you should call node.toElement() and store that into a QDomElement variable and see if it is null. In the cases where there could be comments in the XML, you will get an error. You can also call docElem.firstChildElement() and skip all of the non-element nodes.
After one year I am wiser and I also would suggest using QXmlStreamReader now, because it is simplier and will be the future in Qt ;-). However - the above code still works in current versions...
2

For XML things, it was suggested to use QXmlStreamReader and QXmlStreamWriter from QtCore module, just because the QDom and QSax things have been not actively maintained for a while.

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

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

I will not copy&paste the example code from qt docs to here. Hope you could understand them well. And you also could check examples/xml directory in qt 4.x.

1 Comment

I just implemented it with qdom, but I will have a look at the qxmlstreamreader. thanks for the tip!
1

you could use QXmlQuery. It act like XQuery (i guess the syntax is the same). And you could parse your xml file with the big advantage of XQuery's flexibility. You can start with a 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)");

setQuery method allow you to define your search pattern. It can be based on element id, attribute, and so on...as with XQuery. This is QXmlQuery doc page: link

1 Comment

i implemented it with qdom, but i will read my way through qxmlreader. thank you for your input!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.