21天学习android开发教程之XML解析与生成

2019-12-10 19:08:34于海丽

<linearlayout xmlns:android="http://www.easck.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <button android:layout_height="wrap_content" 
        android:layout_width="fill_parent" android:id="@+id/btnSAX"
        android:text="使用SAX解析XML">
    <button android:layout_height="wrap_content" 
        android:layout_width="fill_parent" android:text="生成XML" android:id="@+id/btnOutput">
    <edittext android:text="@+id/EditText01" android:id="@+id/EditText01" 
        android:layout_width="fill_parent" android:layout_height="fill_parent">

testSAX.java的源码如下:

package com.testSAX;

import java.io.StringWriter;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xmlpull.v1.XmlSerializer;

import android.app.Activity;
import android.os.Bundle;

import android.util.Xml;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class testSAX extends Activity {
    /** Called when the activity is first created. */
    Button btnSAX, btnOutput;
    EditText memo;
    SAXHandler handler = new SAXHandler();

    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btnSAX = (Button) this.findViewById(R.id.btnSAX);
        btnSAX.setOnClickListener(new ClickEvent());
        btnOutput = (Button) this.findViewById(R.id.btnOutput);
        btnOutput.setOnClickListener(new ClickEvent());
        memo = (EditText) this.findViewById(R.id.EditText01);

    }

    class ClickEvent implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            if (v == btnSAX) {//解析XML,并保存标记,属性等值
                try {
                    SAXParserFactory factory = SAXParserFactory.newInstance();
                    SAXParser parser = factory.newSAXParser();
                    XMLReader reader = parser.getXMLReader();
                    reader.setContentHandler(handler);
                    reader.parse(new InputSource(testSAX.this.getResources()
                            .openRawResource(R.raw.test)));
                } catch (Exception ee) {}
            }
            else if (v == btnOutput) {//生成XML
                try {
                    XmlSerializer serializer = Xml.newSerializer();
                    StringWriter writer = new StringWriter();
                    try {
                        serializer.setOutput(writer);
                        serializer.startDocument("UTF-8",true);
                        
                        for(int i=0;i<handler.getkeys().size();i++)
                        {
                            if(handler.GetKeys().get(i).equals("startTag"))
                            {
                                serializer.startTag("", (String) handler.GetValues().get(i));
                            }
                            else if(handler.GetKeys().get(i).equals("Attr")){
                                String[] str= (String[]) handler.GetValues().get(i);
                                serializer.attribute("",str[0],str[1]);
                            }
                            else if(handler.GetKeys().get(i).equals("text"))
                                serializer.text((String)handler.GetValues().get(i));
                            else if(handler.GetKeys().get(i).equals("endTag"))
                            {
                                serializer.endTag("", (String) handler.GetValues().get(i));
                            }
                        }
                        serializer.endDocument();
                        String text=writer.toString();
                        text=text.replace("><", ">/r/n<");
                        memo.setText(text);//输出到文本框
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                    
                } catch (Exception e) {}
            }

        }

    }
}