Android——SAX方式解析XML数据

XML文件建立已经Apache服务器建立已经在上一篇介绍过,此处不再多过阐述
正文:
添加jar包

implementation 'com.squareup.okhttp3:okhttp:3.4.1'

闭包里申请网络权限


布局文件中就一个Button,此处不再阐述,解析的XML数据将以日志方式体现
建立一个ContentHandler类并继承DefaultHandler类,并重写父类的5个方法
ContentHandler.java代码如下:

public class ContentHandler extends DefaultHandler {
    private String nodeName;
    private StringBuilder id;
    private StringBuilder name;
    private StringBuilder version;
    //开始解析XML文件
    @Override
    public void startDocument() throws SAXException {
        super.startDocument();
        id = new StringBuilder();
        name = new StringBuilder();
        version = new StringBuilder();
    }
    //开始解析某个节点
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        super.startElement(uri, localName, qName, attributes);
        nodeName = localName;
    }
   //获取某个节点的内容
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        super.characters(ch, start, length);
        if ("id".equals(nodeName)){
            id.append(ch, start, length);
        }else if ("name".equals(nodeName)){
            name.append(ch, start, length);
        }else if ("version".equals(nodeName)){
            version.append(ch, start, length);
        }
    }
  //完成某个节点解析
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        super.endElement(uri, localName, qName);
        if ("app".equals(nodeName)) {
            Log.d("ID = ", id.toString().trim());
            Log.d("Name = ", name.toString().trim());
            Log.d("Version = ", version.toString().trim());
            //清空StringBuilder
            id.setLength(0);
            name.setLength(0);
            version.setLength(0);
        }
    }
  //完成整个XML文件解析
    @Override
    public void endDocument() throws SAXException {
        super.endDocument();
    }
}

MainActivity.java代码如下:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button SendRequest;
    private TextView ResponseText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SendRequest = (Button) findViewById(R.id.SendRequest);
        ResponseText = (TextView)findViewById(R.id.Response);
        SendRequest.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
      if (v.getId() == R.id.SendRequest){
          SendRequestToOkhttp();
      }
    }
    private void SendRequestToOkhttp() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient Client = new OkHttpClient();
                    //本机ip地址
                    Request request = new Request.Builder().url("http://192.168.0.60:8888/data.xml").build();//发送请求
                    Response response = Client.newCall(request).execute();//发送请求并获取服务器返回的数据
                    String ResponseData = response.body().string();//获取具体数据
                    ParseXMLWithSAX(ResponseData);//显示数据
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    private void ParseXMLWithSAX(String XMLData){
        try{
            SAXParserFactory factory = SAXParserFactory.newInstance();//获取SAXParserFactory实例
            XMLReader xmlReader = factory.newSAXParser().getXMLReader();
            ContentHandler handler = new ContentHandler();
            xmlReader.setContentHandler(handler);
            //开始解析
            xmlReader.parse(new InputSource(new StringReader(XMLData)));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(Android,Studio,java,android,xml,android,studio,安卓)