2013-08-23 77 views
0

我想解析XMLpullparser的本地xml文件,但無法獲取標記名稱。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的其中包含的用戶名和密碼的parameteres解析代碼。

我的活動

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

} 

我收到標記名空always..Could'nt找到it.Plz幫助的原因...

+1

使用'.equals'比較字符串,而不是''==不 – Raghunandan

+0

工作還是...... – user1901079

+0

我EVENTTYPE = parser.getEventType將返回0 .. – user1901079

回答

0

這個代碼適用於me..suit它根據您的需要

編輯:從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); 
} 
+0

此我們使用DOM解析,如果我是正確的。不過op正在使用xml pull解析器。推薦XmlPullParser,這是一種高效且可維護的方式來解析Android上的XML http://developer.android.com/training/basics/network-ops/xml.html – Raghunandan

+0

因此,如果它適合他,他可以更改他的代碼。 –

+0

其推薦的谷歌使用XML拉解析器不是DOM – Raghunandan