![](/img/trans.png)
[英]Regular expression to extract Date value specified in word from the string in Java
[英]How to extract value from a specified string : java
我有一個如下所示的字符串:
<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?
實際上它是服務器的響應,它是第三方,實際上不知道如何從響應中獲取值。
謝謝
對我來說看起來像一個XML節點。 我認為您正在尋找的是如何讀取節點中的XML屬性。
這應該對你有幫助。
例如,通過以下代碼:
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
您可以將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
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.