public void getXML(String url) throws XmlPullParserException,IOException,URISyntaxException { String xmlString=downloadXML(url); XmlPullParserFactory factory=XmlPullParserFactory.newInstance(); factory.setNamespaceAware(true); XmlPullParser parser=factory.newPullParser(); parser.setInput(new StringReader(xmlString)); int eventType=parser.getEventType(); List<Forecast_conditions> conditionsList=new ArrayList<Forecast_conditions>(); Forecast_conditions forecast_conditions=null; while (eventType != XmlPullParser.END_DOCUMENT) { if (eventType == XmlPullParser.START_DOCUMENT) { System.out.println("Start Document"); }else if (eventType== XmlPullParser.START_TAG) { System.out.println("Start Tag "+parser.getName()); if (parser.getName().equals("forecast_conditions")) { forecast_conditions=new Forecast_conditions(); } if (parser.getName().equals("day_of_week")) { forecast_conditions.setDay_of_week(parser.getAttributeValue(0)); }else if (parser.getName().equals("low")) { forecast_conditions.setLow(parser.getAttributeValue(0)); }else if (parser.getName().equals("high")) { forecast_conditions.setHigh(parser.getAttributeValue(0)); }else if (parser.getName().equals("icon")) { try { forecast_conditions.setIcon(parser.getAttributeValue(0)); } catch (Exception e) { // TODO: handle exception } }else if (parser.getName().equals("condition")) { try { forecast_conditions.setCondition(parser.getAttributeValue(0)); } catch(Exception e){ } } /* int size=parser.getAttributeCount(); for (int i = 0; i < size; i++) { System.out.println("AttributeName "+parser.getAttributeName(i)); System.out.println("AttributeValue"+parser.getAttributeValue(i)); } */ }else if(eventType== XmlPullParser.END_TAG){ System.out.println("End Tag "+parser.getName()); if (parser.getName().equals("forecast_conditions")) { conditionsList.add(forecast_conditions); } }else if(eventType==XmlPullParser.TEXT){ System.out.println("Text "+parser.getText()); } eventType=parser.next(); } System.out.println("End Document"); bindToListView(conditionsList); //XmlUtils. }
01.package com.st.demo; 02. 03.import java.io.File; 04.import java.io.FileInputStream; 05.import java.io.InputStream; 06. 07.import javax.xml.parsers.DocumentBuilder; 08.import javax.xml.parsers.DocumentBuilderFactory; 09. 10.import org.w3c.dom.Document; 11.import org.w3c.dom.Element; 12.import org.w3c.dom.Node; 13.import org.w3c.dom.NodeList; 14. 15.public class XmlReader { 16. public static void main(String[] args) { 17. XmlReader reader = new XmlReader(); 18. } 19. public XmlReader(){ 20. DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance(); 21. try { 22. DocumentBuilder domBuilder = domfac.newDocumentBuilder(); 23. InputStream is = new FileInputStream(new File("D:/test1.xml")); 24. Document doc = domBuilder.parse(is); 25. Element root = doc.getDocumentElement(); 26. NodeList books = root.getChildNodes(); 27. if(books!=null){ 28. for (int i = 0; i < books.getLength(); i++) { 29. Node book = books.item(i); 30. if(book.getNodeType()==Node.ELEMENT_NODE) { 31. //(7)取得节点的属性值 32. String email=book.getAttributes().getNamedItem("email").getNodeValue(); 33. System.out.println(email); 34. //注意,节点的属性也是它的子节点。它的节点类型也是Node.ELEMENT_NODE 35. //(8)轮循子节点 36. for(Node node=book.getFirstChild();node!=null;node=node.getNextSibling()) { 37. if(node.getNodeType()==Node.ELEMENT_NODE) { 38. if(node.getNodeName().equals("name")) { 39. String name=node.getNodeValue(); 40. String name1=node.getFirstChild().getNodeValue(); 41. System.out.println(name); 42. System.out.println(name1); 43. } 44. if(node.getNodeName().equals("price")) { 45. String price=node.getFirstChild().getNodeValue(); 46. System.out.println(price); 47. } 48. } 49. } 50. } 51. } 52. } 53. } catch (Exception e) { 54. // TODO Auto-generated catch block 55. e.printStackTrace(); 56. } 57. 58. } 59.}