1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | 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.}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步