Xpath和条件

时间:2013-04-22 09:49:51

标签: java xpath

如果父节点下存在两个节点,我如何获得节点的值。 例如:我有以下Xml。

<?xml version="1.0" encoding="UTF-8"?>
<Services xmlns="http://sample.schema.com/abc">
    <service>
        <name>Sample</name>
        <uri>/v9.0/sample.123.com
        </uri>
    </service>
    <service>
        <name>Sample 2</name>
    </service>
    <service>
        <name>Sample 3</name>
        <uri>/v9.0/sample3.123.com
        </uri>
    </service>
    <service>
        <name>Sample 4</name>
        <uri>/v9.0/sample4.123.com
        </uri>
    </service>
    <service>
        <name>Sample 5</name>
        <uri>/v9.0/sample5.123.com
        </uri>
    </service>
    <service>
        <name>Sample 6</name>

    </service>
    <service>
        <name>Sample 7</name>
        <uri>/v9.0/sample7.123.com
            </uri>
    </service>
    <service>
        <name>Sample 8</name>
    </service>
</Services>

我的代码:

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class SimpleXpath {
    public static void main(String[] args) throws ParserConfigurationException,
            SAXException, IOException, XPathExpressionException {
          DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
            domFactory.setNamespaceAware(false); // never forget this!
            DocumentBuilder builder = domFactory.newDocumentBuilder();
            Document doc = builder.parse("testService1.xml");
            XPathFactory factory = XPathFactory.newInstance();
            XPath xpath = factory.newXPath();
            XPathExpression expr = xpath.compile("//Services/service[(name) and (uri)/text()]");
            Object result = expr.evaluate(doc, XPathConstants.NODESET);
            NodeList nodes = (NodeList) result;
           for (int i = 0; i < nodes.getLength(); i++) {
            String value=nodes.item(i).getNodeValue();
               System.out.println(" output : "+i+"  "+value); 
            }
    }

}

我想从xml上面读取name和url的值,如果名称和uri存在于服务节点下面。 我们可以看到一些服务节点只包含名称。我想避免那些。我的xpath表达式将null值作为输出。

如果服务包含两者,我如何获得name和uri的“文本”?

我可以使用xpath获取输出名称,将uri作为第二名(如果两者都存在于服务中)?

很多人。

约翰

4 个答案:

答案 0 :(得分:0)

首先,您可以使用更简单的xpath://Services/service[name and uri]。然后NodeList返回service个元素的列表。您可以迭代其他子项并查找名为nameuri的元素,然后获取其文本值。或者你可以再创建两个xpath表达式:

        XPathExpression exprName = xpath.compile("normalize-space(name)");
        String name = (String)exprName.evaluate(serviceNode, XPathConstants.STRING);

和uri一样。

答案 1 :(得分:0)

谢谢大家...... 以下代码满足要求。 如果有任何其他选择来提高质量,请建议。

public static void main(String[] args) throws ParserConfigurationException,
            SAXException, IOException, XPathExpressionException {
          DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
            domFactory.setNamespaceAware(false); // never forget this!
            DocumentBuilder builder = domFactory.newDocumentBuilder();
            Document doc = builder.parse("testService1.xml");
            XPathFactory factory = XPathFactory.newInstance();
            XPath xpath = factory.newXPath();
            XPathExpression expr = xpath.compile("//Services/service[(name) and (uri)]/name/text()");
            Object result = expr.evaluate(doc, XPathConstants.NODESET);
            NodeList nodes = (NodeList) result;

            XPathExpression expr1 = xpath.compile("//Services/service[(name) and (uri)]/uri/text()");
            Object result1 = expr1.evaluate(doc, XPathConstants.NODESET);
            NodeList nodes1 = (NodeList) result1;

           for (int i = 0; i < nodes1.getLength()&&i < nodes.getLength(); i++) {
               String value=nodes.item(i).getNodeValue();
            String value1=nodes1.item(i).getNodeValue();
               System.out.println(" output : "+i+"  "+value+ " "+ value1); 
            }
    }

答案 2 :(得分:0)

此xpath选择包含2个子项的元素

//*[count(*)=2]

答案 3 :(得分:-1)

for(int j=0; j<children.getLength();j++) {
   if (children.item(j) instanceof Element == false)
       continue;

   NamedNodeMap n = children.item(j).getAttributes();
   passwordTagAttr=(Attr) n.getNamedItem("name");
   passwordTagAttr=(Attr) n.getNamedItem("uri");
   passwordTag=stopTagAttr.getValue();  
   passwordList.add(passwordTag);                   
}