Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Solved]How can I get CDATA from xml
Forum Update on Monday, May 27th 2025

[Solved]How can I get CDATA from xml

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 3 Posters 5.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    leaf
    wrote on last edited by
    #1

    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>

    1 Reply Last reply
    0
    • AlicemirrorA Offline
      AlicemirrorA Offline
      Alicemirror
      wrote on last edited by
      #2

      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>

      @

      Enrico Miglino (aka Alicemirror)
      Balearic Dynamics
      Islas Baleares, Ibiza (Spain)
      www.balearicdynamics.com

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mellkord
        wrote on last edited by
        #3

        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()" }@

        1 Reply Last reply
        0
        • AlicemirrorA Offline
          AlicemirrorA Offline
          Alicemirror
          wrote on last edited by
          #4

          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 ?

          Enrico Miglino (aka Alicemirror)
          Balearic Dynamics
          Islas Baleares, Ibiza (Spain)
          www.balearicdynamics.com

          1 Reply Last reply
          0
          • L Offline
            L Offline
            leaf
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved