Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to replace a word between two elements in a XML file

How to replace a word between two elements in a XML file

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.7k 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.
  • B Offline
    B Offline
    Brikinhos
    wrote on 1 May 2015, 02:13 last edited by Brikinhos 5 Jan 2015, 05:08
    #1

    Hi, i'm working with the class QRegExp and i need to replace a word between two elements in a XML file, for example, i have this text

    <Vars>
    </Vars>
    <Code>
        cvNamedWindow("ventana_con_AUTOSIZE",1);
    	cvShowImage("ventana_con_AUTOSIZE", image);
     	cvWaitKey(0);
    	cvReleaseImage(&image);
    </Code>
    

    I need to replace the ampersand, or all ampersands, to & amp; between <Code> and </Code>, never before <Code> or after </Code>. I tried this

    QRegExp re_code"(?:<Code>)(?:.*)&(?!amp;)(?=.*</Code)"
    

    but it doesn't work, it replaces from <Code> to &. The result is this:

    <Vars>
    </Vars>
    &amp;image);
    </Code>
    

    I want to do the same with >, < and all illegal characters of XML that they have to be escaped.

    Thanks in advance

    K 1 Reply Last reply 1 May 2015, 10:06
    0
    • B Brikinhos
      1 May 2015, 02:13

      Hi, i'm working with the class QRegExp and i need to replace a word between two elements in a XML file, for example, i have this text

      <Vars>
      </Vars>
      <Code>
          cvNamedWindow("ventana_con_AUTOSIZE",1);
      	cvShowImage("ventana_con_AUTOSIZE", image);
       	cvWaitKey(0);
      	cvReleaseImage(&image);
      </Code>
      

      I need to replace the ampersand, or all ampersands, to & amp; between <Code> and </Code>, never before <Code> or after </Code>. I tried this

      QRegExp re_code"(?:<Code>)(?:.*)&(?!amp;)(?=.*</Code)"
      

      but it doesn't work, it replaces from <Code> to &. The result is this:

      <Vars>
      </Vars>
      &amp;image);
      </Code>
      

      I want to do the same with >, < and all illegal characters of XML that they have to be escaped.

      Thanks in advance

      K Offline
      K Offline
      koahnig
      wrote on 1 May 2015, 10:06 last edited by
      #2

      @Brikinhos
      I am not sure of how you like to replace the character.
      However, to me it looks that your match of QRegExp is not correct. It is:

      <Code>
          cvNamedWindow("ventana_con_AUTOSIZE",1);
          cvShowImage("ventana_con_AUTOSIZE", image);
          cvWaitKey(0);
          cvReleaseImage(&
      

      If you replace this, the result is logical. The result of your regular expression you can easily check if you are using the QRegExp example.

      Vote the answer(s) that helped you to solve your issue(s)

      B 1 Reply Last reply 1 May 2015, 14:09
      0
      • K koahnig
        1 May 2015, 10:06

        @Brikinhos
        I am not sure of how you like to replace the character.
        However, to me it looks that your match of QRegExp is not correct. It is:

        <Code>
            cvNamedWindow("ventana_con_AUTOSIZE",1);
            cvShowImage("ventana_con_AUTOSIZE", image);
            cvWaitKey(0);
            cvReleaseImage(&
        

        If you replace this, the result is logical. The result of your regular expression you can easily check if you are using the QRegExp example.

        B Offline
        B Offline
        Brikinhos
        wrote on 1 May 2015, 14:09 last edited by Brikinhos 5 Jan 2015, 14:11
        #3

        @koahnig I replace it using this piece of code

            QFile f(dir);
            if(!f.open(QIODevice::ReadOnly)) {
                QMessageBox::information(0, "error", f.errorString());
            }
            QTextStream in(&f);
            QString text= in.readAll();
            QRegExp re_code("(?:<Code>)(?:.*)&(?!amp;)(?=.*</Code)");
            text = text.replace(re_codigo,"&amp;");
        
        K 1 Reply Last reply 1 May 2015, 16:16
        0
        • B Brikinhos
          1 May 2015, 14:09

          @koahnig I replace it using this piece of code

              QFile f(dir);
              if(!f.open(QIODevice::ReadOnly)) {
                  QMessageBox::information(0, "error", f.errorString());
              }
              QTextStream in(&f);
              QString text= in.readAll();
              QRegExp re_code("(?:<Code>)(?:.*)&(?!amp;)(?=.*</Code)");
              text = text.replace(re_codigo,"&amp;");
          
          K Offline
          K Offline
          koahnig
          wrote on 1 May 2015, 16:16 last edited by
          #4

          @Brikinhos
          The problem is the match as already described in my previous reply.

              QRegExp re_code("((?:<Code>)(?:.*))(&)(?!amp;)(?=.*</Code)");
              text = text.replace(re_code, re_code.cap(1) +"&amp;");
          

          You may want to try the code above. I have changed the last two lines only of your code block.
          The text replaced is still the same, but it adds the part which is missing.
          Note: this brain to keyboard and not really tested. It is not elegant in my opinion, but it is the only solution I see at the moment. Maybe someone with more regex experience has a better solution.

          Vote the answer(s) that helped you to solve your issue(s)

          B 1 Reply Last reply 4 May 2015, 00:26
          1
          • K koahnig
            1 May 2015, 16:16

            @Brikinhos
            The problem is the match as already described in my previous reply.

                QRegExp re_code("((?:<Code>)(?:.*))(&)(?!amp;)(?=.*</Code)");
                text = text.replace(re_code, re_code.cap(1) +"&amp;");
            

            You may want to try the code above. I have changed the last two lines only of your code block.
            The text replaced is still the same, but it adds the part which is missing.
            Note: this brain to keyboard and not really tested. It is not elegant in my opinion, but it is the only solution I see at the moment. Maybe someone with more regex experience has a better solution.

            B Offline
            B Offline
            Brikinhos
            wrote on 4 May 2015, 00:26 last edited by Brikinhos 5 Apr 2015, 00:28
            #5

            @koahnig Hi, i did something that it works, but is a botched job xD, maybe it is usefull for anyone who reads this post. Now i can lie to Sax parse

                QRegExp re_amp("&(?!.*<Code>)(?=.*</Code>)");
                text.replace(re_amp,"&amp;");
            
            //    QRegExp re_lt("(?!.*<Code>)<(?=.*</Code>)"); This works but i don't know why 
            
                QRegExp re_lt("<(?!.*<Code>)(?=.*</Code>)");
                text.replace(re_lt,"&lt;");
            
                QRegExp re_lt_fix("&lt;(?=Code>)");
                text.replace(re_lt_fix,"<");
            
                QRegExp re_gt(">(?!.*<Code>)(?=.*</Code>)");
                text.replace(re_gt,"&gt;");
            
                QRegExp re_gt_fix("<Code&gt;");
                text.replace(re_gt_fix,"<Code>");
            

            I tried to use QRegularExpression but it works different, I'll try again in a future. Thank you very much anyway.

            1 Reply Last reply
            0

            3/5

            1 May 2015, 14:09

            • Login

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