开发者

xpath concat() function with java

I have an xml as below.

<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>

I need to get <fieldName> value and <classType> value seperate by some special symbol(#).

forexample: instanceId#Integer (The desired output should look like this)

I am trying xpath using java as below.I could not able get the desired ouput.

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())");开发者_如何学Go
  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);

I am getting the below exception.

Your help Appreciated.

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


The concat function is returning a String, but the xpath evaluation you use tries to return a NodeList.

Try:

Object result = expr.evaluate(doc, XPathConstants.STRING);
String s = (String)result;

UPDATE

To get all the elements you can do the following:

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();
  }
}


Change the following line to XPathConstants.STRING as you are getting back a string not a nodelist

Object result = expr.evaluate(doc, XPathConstants.STRING);

and for the XPATH

XPathExpression expr = xpath.compile("concat(//foo:fieldConstraint/foo:fieldBinding/foo:fieldName/text(),'#', //foo:fieldConstraint/foo:fieldBinding/foo:classType/text())");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜