0

我正在尝试使用 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>

我的活动类包含解析代码,实体类为 UserClass,其中包含用户名和密码参数。

我的活动

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        XmlPullParserFactory pullParserFactory;
        try {
            pullParserFactory = XmlPullParserFactory.newInstance();
            XmlPullParser parser = pullParserFactory.newPullParser();

                InputStream in_s = getApplicationContext().getAssets().open("temp.xml");
                parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
                parser.setInput(in_s, null);

                parseXML(parser);

        } catch (XmlPullParserException e) {

            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



    }

    private void parseXML(XmlPullParser parser) throws XmlPullParserException,IOException {

        UserClass userClass = null;

        int eventType = parser.getEventType();

        while(eventType!= XmlPullParser.END_DOCUMENT){
            String tagName = null;
            switch(eventType){

            case XmlPullParser.START_DOCUMENT:

                userClass = new UserClass();

            case XmlPullParser.START_TAG:

                tagName = parser.getName();

                if(tagName=="userName"){

                    userClass.setUsername(tagName);
                }

                if(tagName=="password"){

                    userClass.setPassword(tagName);
                }
                break;

            case XmlPullParser.END_DOCUMENT:
                printValues(userClass);
                break;

            }
        }

    }

    private void printValues(UserClass userClass) {

        System.out.println(userClass.getUsername()+"   "+userClass.getPassword());

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

我总是将 tagName 设为 null..Could'nt 找到它的原因。请帮助...

4

1 回答 1

0

此代码适用于我..根据您的需要适合它

编辑:从 iternet 解析:

 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();

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);
}
于 2013-08-23T13:31:59.633 回答