[英]Java XML JDOM2 XPath - Read text value from XML attribute and element using XPath expression
[英]Unable to read node element and its value from XML using Java
我無法使用XPath和DOMParser讀取XML元素及其值。 客戶端在我的應用程序中將此XML作為請求發送,我無法控制操作客戶端代碼。 我想使用DOMParser讀取AccountID。
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns="https://billpayment.weaut.com/">
<soap:Body>
<GetAccountBalanceByAccount xmlns="https://billpayment.weaut.com/">
<CompanyName>AABC</CompanyName>
<Language>ENG</Language>
<AccountID>54698214</AccountID>
</GetAccountBalanceByAccount>
</soap:Body>
</soap:Envelope>
這就是我試圖解析XML以獲取AccountID節點及其內容的方法。
@RequestMapping(value = "/", method = { RequestMethod.POST}, consumes = {"text/xml"}, produces = "text/xml")
public ResponseEntity messageStub(@RequestBody String requestString)
throws ClientProtocolException, IOException {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(new StringReader(requestString)));
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xpath = xPathFactory.newXPath();
javax.xml.namespace.NamespaceContext ns = new javax.xml.namespace.NamespaceContext()
{
@Override
public String getNamespaceURI(String prefix)
{
if ( "soap".equals( prefix ) )
{
return "http://www.w3.org/2003/05/soap-envelope";
}
return javax.xml.XMLConstants.NULL_NS_URI;
}
@Override
public String getPrefix(String namespaceURI)
{
return null;
}
@Override
public Iterator<?> getPrefixes(String namespaceURI)
{
return null;
}
};
xpath.setNamespaceContext(ns);
String nodeName="", nodeContent="";
NodeList nodeList = null;
//XML Path
String chkXMLPath="/soap:Envelope/soap:Body/GetAccountBalanceByAccount/AccountID";
XPathExpression expr = xpath.compile(chkXMLPath);
//evaluating each node set against the requested xml
Object result = expr.evaluate(doc, XPathConstants.NODESET);
nodeList = (NodeList) result;
//Here I am getting 0 node
System.out.println("Got " + nodeList.getLength() + " nodes");
for (int i = 0; i < nodeList.getLength(); i++)
{
nodeName = nodeList.item(i).getNodeName();
nodeContent = nodeList.item(i).getTextContent();
System.out.println("\nCurrent Element :" + nodeName);
System.out.println("\nCurrent Value :" + nodeContent);
break;
}
}
}
當我從兩個位置刪除xmlns命名空間后測試此方法時,我能夠讀取元素及其內容。 您能否建議我如何在不修改XML的情況下閱讀AccountID及其內容。
雖然我已經通過從XML中刪除xmlns名稱空間來解決了這個問題。 我使用以下方法來執行此操作。 這為我提供了適當的XML。
public static String RemoveAllXmlNamespace(String xmlData)
{
//Regex for xmlNS
String xmlnsRegex = "\\s+xmlns\\s*(:\\w)?\\s*=\\s*\\\"(?<url>[^\\\"]*)\\\"";
Pattern p = Pattern.compile(xmlnsRegex);
Matcher m = p.matcher(xmlData); //get a matcher object
int count = 0;
while(m.find())
{
String replaceString= xmlData.substring(m.start(), m.end());
//Removing xmlNS from the XML String
xmlData = xmlData.replace(replaceString, "");
System.out.println("xmlData: "+xmlData);
break;
}
return xmlData;
}
上面的代碼是我認為擺脫不需要的命名空間最安全的方法。 此方法在輸出中提供以下XML。
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Body>
<GetAccountBalanceByAccount>
<CompanyName>AABC</CompanyName>
<Language>ENG</Language>
<AccountID>54698214</AccountID>
</GetAccountBalanceByAccount>
</soap:Body>
</soap:Envelope>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.