java - 无法使用 Java 从 XML 中读取节点元素及其值
问题描述
我无法使用 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>
推荐阅读
- c++ - unique_ptr 的有效性
- flutter - 如何将文本小部件长度固定为特定长度,然后应继续中断到下一行
- php - Apache2 服务器在同一文件夹中找不到文件
- python - python中MNIST数据集的KNN分类器
- excel - 对象定义的数据验证错误。VBA 错误中的数据验证
- filter - 根据文本框过滤子表单中的组合框
- c - fscanf 只读取一些字符
- typescript - 具有 TypeORM 存储库的通用类型
- bash - 如何在 bash/sed 中替换采用 shell 变量形式的字符串文字?
- javascript - 我收到错误列表中的每个孩子都应该有一个唯一的“关键”道具