首页 > 系统 > Android > 正文

Android 解析XML

2019-11-08 18:32:39
字体:
来源:转载
供稿:网友

我们手机通常使用的时候,需要一些数据如朋友圈、QQ空间等等,那么那些数据肯定是不在手机内存,而是在服务器里,我们通过请求数据,看它响应过来的是什么文件(通常是xml、JSON),然后根据服务器传过来的文件,进行相应的解析,拿到我们所需要的数据。 现在我就写怎么解析XML文件的吧! 解析XML一共有三种方法,第一种是最底层的、第二种是SAX,第三种是PULL。 一、解析XML文件之前,首先要得到你要解析的网络数据(URLConnection),在后台打印输出一下

//你要解析文件的路径URL url=new URL("http://www.vevb.com.cn/example/xmle/plant_catalog.xml"); HttpURLConnection connection=(HttpURLConnection)url.openConnection(); //设置请求方式 connection.setRequestMethod("GET"); //设置请求连接超时的时间(优化) connection.setConnectTimeout(5000); //结果码(成功:200,失败:404,304,500....) int code=connection.getResponseCode(); if(code==200){ //获取服务器返回过来的结果 InputStream is=connection.getInputStream(); //打印(读) BufferedReader br=new BufferedReader(new InputStreamReader(is)); String str=null; while((str=br.readLine())!=null){ Log.i("test",str); }

接下来就开始解析了: <1>底层做法(常说的DOM解析):

DocumentBuilderFactory documentBuilderFactory=DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder=(DocumentBuilder) documentBuilderFactory.newDocumentBuilder(); Document document=documentBuilder.parse(is); //获取根标签 Element root=document.getDocumentElement(); //根标签为:CATALOG Log.i("test","根标签为:"+root.getNodeName()); //获取<CATALOG>下面的子标签 NodeList nodeList=root.getElementsByTagName("PLANT"); for (int i = 0; i <nodeList.getLength() ; i++) { //获取单个 Element element= (Element) nodeList.item(i); //获取属性id的值// String id=element.getAttribute("id");// Log.i("test",element+"oooooo"); Element COMMONelement= (Element) element.getElementsByTagName("COMMON").item(0); String COMMON=COMMONelement.getTextContent(); Element BOTANICALelement= (Element) element.getElementsByTagName("BOTANICAL").item(0); String BOTANICAL=BOTANICALelement.getTextContent(); Element ZONEelement= (Element) element.getElementsByTagName("ZONE").item(0); String ZONE=ZONEelement.getTextContent(); Element LIGHTelement= (Element) element.getElementsByTagName("LIGHT").item(0); String LIGHT=LIGHTelement.getTextContent(); Element PRICEelement= (Element) element.getElementsByTagName("PRICE").item(0); String PRICE=PRICEelement.getTextContent(); Element AVAILABILITYelement= (Element) element.getElementsByTagName("AVAILABILITY").item(0); String AVAILABILITY=AVAILABILITYelement.getTextContent(); Log.i("test",ZONE+" 价格是"+PRICE); }

<2>、SAX解析(边读边解析),要实现5个方法

SAXParserFactory saxParserFactory= SAXParserFactory.newInstance(); SAXParser saxParser=saxParserFactory.newSAXParser(); saxParser.parse(is,new DefaultHandler(){ @Override public void startDocument() throws SAXException { super.startDocument(); } @Override public void endDocument() throws SAXException { super.endDocument(); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { super.startElement(uri, localName, qName, attributes); currentTag=localName; //获取开始标签名 } @Override public void endElement(String uri, String localName, String qName) throws SAXException { super.endElement(uri, localName, qName); currentTag=null; } @Override public void characters(char[] ch, int start, int length) throws SAXException { super.characters(ch, start, length); if("COMMON".equals(currentTag)){ //得到COMMON的值 String COMMON=new String(ch,start,length); Log.i("test","第一个:"+COMMON); }else if("BOTANICAL".equals(currentTag)){ //得到BOTANICAL的值 String BOTANICAL=new String(ch,start,length); Log.i("test","第二个:"+BOTANICAL); }else if("ZONE".equals(currentTag)){ //得到ZONE的值 String ZONE=new String(ch,start,length); Log.i("test","第三个:"+ZONE); }else if("LIGHT".equals(currentTag)){ //得到LIGHT的值 String LIGHT=new String(ch,start,length); Log.i("test","第四个:"+LIGHT); }else if("PRICE".equals(currentTag)){ //得到PRICE的值 String PRICE=new String(ch,start,length); Log.i("test","第五个:"+PRICE); }else if("AVAILABILITY".equals(currentTag)){ //得到AVAILABILITY的值 String AVAILABILITY=new String(ch,start,length); Log.i("test","第六个:"+AVAILABILITY); } } });

<3>、使用PULL解析

XmlPullParser xmlpullparse=Xml.newPullParser(); xmlpullparse.setInput(is,"UTF-8"); //获取解析的标签的类型 int type=xmlpullparse.getEventType(); while (type!=xmlpullparse.END_DOCUMENT){ switch (type){ case XmlPullParser.START_TAG: //获取标签名字 String startTag=xmlpullparse.getName(); if("PLANT".equals(startTag)){ Log.i("test","我是第一个标签"); }else if("COMMON".equals(startTag)){ String COMMON=xmlpullparse.nextText(); Log.i("test",COMMON); }else if("BOTANICAL".equals(startTag)){ String BOTANICAL=xmlpullparse.nextText(); Log.i("test",BOTANICAL); }else if("ZONE".equals(startTag)){ String ZONE=xmlpullparse.nextText(); Log.i("test",ZONE); }else if("LIGHT".equals(startTag)){ String LIGHT=xmlpullparse.nextText(); Log.i("test",LIGHT); }else if("PRICE".equals(startTag)){ String PRICE=xmlpullparse.nextText(); Log.i("test",PRICE); }else if("AVAILABILITY".equals(startTag)){ String AVAILABILITY=xmlpullparse.nextText(); Log.i("test",AVAILABILITY); } case XmlPullParser.END_TAG: } //细节: type=xmlpullparse.next(); }

注意:这是一个耗时操作,要写在子线程+Handler或者异步任务类AsyncTask中,不然会报错,还要写获得网络数据的权限

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表