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.
  • BrikinhosB Offline
    BrikinhosB Offline
    Brikinhos
    wrote on last edited by Brikinhos
    #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
    0
    • BrikinhosB Brikinhos

      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 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)

      BrikinhosB 1 Reply Last reply
      0
      • K koahnig

        @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.

        BrikinhosB Offline
        BrikinhosB Offline
        Brikinhos
        wrote on last edited by Brikinhos
        #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
        0
        • BrikinhosB Brikinhos

          @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 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)

          BrikinhosB 1 Reply Last reply
          1
          • K koahnig

            @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.

            BrikinhosB Offline
            BrikinhosB Offline
            Brikinhos
            wrote on last edited by Brikinhos
            #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

            • Login

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