0

I am writing a test case where my response could be of one the below two formats

<Amount currency="USD" xsi:Type="cc:MoneyType">"10.00"</Amount>

or

<Amount xsi:Type="cc:MoneyType" currency="USD">"10.00"</Amount>

And my code for finding the currency attribute value and value of amount is as follows,

Pattern pattern = Pattern.compile("<Amount currency=\"(\\S+)\" xsi:type=\"cc:Money\">(\\S+)</Amount>");
Matcher matcher = null;
Double sumOfAmount = 0.0;
String currency = null;
matcher = pattern.matcher(response);
while(matcher.find()) {
    currency = matcher.group(1);
    sumOfAmount += Double.valueOf(matcher.group(2));
}

But this code works only for the first format of the response, how should I change it to work to match the second type of format as well. In other words, ignore the ordering of the attributes.

g0c00l.g33k
  • 2,458
  • 2
  • 31
  • 41
  • 2
    Instead of string parsing using regular expressions, you could use xml deserialization, where you define Amount and Money classes. The deserializer will take care of creating objects from xml and you can work on objects directly. MoneyType can be an enum if you want – Sangram Jadhav May 31 '16 at 05:01
  • You should consider using an XML parser (q.v. [here](http://stackoverflow.com/questions/11863038/how-to-get-the-attribute-value-of-an-xml-node-using-java)), rather than trying to write a complex regex. – Tim Biegeleisen May 31 '16 at 05:02

2 Answers2

0

If you don't need to work with the type, I think this regex should work:

"<Amount .*currency=\"(\\S+)\".*>(\\S+)</Amount>"

But if you need the type, it would require a more sophisticated way.

Fábio Clug
  • 131
  • 1
  • 7
0

You can use alternation to make it order-independent:

<Amount(?:\s+(?:currency="([^"]*)"|xsi:Type="([^"]*)"))+>([^<>]+)</Amount>

...or as a Java string literal:

"<Amount(?:\\s+(?:currency=\"([^\"]*)\"|xsi:Type=\"([^\"]*)\"))+>([^<>]+)</Amount>"

But be aware that this regex is tailored to your example, as regexes always have to be, when dealing with XML or HTML. (For example, it doesn't account for optional whitespace, the simplest of many complicating issues.) For processing XML, you really should use a dedicated parser.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156