2016-02-17 101 views

回答

1

嘗試使用XmlPullParser http://developer.android.com/training/basics/network-ops/xml.html

public class EmployeeXmlParser { 
    private static final String ns = null; 

    // We don't use namespaces 

    public Detail parse(InputStream in) throws XmlPullParserException, IOException { 
     try { 
      XmlPullParser parser = Xml.newPullParser(); 
      parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false); 
      parser.setInput(in, null); 
      parser.nextTag(); 
      return readDetail(parser); 
     } finally { 
      in.close(); 
     } 
    } 

    private Detail readDetail(XmlPullParser parser) throws XmlPullParserException, IOException { 
     Detail detail = new Detail(); 

     parser.require(XmlPullParser.START_TAG, ns, "employees"); 
     while (parser.next() != XmlPullParser.END_TAG) { 
      if (parser.getEventType() != XmlPullParser.START_TAG) { 
       continue; 
      } 
      String tag = parser.getName(); 
      // Starts by looking for the entry tag 
      if (tag.equals("detail")) { 
       detail.entries = readEmployees(parser); 
      } else { 
       skip(parser); 
      } 
     } 
     return detail; 
    } 

    private List<Employee> readEmployees(XmlPullParser parser) throws XmlPullParserException, IOException { 
     List<Employee> entries = new ArrayList<Employee>(); 

     parser.require(XmlPullParser.START_TAG, ns, "detail"); 
     while (parser.next() != XmlPullParser.END_TAG) { 
      if (parser.getEventType() != XmlPullParser.START_TAG) { 
       continue; 
      } 
      String tag = parser.getName(); 
      // Starts by looking for the entry tag 
      if (tag.equals("emp")) { 
       entries.add(readEmployee(parser)); 
      } else { 
       skip(parser); 
      } 
     } 
     return entries; 
    } 


    private Employee readEmployee(XmlPullParser parser) throws XmlPullParserException, IOException { 
     parser.require(XmlPullParser.START_TAG, ns, "emp"); 
     String name = parser.getAttributeValue(null, "name"); 
     String link = null; 
     while (parser.next() != XmlPullParser.END_TAG) { 
      if (parser.getEventType() != XmlPullParser.START_TAG) { 
       continue; 
      } 

      String tag = parser.getName(); 
      if (tag.equals("img")) { 
       link = readText(parser); 
      } else { 
       skip(parser); 
      } 
     } 
     return new Employee(name, link); 
    } 


    // For the tags name and summary, extracts their text values. 
    private String readText(XmlPullParser parser) throws IOException, XmlPullParserException { 
     String result = ""; 
     if (parser.next() == XmlPullParser.TEXT) { 
      result = parser.getText(); 
      parser.nextTag(); 
     } 
     return result; 
    } 

    // Skips tags the parser isn't interested in. Uses depth to handle nested tags. i.e., 
    // if the next tag after a START_TAG isn't a matching END_TAG, it keeps going until it 
    // finds the matching END_TAG (as indicated by the value of "depth" being 0). 
    private void skip(XmlPullParser parser) throws XmlPullParserException, IOException { 
     if (parser.getEventType() != XmlPullParser.START_TAG) { 
      throw new IllegalStateException(); 
     } 
     int depth = 1; 
     while (depth != 0) { 
      switch (parser.next()) { 
       case XmlPullParser.END_TAG: 
        depth--; 
        break; 
       case XmlPullParser.START_TAG: 
        depth++; 
        break; 
      } 
     } 
    } 

    public static class Detail { 
     public List<Employee> entries = new ArrayList<Employee>(); 
    } 

    public static class Employee { 
     public final String name; 
     public final String link; 

     private Employee(String name, String link) { 
      this.name = name; 
      this.link = link; 
     } 
    } 

} 
+0

感謝。你能爲我解釋嗎?因爲我更新在java – abcd1234

+0

非常感謝你。如果我想要活動,將顯示我選擇的img。怎麼做 – abcd1234