Android 返回空标记的Xml解析

Android 返回空标记的Xml解析,android,xml,xmlpullparser,Android,Xml,Xmlpullparser,我试图用XMLpullparser解析本地xml文件,但无法正确获取标记名 我的xml是 <?xml version="1.0"?> - <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" > - <s:Header> <Action xmlns="http://schemas.microsoft.com/ws/2005/

我试图用XMLpullparser解析本地xml文件,但无法正确获取标记名

我的xml是

<?xml version="1.0"?>
-
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" >
   -

    <s:Header>

        <Action
            xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none"
            s:mustUnderstand="1" >
                  http://tempuri.org/IUDTServices/ValidateUser

        </Action>
    </s:Header>
         -

    <s:Body>
 -

        <ValidateUser xmlns="http://tempuri.org/" >

            <userName>admin</userName>

            <password>admin</password>
        </ValidateUser>
    </s:Body>

</s:Envelope>

我得到的标记名总是空的..找不到它的原因。请帮助…

这段代码适合我..根据您的需要进行调整

编辑:用于从iternet进行解析: Dom解析器

DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("D:/workspace1/dd.xml");
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
//XPathExpression expr = xpath.compile("//resp/artist/images/image[@uri]");
XPathExpression expr = xpath.compile("//resp/artist/images/uri");
NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

for (int zzz = 0; zzz < nl.getLength(); zzz++)
{
    Node currentItem = nl.item(zzz);
    //String key = currentItem.getAttributes().getNamedItem("uri").getNodeValue();
    String key = currentItem.getTextContent();
    System.out.println(key);
}
DocumentBuilderFactory domFactory=DocumentBuilderFactory.newInstance();
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
DocumentBuilder=factory.newDocumentBuilder();
Document doc=builder.parse(“D:/workspace1/dd.xml”);
XPathFactory XPathFactory=XPathFactory.newInstance();
XPath=xPathfactory.newXPath();
//XPathExpression expr=xpath.compile(“//resp/artist/images/image[@uri]”);
XPathExpression expr=xpath.compile(“//resp/artist/images/uri”);
NodeList nl=(NodeList)expr.evaluate(doc,XPathConstants.NODESET);
对于(int zzz=0;zzz
使用
.equals
来比较字符串,而不是
=
我的eventType=parser.getEventType返回0。如果我是对的,这将使用dom parse。然而,op使用的是xml拉式解析器。推荐XmlPullParser,这是一种在Android上解析XML的高效且可维护的方法,谢谢你告诉我,我会记住这一点…你知道我如何使用上面的(我的代码)解析url中的信息吗?上面的代码应该这样做吗?(我已经编辑了代码)如果你有疑问,问一个新问题,为什么不能在这里回答?顺便问一下,问题在这里
 URL url = new URL("http://api.discogs.com/artist/ac/dc");
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new InputSource(url.openStream()));
            doc.getDocumentElement().normalize();
            XPathFactory xPathfactory = XPathFactory.newInstance();
        XPath xpath = xPathfactory.newXPath();
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("D:/workspace1/dd.xml");
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
//XPathExpression expr = xpath.compile("//resp/artist/images/image[@uri]");
XPathExpression expr = xpath.compile("//resp/artist/images/uri");
NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

for (int zzz = 0; zzz < nl.getLength(); zzz++)
{
    Node currentItem = nl.item(zzz);
    //String key = currentItem.getAttributes().getNamedItem("uri").getNodeValue();
    String key = currentItem.getTextContent();
    System.out.println(key);
}