2013-04-22 64 views
0

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

<?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名称和URL的值,如果用户名和URI是服务节点下存在。 我们可以看到一些服务节点只包含名称。我想避免那些。我的xpath表达式给出了空值作为输出。

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

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

Thanks很多。

约翰

+0

'/服务/服务/ url元素[文()]/..'也许? – 2013-04-22 10:00:16

+0

'/ Services/service [name and uri]'是否工作? – kan 2013-04-22 10:00:44

+0

伙计们,我把它们分开了,这个//服务/服务[(name)和(uri)]/uri/text()'xpath O/P输出:0 /v9.0/sample.123.com \t \t 输出:1个/v9.0/sample3.123.com \t \t 输出:2 /v9.0/sample4.123.com \t \t 输出:3 /v9.0/sample5.123.com \t \t 输出:4 /v9.0/sample7.123.com \t \t 只有5个匹配的情况可以看到xml其余的服务标签包含onlu的名字。 – 2013-04-22 10:03:42

回答

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相同。

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

此XPath选择有2个孩子

//*[count(*)=2]