0

如果父节点下存在两个节点,我如何获取节点的值。例如:我有以下 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 的值,如果 name 和 uri 存在于服务节点下。我们可以看到,有些服务节点只包含名称。我想避免那些。我的 xpath 表达式将 null 值作为输出。

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

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

非常感谢。

约翰

4

4 回答 4

0

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

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

和 uri 一样。

于 2013-04-22T10:08:25.740 回答
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); 
            }
    }
于 2013-04-22T10:17:28.090 回答
0

这个 xpath 选择有 2 个孩子的元素

//*[count(*)=2]
于 2013-04-25T21:55:17.090 回答
-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);                   
}
于 2013-04-22T10:59:01.290 回答