XMLParse

Qq q##发送回显请求

sendRequestWithOkhtp
  OkHttpClient okHttpClient=new OkHttpClient();//OKHttp开yuan
                    Request request=new Request.Builder().url("http://10.39.1.16/1.xml").build();
                    Response response=okHttpClient.newCall(request).execute();
                    String responsedata=response.body().string();

PULLParse

            String reslut="";
            XmlPullParserFactory factory=XmlPullParserFactory.newInstance();//拿到pull解析工厂
            XmlPullParser xmlpullParser=factory.newPullParser();//拿到pullparser解析器
            xmlpullParser.setInput(new StringReader(responsedata));//设置解析数据
            int evenType=xmlpullParser.getEventType();//获取当前解析事件
            String id="";
            String name="";
            String version="";
            while (evenType!=xmlpullParser.END_DOCUMENT){//不停的解析知道解析完成
                String nodename=xmlpullParser.getName();//获取当前节点名字
                switch (evenType){
                    case XmlPullParser.START_TAG:{
                        if ("id".equals(nodename)){
                            id=xmlpullParser.nextText();//获取内容
                        }else if ("name".equals(nodename)){
                            name=xmlpullParser.nextText();
                        }else if ("version".equals(nodename)){
                            version=xmlpullParser.nextText();
                        }

                    }
                    break;
                    case XmlPullParser.END_TAG:{//结束节点
                        if ("student".equals(nodename)){
                            Log.d("TAG", "parseXMlPull: "+id+name+version);
                         reslut=id+name+version;
//                            mTVshow.append(reslut);
                        }
                    }
                }
                evenType=xmlpullParser.next();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

SAXParse

借助一个继承DefaultHandler类

public class ContentHandler extends DefaultHandler {
    private String mnodeName;
    private StringBuilder mid;
    private StringBuilder mname;
    private StringBuilder mversion;
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        mnodeName=localName;
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if ("student".equals(localName)){
            Log.d("TAG", "parseXMlPull: "+mid.toString()+mname.toString()+mversion.toString());
            String reslut=mid.toString()+mname.toString()+mversion.toString();
            Log.d(TAG, "endElement: "+reslut);
            mid.setLength(0);
            mname.setLength(0);
            mversion.setLength(0);
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if ("id".equals(mnodeName)){
            mid.append(ch,start,length);
        }else if ("name".equals(mnodeName)){
            mname.append(ch,start,length);
        }else if ("version".equals(mnodeName)){
            mversion.append(ch,start,length);
        }
    }

    @Override
    public void startDocument() throws SAXException {
        mid=new StringBuilder();
        mname=new StringBuilder();
        mversion=new StringBuilder();
    }

    @Override
    public void endDocument() throws SAXException {
        super.endDocument();
    }
}

try{
            SAXParserFactory factroy=SAXParserFactory.newInstance();
            XMLReader xmlreader=factroy.newSAXParser().getXMLReader();
            ContentHandler handler=new ContentHandler();
            xmlreader.setContentHandler(handler);
            xmlreader.parse(new InputSource(new StringReader(responsedata)));
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

你可能感兴趣的:(XMLParse)