[Solved]How can I get CDATA from xml



  • Hi all,
    I'm trying to use XmlListModel to get some data from xml file.
    All is well but title item is lose,here is my code:
    @XmlListModel {
    source: "site.xml"
    query: "/urlset/url/data/display"
    XmlRole { name: "website"; query: "website/string()" }
    XmlRole { name: "title"; query: "title/string()" }
    }@
    site.xml:
    <?xml version="1.0" encoding="UTF-8"?>
    <urlset count="82">
    <url id="1">
    <data>
    <display>
    <website>www.mywebsite.com</website>
    <title><![CDATA[[this is text what I need]]></title>
    </display>
    </data>
    </url>



  • Hi, as I see in your xml the line
    @
    <urlset count=“82”>
    @
    is not part of the quesy tree. Try querying to the next level:
    @
    XmlListModel {
    source: "site.xml"
    query: "/url/data/display"
    XmlRole { name: "website"; query: "website/string()" }
    XmlRole { name: "title"; query: "title/string()" }
    }
    @
    Is possible that urlset is wrong in your file and will be
    @
    <urlset count=82 />
    @
    To have your code working I think that the xml will be:
    @
    <?xml version=“1.0” encoding=“UTF-8”?>
    <urlset count=“82”>
    <url id=“1”>
    <data>
    <display>
    <website>www.mywebsite.com</website>
    <title><![CDATA[[this is text what I need]]></title>
    </display>
    </data>
    </url>
    </urlset>

    @



  • Hi, when XML parser parse an XML file it automatically create child node for title is called cdata-section so you can use:
    @XmlRole { name: "title"; query: "title/cdata-section/string()" }@



  • Sure, I know. Te main query is needed to point the "entry" of your xml tree where you want the fields, then you can sub-query every XmlRole.

    Useful the example ?



  • Dear all,
    I used new query way which mellkord tech me but no solved the problem.
    I just change back the code without 'cdata-section' now it works.
    Thank you help.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.