Results 1 to 2 of 2

Thread: Xml namespace parsing using QXmlStreamReader

  1. #1
    Join Date
    Aug 2011
    Posts
    38
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Xml namespace parsing using QXmlStreamReader

    Hi how namespace parsing should handle when QXmlStreamReader is used ? Or should i use different approach?

    m_xml.setNamespaceProcessing (true);
    m_xml.readNext();

    if (m_xml.isStartElement()) {
    if (m_xml.name() == "item") {
    qDebug() << m_xml.attributes().value("atom:link").toString();
    parseItemElement();
    } else if (m_xml.name() == "image") {
    parseImageElement ();
    } else {
    qDebug() << m_xml.name() << m_xml.text() << m_xml.isEntityReference();
    m_xml.readNext();
    }
    } else


    Namespace attributes like atom:link doesnt get catched, atom:link, only link will fetch.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Xml namespace parsing using QXmlStreamReader

    Your should use name() and namespaceUri() together to determine if this is the element you want. Using the URI makes your code independent of the arbitrary choice of namespace prefix. made by the XML originator.

    Qt Code:
    1. #include <QtCore>
    2. #include <QXmlStreamReader>
    3.  
    4. static const QString data(
    5. "<?xml version='1.0' ?>"
    6. "<a:doc xmlns:a='urn:first' xmlns:b='urn:second' >"
    7. "<a:thingy>Test A</a:thingy>"
    8. "<b:thingy>Test B</b:thingy>"
    9. "<c xmlns='urn:third'><d>Text D</d><d>Text D</d></c>"
    10. "</a:doc>"
    11. );
    12.  
    13. int main(int argc, char **argv)
    14. {
    15. QCoreApplication app(argc, argv);
    16.  
    17. QXmlStreamReader xml(data);
    18. xml.setNamespaceProcessing(true);
    19.  
    20. while (!xml.atEnd()) {
    21. if(xml.readNextStartElement()) {
    22. if (xml.namespaceUri() == "urn:first") {
    23. qDebug() << "Process element" << xml.name() << "from first namespace";
    24. }
    25. else if (xml.namespaceUri() == "urn:second") {
    26. qDebug() << "Process element" << xml.name() << "from second namespace";
    27. }
    28. else if (xml.namespaceUri() == "urn:third") {
    29. qDebug() << "Process element" << xml.name() << "from third namespace";
    30. }
    31. else { // no namespace
    32. qDebug() << "Process element from ? namespace";
    33. }
    34. }
    35. }
    36.  
    37. return 0;
    38. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QXmlStreamReader help?
    By nthung in forum Newbie
    Replies: 2
    Last Post: 4th October 2011, 09:03
  2. Parsing RSS 2.0 using QXmlStreamReader
    By matsukan in forum Qt Programming
    Replies: 2
    Last Post: 29th August 2011, 11:51
  3. QXmlStreamReader
    By sophister in forum Qt Programming
    Replies: 6
    Last Post: 24th August 2011, 17:31
  4. Mysterious QXMLStreamReader parsing behavior
    By ym1206 in forum Qt Programming
    Replies: 3
    Last Post: 14th June 2010, 19:56
  5. Need help with QXmlStreamReader
    By jknotzke in forum Qt Programming
    Replies: 2
    Last Post: 26th October 2009, 09:26

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
  •  
Qt is a trademark of The Qt Company.