0

xml 可在http://thecybersoft.us/BridalExpo/Getmember.xml获得

    XPathFactory xpathfactory = XPathFactory.newInstance();
    XPath xpath = xpathfactory.newXPath();
    try {
         xpathexpression = xpath.compile("//@[name()='diffgr:id']");//bookstore//book
            result = xpathexpression.evaluate(doc,XPathConstants.NODESET);
           Log.v(result.toString(), "Value of result");
    } catch (XPathExpressionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

在上面的代码中,我按属性获取节点,如何获取各个节点的子节点。还有这个xml的根节点是什么

4

1 回答 1

0

您必须像这样遍历节点:

   XPathFactory xpathfactory = XPathFactory.newInstance();
   XPath xpath = xpathfactory.newXPath();
    try 
           {

                    expr = xpath.compile("//@[name()='diffgr:id']");
        result = expr.evaluate(rootDoc, XPathConstants.NODESET);
        nodes = (NodeList) result;
        for (int i = 0; i < nodes.getLength(); i++)

                   {
       userDTO.setUser_id((int)Integer.parseInt(nodes.item(i).getTextContent()));
        }

如此迭代,您将获得子节点。您所说的“根不”是什么意思。我不明白你的意思

于 2012-11-28T11:48:51.930 回答