Java String Replace on an XML pattern

p0tta :

I have the following XML String:

String XML = "<TEST><MESSAGEID>5435646578</MESSAGEID></TEST>";

The number in the xml string keeps changing so I want to do a string replace and want to make the XML into

<TEST><MESSAGEID></MESSAGEID></TEST>

I am looking for doing something like this but I'm not sure how to get the pattern for the first argument in the replaceAll method.

public class HelloWorld {

    public static void main(String[] args) {
        String XML = "<MESSAGEID>5435646578</MESSAGEID>";
        String newStr = XML.replaceAll("<MESSAGEID>*</MESSAGEID>", "<MESSAGEID></MESSAGEID>");
        System.out.println(newStr);
    }
}
dvo :

try replacing your * in uotmXML.replaceAll("<MESSAGEID>*</MESSAGEID>", ... with [^<]+. This will match everything until the < character

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=308296&siteId=1