Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    General and Desktop
    2
    5
    1414
    Loading More Posts
    • 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.
    • Brikinhos
      Brikinhos last edited by 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 1 Reply Last reply Reply Quote 0
      • K
        koahnig @Brikinhos last edited by

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

        Brikinhos 1 Reply Last reply Reply Quote 0
        • Brikinhos
          Brikinhos @koahnig last edited by 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 1 Reply Last reply Reply Quote 0
          • K
            koahnig @Brikinhos last edited by

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

            Brikinhos 1 Reply Last reply Reply Quote 1
            • Brikinhos
              Brikinhos @koahnig last edited by Brikinhos

              @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 Reply Quote 0
              • First post
                Last post