Results 1 to 9 of 9

Thread: XML File reading issue

  1. #1
    Join Date
    Feb 2009
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default XML File reading issue

    Hi,
    I am trying to read an xml file using DOM APIs, now there is an API in QDomElement elementsByTagName() which takes the element tag and returns the list of QDomNode as QDomNodeList. But I am not getting any Node when I am passing the QDomDocument which is having the mentioned element tag.It is always returning empty NodeList. Is there any kind of bug in this API or my usage is wrong.
    Also if I have to locate an element in an xml tree is there any better way, I found this API to be qute simple to get the QDomElement having the requried element tag so I thought of using it.
    Is there any other way as to how I can retrieve the element which is deep down in the tree??

    Regards,
    Yogesh

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: XML File reading issue

    Quote Originally Posted by yogesh View Post
    Is there any kind of bug in this API
    no, don't think so.
    or my usage is wrong.
    Guess so, but without providing your code, hard to say!

    Is there any other way as to how I can retrieve the element which is deep down in the tree??
    Well, I think elementsByTagName() is your friend... (or you could use XQuery)

  3. #3
    Join Date
    Feb 2009
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: XML File reading issue

    Qt Code:
    1. QFile file("input.xml");
    2. file.open(QIODevice::ReadOnly)
    3. byteArray = file.readAll();
    4. m_Doc.setContent(byteArray);
    5. QDomNode root = m_Doc.documentElement();
    6. QDomNodeList nodeList = root.elementsByTagName(aNodeElement);
    7. if(!nodeList.count())
    8. {
    9. QDomNode node = nodeList.at(0);
    10. if(node.isElement())
    11. {
    12. QDomElement element = node.toElement();
    13. aValue = element.text();
    14. qDebug()<< aValue;
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 
    Can you please tell if there is any problem in the usage??

    Regards,
    Yogesh

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: XML File reading issue

    Hi,

    so far I can't see any error but
    • is the file opened and the content set properly?
    • what's aNodeElement exactly? Right value?

  5. #5
    Join Date
    Feb 2009
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: XML File reading issue

    Hi,
    The file is opened properly and aNodeElement is one of the elements present in the xml file.I checked it in the debugging, still the NodeList is returning empty. Any idea why??

    Regards,
    Yogesh

  6. #6
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: XML File reading issue

    is the code compiling? i see a semi-colon missing on the third line..

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: XML File reading issue

    So, to be sure: aNodeElement is a QString. Uses your XML file namespaces? That may be a problem.

    Lykurg

  8. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: XML File reading issue

    Äh, got it: nodeList.count() is no bool! use
    Qt Code:
    1. if (nodeList.count() > 0)
    To copy to clipboard, switch view to plain text mode 
    and your provided example won't compile because QDomNode has no elementsByTagName! So in future please check yourself your minimal code example to be runable...

    Lykurg

  9. The following user says thank you to Lykurg for this useful post:

    yogesh (3rd March 2009)

  10. #9
    Join Date
    Feb 2009
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: XML File reading issue

    Hi ,
    Thanks a lot for the help. Surely next time I will keep in mind to put the working code actually the implementation was a bit complex with some other logic so I just picked up the requried code and pasted.
    Thanks again.

    Regards,
    Yogesh

Similar Threads

  1. Re: Opening Project file Issue in Edyuk
    By philwinder in forum Qt-based Software
    Replies: 6
    Last Post: 5th May 2008, 20:49
  2. QWT 5, QT3, SuSE 10.2. Crash and burn
    By DrMcCleod in forum Qwt
    Replies: 8
    Last Post: 7th September 2007, 20:53
  3. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  4. Replies: 5
    Last Post: 22nd September 2006, 08:04
  5. Qt 3.3.4 -> Qt 4.1.2, moc file issue
    By philski in forum Qt Tools
    Replies: 1
    Last Post: 11th September 2006, 21:08

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.