我有一个xml如下。
<fieldConstraint>
<fieldBinding>
<fieldName>instanceId</fieldName>
<classType>Integer</classType>
<bindingName>$instanceId</bindingName>
<genericType>Number</genericType>
</fieldBinding>
</fieldConstraint>
<fieldConstraint>
<fieldBinding>
<fieldName></fieldName>
<classType>String</classType>
<bindingName>$alertText</bindingName>
<genericType>String</genericType>
</fieldBinding>
<fieldBinding>
<fieldName>alertText22</fieldName>
<classType>String22</classType>
<bindingName>$alertText</bindingName>
<genericType>String</genericType>
</fieldBinding>
</fieldConstraint>
我需要通过某个特殊符号(#)分隔<fieldName>
值和<classType>
值。
例如:instanceId #Integer(所需的输出应如下所示)
我正在使用java尝试使用xpath。我无法获得所需的输出。
List importedFields =new ArrayList();
DocumentBuilderFactory domFactory =
DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("abc.xml");
XPath xpath = XPathFactory.newInstance().newXPath();
NamespaceContext context = new NamespaceContextMap(
"foo", "http://www.cisco.com/BRL");
xpath.setNamespaceContext(context);
XPathExpression expr = xpath.compile("concat(//foo:fieldConstraint/foo:fieldBinding/foo:fieldName/text(),//foo:fieldConstraint/foo:fieldBinding/foo:classType/text())");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
Node currentItem = nodes.item(i);
String value = currentItem.getTextContent();
importedFields.add(value);
System.out.println("FIELD ARRAY list IS:"+importedFields);
我收到以下异常。
你的帮助赞赏。
Exception in thread "main"
com.sun.org.apache.xpath.internal.XPathException: Can not convert #STRING to a NodeList!
at com.sun.org.apache.xpath.internal.objects.XObject.error(Unknown Source)
at com.sun.org.apache.xpath.internal.objects.XObject.nodelist(Unknown Source)
at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.getResultAsType(Unknown Source)
at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(Unknown Source)
at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(Unknown Source)
at com.cisco.sample.Xpath.main(Xpath.java:47)
--------------- linked to ------------------
javax.xml.xpath.XPathExpressionException
at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(Unknown Source)
at com.cisco.sample.Xpath.main(Xpath.java:47)
Caused by: com.sun.org.apache.xpath.internal.XPathException: Can not convert #STRING to a NodeList!
at com.sun.org.apache.xpath.internal.objects.XObject.error(Unknown Source)
at com.sun.org.apache.xpath.internal.objects.XObject.nodelist(Unknown Source)
at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.getResultAsType(Unknown Source)
at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(Unknown Source)
... 2 more
答案 0 :(得分:2)
concat
函数返回一个String,但您使用的xpath求值会尝试返回NodeList。
尝试:
Object result = expr.evaluate(doc, XPathConstants.STRING);
String s = (String)result;
更新
要获取所有元素,您可以执行以下操作:
NodeList nodeList = doc.getElementsByTagName("fieldBinding");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
Node fieldNameNode = element.getElementsByTagName("fieldName").item(0);
String fieldName = fieldNameNode.getNodeValue();
Node classTypeNode = element.getElementsByTagName("classType").item(0);
String classType = classTypeNode.getNodeValue();
}
}
答案 1 :(得分:1)
将以下行更改为XPathConstants.STRING
,因为您返回的字符串不是节点列表
Object result = expr.evaluate(doc, XPathConstants.STRING);
和XPATH
XPathExpression expr = xpath.compile("concat(//foo:fieldConstraint/foo:fieldBinding/foo:fieldName/text(),'#', //foo:fieldConstraint/foo:fieldBinding/foo:classType/text())");