我有一个如下所示的字符串:
<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" />
并想要提取值:
transactionId;
paymentId;
amount;
dateTime;
mode;
referenceNo;
transactionType;
status;
isFlagged;
我怎么做java?
实际上它是服务器的响应,它是第三方,实际上不知道如何从响应中获取值。
感谢
答案 0 :(得分:1)
对我来说看起来像一个XML节点。我认为您正在寻找的是如何读取节点中的XML属性。
This可以帮到你。
答案 1 :(得分:1)
例如,通过以下代码:
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));
}
}
}
它给出了:
53264089
21575285
52.78
2013-12-13 15:04:42
TEST
80001186
Authorized
Processed
NO
答案 2 :(得分:1)
您可以使用Jsoup作为xml解析器。这很简单:
output
元素,这是代码示例
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());
输出:
transactionid -> 53264089
paymentid -> 21575285
amount -> 52.78
datetime -> 2013-12-13 15:04:42
mode -> TEST
referenceno -> 80001186
transactiontype -> Authorized
status -> Processed
isflagged -> NO
答案 3 :(得分:0)
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]);
输出:
53264089
21575285
52.78
2013-12-13 15:04:42
TEST
80001186
Authorized
Processed
NO