简体   繁体   中英

How to extract value from a specified string : java

I have a string like the below :

<output transactionId="53264089"
        paymentId="21575285"
        amount="52.78"
        dateTime="2013-12-13 15:04:42"
        mode="TEST"
        referenceNo="80001186"
        transactionType="Authorized"
        status="Processed"
        isFlagged="NO" />

and want to extract the values :

transactionId;
paymentId;
amount;
dateTime;
mode;
referenceNo;
transactionType;
status;
isFlagged;

how do i do in java?

Actually it is a response from the server which is third party and really do not know how to get the values from the response.

thanks

Looks like an XML node to me. I think what you're looking for is to how read XML attributes in a node.

This should help you.

For example, by following code:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Example {
     public static void main(String[] args) {
         String input = "<output transactionId=\"53264089\" paymentId=\"21575285\" " +
                "amount=\"52.78\" dateTime=\"2013-12-13 15:04:42\" " +
                    "mode=\"TEST\" referenceNo=\"80001186\" transactionType=\"Authorized\" " +
                        "status=\"Processed\" isFlagged=\"NO\"/>";

         Pattern pattern = Pattern.compile("\"([^\"]*)\"");
         Matcher matcher = pattern.matcher(input);

         while(matcher.find()) {
           System.out.println(matcher.group(1));
         }   
     }
}

it gives:

53264089
21575285
52.78
2013-12-13 15:04:42
TEST
80001186
Authorized
Processed
NO

You can use Jsoup as xml parser. It is very straightforward:

  1. select output elements,
  2. pick first one from founded,
  3. get its attributes
  4. iterate over all attributes

Here is code sample

String xml = "<output transactionId=\"53264089\"\r\n" + 
        "        paymentId=\"21575285\"\r\n" + 
        "        amount=\"52.78\"\r\n" + 
        "        dateTime=\"2013-12-13 15:04:42\"\r\n" + 
        "        mode=\"TEST\"\r\n" + 
        "        referenceNo=\"80001186\"\r\n" + 
        "        transactionType=\"Authorized\"\r\n" + 
        "        status=\"Processed\"\r\n" + 
        "        isFlagged=\"NO\" />";


Document doc = Jsoup.parse(xml, "", Parser.xmlParser());
Attributes attr = doc.select("output").first().attributes();
for (Attribute a : attr)
    System.out.printf("%-15s -> %s%n", a.getKey(), a.getValue());

Output:

transactionid   -> 53264089
paymentid       -> 21575285
amount          -> 52.78
datetime        -> 2013-12-13 15:04:42
mode            -> TEST
referenceno     -> 80001186
transactiontype -> Authorized
status          -> Processed
isflagged       -> NO
String str ="<output transactionId=\"53264089\" paymentId=\"21575285\" amount=\"52.78\"   dateTime=\"2013-12-13 15:04:42\" mode=\"TEST\" referenceNo=\"80001186\" transactionType=\"Authorized\" status=\"Processed\" isFlagged=\"NO\" />";
String[] values = str.split("\"");
for(int i=1;i<values.length;i+=2)
System.out.println(values[i]);

output:

 53264089
 21575285
 52.78
 2013-12-13 15:04:42
 TEST
 80001186
 Authorized
 Processed
 NO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM