0

I have looked at several regex questions on the site but none work, sorry if people feel this repititon. I have this string in an XML file:

<MessageRef>Trading01</MessageRef>

Where Trading01 will be a different String (which needs to be manually input) every time the xml is generated. The objective is to automatically generate any unique value here for testing purposes. How do I remove whatever is between <MessageRef> and </MessageRef>?

I have tried this but it is not working:

 message.replaceAll("(<MessageRef>)[^&]*(</MessageRef>)", String.valueOf(System.currentTimeMillis()));

It's a simple issue I know but it has been annoying me all morning! Any help will be greatly appreciated!

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Reginald
  • 798
  • 1
  • 6
  • 16

4 Answers4

2

I tried this and it modified whatever is inside the XML tag:

message.replaceAll("(<MessageRef>)[^&]*(</MessageRef>)", "<MessageRef>" + String.valueOf(System.currentTimeMillis()) + "</MessageRef>");

Output:

<MessageRef>1435744441381</MessageRef>
mike
  • 1,956
  • 16
  • 22
2

Regex is not best tool to parse XML. Use parser instead. I like to use Jsoup for its simplicity (its main purpose was to be HTML parser so it supports CSS queries).

Here is code example

String text = "<MessageRef>Trading01</MessageRef>";

Document doc = Jsoup.parse(text, "", Parser.xmlParser());
System.out.println(doc);
System.out.println("---------");

Elements elements = doc.select("MessageRef");// cssQuery
for (Element el : elements) {//for each tag named MessageRef
    //set its text value to:
    el.text("date = " + new Date());
}

String replaced = doc.toString();
System.out.println(replaced);

Output:

<messageref>
 Trading01
</messageref>
---------
<messageref>
 now = Wed Jul 01 12:03:52 CEST 2015
</messageref>

If you want to prevent prettifying your text to match XML standards like adding new lines simply use

doc.outputSettings().prettyPrint(false);

and you will get

<messageref>Trading01</messageref>
---------
<messageref>date = Wed Jul 01 12:07:42 CEST 2015</messageref>
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • Really cool alternative, was unaware of Jsoup, I will definitely try this out too. Thanks for taking the time to answer! – Reginald Jul 01 '15 at 10:42
  • @Reginald You don't have to use Jsoup. Java have its own build-in XML parser (which you probably should start with to really appreciate simplicity of Jsoup :). – Pshemo Jul 01 '15 at 11:42
  • Thank you for the advice Pshemo! Will do. – Reginald Jul 01 '15 at 12:23
1

You can do it this way as well:

Regex: <MessageRef>(.*?)</MessageRef>

Example:

System.out.println(message.replaceAll("<MessageRef>(.*?)</MessageRef>", "<MessageRef>" + String.valueOf(System.currentTimeMillis() + "</MessageRef>")));
Pramod Karandikar
  • 5,289
  • 7
  • 43
  • 68
0

The following can also be done

    public static void main(String[] args) {
                    String input="<MessageRef>Trading01</MessageRef>";
                    System.out.println(input.substring(0,input.indexOf(">", 0)+1)+System.currentTimeMillis()+input.substring(input.indexOf("<",1),input.length()));

                }
KDP
  • 1,481
  • 7
  • 13