我无法使用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及其内容。
答案 0 :(得分:0)
尽管我已经通过从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>