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!

4 Answers 4

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>
Sign up to request clarification or add additional context in comments.

2 Comments

not working for <MessageRef>Trading01</MessageRef><MessageRef>Trading01</MessageRef><MessageRef>Trading01</MessageRef>
Change the regex by [A-Za-z0-9]* for example. You can also look for anything but < (i.e. [^<]* )
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>

3 Comments

Really cool alternative, was unaware of Jsoup, I will definitely try this out too. Thanks for taking the time to answer!
@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 :).
Thank you for the advice Pshemo! Will do.
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>")));

2 Comments

There is noting like var in java
OP wanted java not javascript :)
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()));

                }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.