2013-04-01 29 views
5

Dưới đây là một ví dụ về tập tin XML của tôi:XMLResourceParser, tôi không chắc chắn làm thế nào để đọc XML mà tôi có

<?xml version="1.0" encoding="utf-8"?> 
<assurances> 
<assurance hasReference="true"> 
    <message>You're Awesome!</message> 
    <reference>Genesis 1:26</reference> 
</assurance> 
<assurance hasReference="true"> 
    <message>Your Wonderfull!</message> 
    <reference>Genesis 1:26</reference> 
</assurance> 
</assurances> 

Tôi đang sử dụng mã như thế này để cố gắng lấy nó:

int eventType = -1; 
    while(eventType != XmlResourceParser.END_DOCUMENT) 
    { 
     XmlResourceParser assurances = getResources().getXml(R.xml.assurances); 
     String name = assurances.getText(); 
     Log.d(TAG, name); 

     try { 
      if (assurances.getEventType() == XmlResourceParser.START_TAG) { 
       String s = assurances.getName(); 

       if (s.equals("assurance")) { 
        String strMessage = assurances.getAttributeValue(null, "message"); 
        String strReference = assurances.getAttributeValue(null, "reference"); 

        Log.d(TAG, strMessage); 
        Log.d(TAG, strReference); 
       } 
      } 
     } catch (XmlPullParserException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

Nó không nhận được dữ liệu và tôi không chắc phải đi đâu từ đây.

Trả lời

3

Bạn không cần sử dụng phương thức getAttributeValue thay vào đó bạn cần sử dụng phương thức getText(). Tôi đã thực hiện một số thay đổi đối với mã của bạn, vì vậy nếu bạn sử dụng mã bên dưới thì nó sẽ hoạt động tốt:

int eventType = -1; 
while(eventType != XmlResourceParser.END_DOCUMENT) 
{ 
    XmlResourceParser assurances = getResources().getXml(R.xml.assurances); 
    String name = assurances.getText(); 
    Log.d(TAG, name); 

    try { 
     if (assurances.getEventType() == XmlResourceParser.START_TAG) { 
      String s = assurances.getName(); 

      if (s.equals("assurance")) { 
       assurances.next(); /// moving to the next node 
       if(assurances.getName() != null && assurances.getName().equalsIgnoreCase("message")){ 
        String strMessage = assurances.getText(); ///to get value getText() method should be used 
        assurances.next(); ///jumping on to the next node 
       String strReference = assurances.getText(); ////similar to above 
      } 

       Log.d(TAG, strMessage); 
       Log.d(TAG, strReference); 
      } 
     } 
    } catch (XmlPullParserException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 
+0

Cảm ơn vì điều đó, tôi đã có được nó làm việc với mã đó, tôi đã kết thúc thực hiện một cơ sở dữ liệu SQLite mặc dù. – mathacka

0

Hy vọng điều này sẽ giúp bạn trên đường. Trân S

public static void main(String[] args) { 
    try { 
     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder db = dbf.newDocumentBuilder(); 
     Document doc = db.parse(new File("c:\\_work\\test.xml")); 

     //get nodes by name 
     NodeList nl = doc.getElementsByTagName("assurance"); 
     int nbrOfElements = nl.getLength(); 
     for (int i = 0; i < nbrOfElements; i++) { 
      Element e = (Element) nl.item(i); 
      System.out.print(e.getTagName()); 
      System.out.print(":"); 
      System.out.println(e.getTextContent()); 
     } 
    } catch (Exception ex) { 
     Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 
Các vấn đề liên quan