[英]Parsing HTTP XML Response Using Regex In Java
我正在進行API調用,現在我需要從響應中獲取特定的數據。 我需要獲取“ 描述 ” 發票的DocumentID ,在以下情況下為110107。
我已經創建了一種通過執行以下操作從單個標簽獲取數據的方法:
public synchronized String getTagFromHTTPResponseAsString(String tag, String body) throws IOException {
final Pattern pattern = Pattern.compile("<"+tag+">(.+?)</"+tag+">");
final Matcher matcher = pattern.matcher(body);
matcher.find();
return matcher.group(1);
} // end getTagFromHTTPResponseAsString
但是 ,我的問題是這個結果集,有多個具有相同標簽的字段,我需要一個特定的字段。 這是響應:
<?xml version="1.0" encoding="utf-8"?>
<Order TrackingID="351535" TrackingNumber="TEST-843245" xmlns="">
<ErrorMessage />
<StatusDocuments>
<StatusDocument NUM="1">
<DocumentDate>7/14/2017 6:52:00 AM</DocumentDate>
<FileName>4215.pdf</FileName>
<Type>Sales Contract</Type>
<Description>Uploaded Document</Description>
<DocumentID>110098</DocumentID>
<DocumentPlaceHolder />
</StatusDocument>
<StatusDocument NUM="2">
<DocumentDate>7/14/2017 6:52:00 AM</DocumentDate>
<FileName>Apex_Shortcuts.pdf</FileName>
<Type>Other</Type>
<Description>Uploaded Document</Description>
<DocumentID>110100</DocumentID>
<DocumentPlaceHolder />
</StatusDocument>
<StatusDocument NUM="3">
<DocumentDate>7/14/2017 6:52:00 AM</DocumentDate>
<FileName>CRAddend.pdf</FileName>
<Type>Other</Type>
<Description>Uploaded Document</Description>
<DocumentID>110104</DocumentID>
<DocumentPlaceHolder />
</StatusDocument>
<StatusDocument NUM="4">
<DocumentDate>7/14/2017 6:52:00 AM</DocumentDate>
<FileName>test.pdf</FileName>
<Type>Other</Type>
<Description>Uploaded Document</Description>
<DocumentID>110102</DocumentID>
<DocumentPlaceHolder />
</StatusDocument>
<StatusDocument NUM="5">
<DocumentDate>7/14/2017 6:55:00 AM</DocumentDate>
<FileName>Invoice.pdf</FileName>
<Type>Invoice</Type>
<Description>Invoice</Description>
<DocumentID>110107</DocumentID>
<DocumentPlaceHolder />
</StatusDocument>
</StatusDocuments>
</Order>
我嘗試在https://regex101.com/上創建和測試我的正則表達式,並使該RegEx在那里工作,但是我無法獲得將其正確轉換為Java代碼的方法:
<Description>Invoice<\/Description>
<DocumentID>(.*?)<\/DocumentID>
例:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class sssaa {
public static void main(String[] args) throws Exception {
String xml = "yourXML";
Document doc = Jsoup.parse(xml);
Elements StatusDocuments = doc.select("StatusDocument");
for(Element e : StatusDocuments){
if(e.select("Description").text().equals("Invoice")){
System.out.println(e.select("DocumentID").text());
}
}
}
}
為了解決這個問題,我要做的是使用StringBuilder將響應轉換為單個字符串,然后使用這段代碼來獲取DocumentID:
// Create the pattern and matcher
Pattern p = Pattern.compile("<Description>Invoice<\\/Description><DocumentID>(.*)<\\/DocumentID>");
Matcher m = p.matcher(responseText);
// if an occurrence if a pattern was found in a given string...
if (m.find()) {
// ...then you can use group() methods.
System.out.println("group0 = " + m.group(0)); // whole matched expression
System.out.println("group1 = " + m.group(1)); // first expression from round brackets (Testing)
}
// Set the documentID for the Invoice
documentID = m.group(1);
看起來這可能不是執行此操作的最佳方法,但它目前正在起作用。 我將回來,嘗試從此處給出的建議中使用更正確的解決方案進行清理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.