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. How to update child-data in XML file?
Forum Updated to NodeBB v4.3 + New Features

How to update child-data in XML file?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 2 Posters 564 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.
  • nishiokasN Offline
    nishiokasN Offline
    nishiokas
    wrote on last edited by nishiokas
    #1

    Hi.

    I'd like to change "commonPrimaryUri" value in this XML file.
    As follow.

    <?xml version="1.0" encoding="UTF-8"?>
    <tResource version="1.3.0">
        <common>
            <commonPrimaryUri>http://192.168.39.91:8000/common-shop/</commonPrimaryUri>
        </common>
    </thincaResource>
    

    I wrote this code.
    It can works, but this result is different from what I expected.

    QDomDocument xmlBOM;
    QFile f("/home/root/d/resource/resource.xml");
    if (f.open(QIODevice::ReadOnly))
    {
        xmlBOM.setContent(&f);
        f.close();
    
        QDomNodeList domList = xmlBOM.elementsByTagName("common");
        QDomElement element = domList.at(0).toElement();
        element.setAttribute("commonPrimaryUri", url);
        if (f.open(QIODevice::Truncate | QIODevice::WriteOnly))
        {
            QByteArray xml = xmlBOM.toByteArray();
            f.write(xml);
            f.close();
        }
    }
    

    this is result.

    <?xml version='1.0' encoding='UTF-8'?>
    <thincaResource version="1.3.0">
        <common commonPrimaryUri="http://1.2.3.4:8000/common-shop/">
            <commonPrimaryUri>http://192.168.39.91:8000/common-shop/</commonPrimaryUri>
            ...
    

    I don't want to change <common> data.
    I'd like to change <commonPrimaryUri> data.
    like this

    <common>
        <commonPrimaryUri>http://1.2.3.4:8000/common-shop/</commonPrimaryUri>
    </common>
    

    Which part should I change?
    Thanks.

    JonBJ 1 Reply Last reply
    0
    • nishiokasN nishiokas

      Hi.

      I'd like to change "commonPrimaryUri" value in this XML file.
      As follow.

      <?xml version="1.0" encoding="UTF-8"?>
      <tResource version="1.3.0">
          <common>
              <commonPrimaryUri>http://192.168.39.91:8000/common-shop/</commonPrimaryUri>
          </common>
      </thincaResource>
      

      I wrote this code.
      It can works, but this result is different from what I expected.

      QDomDocument xmlBOM;
      QFile f("/home/root/d/resource/resource.xml");
      if (f.open(QIODevice::ReadOnly))
      {
          xmlBOM.setContent(&f);
          f.close();
      
          QDomNodeList domList = xmlBOM.elementsByTagName("common");
          QDomElement element = domList.at(0).toElement();
          element.setAttribute("commonPrimaryUri", url);
          if (f.open(QIODevice::Truncate | QIODevice::WriteOnly))
          {
              QByteArray xml = xmlBOM.toByteArray();
              f.write(xml);
              f.close();
          }
      }
      

      this is result.

      <?xml version='1.0' encoding='UTF-8'?>
      <thincaResource version="1.3.0">
          <common commonPrimaryUri="http://1.2.3.4:8000/common-shop/">
              <commonPrimaryUri>http://192.168.39.91:8000/common-shop/</commonPrimaryUri>
              ...
      

      I don't want to change <common> data.
      I'd like to change <commonPrimaryUri> data.
      like this

      <common>
          <commonPrimaryUri>http://1.2.3.4:8000/common-shop/</commonPrimaryUri>
      </common>
      

      Which part should I change?
      Thanks.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @nishiokas said in How to update child-data in XML file?:

      element.setAttribute("commonPrimaryUri", url);

      This sets an attribute on (the first) xmlBOM.elementsByTagName("common");, resulting in <common commonPrimaryUri="http://1.2.3.4:8000/common-shop/">.

      You say you don't want that. You want to operate on the <commonPrimaryUri> child element of <common>. So do that. (Visit <common> children, or element.elementsByTagName("commonPrimaryUri").)

      1 Reply Last reply
      1
      • nishiokasN Offline
        nishiokasN Offline
        nishiokas
        wrote on last edited by
        #3

        @JonB Thank you for your reply!

        (Visit <common> children, or element.elementsByTagName("commonPrimaryUri").)

        I used "element.elementsByTagName("commonPrimaryUri")" instead of "xmlBOM.elementsByTagName("common")".
        But I couldn't get positive result.

        QDomNodeList domList = xmlBOM.elementsByTagName("commonPrimaryUri");
        QDomElement element = domList.at(0).toElement();
        element.setAttribute("", "http://1.2.3.4:8000/common-shop/");    
        // result  XML
        // <commonPrimaryUri ="http://1.2.3.4:8000/common-shop/">http://192.168.39.91:8000/common-shop/</commonPrimaryUri>
        element.setNodeValue("http://1.2.3.4:8000/common-shop/");
        //retult XML
        //nothing change
        element.setTagName("http://1.2.3.4:8000/common-shop/");
        //retult XML
        //  <http://1.2.3.4:8000/pos-api/close>http://192.168.39.91:8000/pos-api/close</http://1.2.3.4:8000/pos-api/close>
        

        I'm afraid I don't understand how to visit "<common> children".
        Should I use "appendChld" in QDomNodeList?

        How should I call this potision? "<tag>here!</tag>"
        I think it's "Value" but setNodeValue couldn't change anything.

        thanks.

        JonBJ 1 Reply Last reply
        0
        • nishiokasN nishiokas

          @JonB Thank you for your reply!

          (Visit <common> children, or element.elementsByTagName("commonPrimaryUri").)

          I used "element.elementsByTagName("commonPrimaryUri")" instead of "xmlBOM.elementsByTagName("common")".
          But I couldn't get positive result.

          QDomNodeList domList = xmlBOM.elementsByTagName("commonPrimaryUri");
          QDomElement element = domList.at(0).toElement();
          element.setAttribute("", "http://1.2.3.4:8000/common-shop/");    
          // result  XML
          // <commonPrimaryUri ="http://1.2.3.4:8000/common-shop/">http://192.168.39.91:8000/common-shop/</commonPrimaryUri>
          element.setNodeValue("http://1.2.3.4:8000/common-shop/");
          //retult XML
          //nothing change
          element.setTagName("http://1.2.3.4:8000/common-shop/");
          //retult XML
          //  <http://1.2.3.4:8000/pos-api/close>http://192.168.39.91:8000/pos-api/close</http://1.2.3.4:8000/pos-api/close>
          

          I'm afraid I don't understand how to visit "<common> children".
          Should I use "appendChld" in QDomNodeList?

          How should I call this potision? "<tag>here!</tag>"
          I think it's "Value" but setNodeValue couldn't change anything.

          thanks.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @nishiokas
          Please don't introduce setAttribute() or setTagName(), they are not what you want and just unnecessarily complicate the code.

          element.setNodeValue("http://1.2.3.4:8000/common-shop/");
          

          If I am not mistaken, <commonPrimaryUri> does not directly have the value. Rather, it has a child node, of type QDomText, which has the text you want to alter. Try enumerating <commonPrimaryUri>'s children to see if I am right.

          1 Reply Last reply
          1

          • Login

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