android:我用属性获取节点如何获取其子节点

时间:2012-11-28 08:06:04

标签: android xml xml-parsing android-parser

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的根节点是什么

1 个答案:

答案 0 :(得分: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()));
        }

迭代,你将获得子节点。你是什​​么意思“Root Not”。我没有得到你