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



  • 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


  • Moderators

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



  • @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;");
    

  • Moderators

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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.