import java.io.StringReader;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private Button start = null;
private TextView display = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button)findViewById(R.id.start);
start.setOnClickListener(new StartOnClickListener());
}
public class StartOnClickListener implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
//注意2点,第1:字符串中如果要换行写,则应该用引号和加号来相加得到,不能直接换行写
//第2点:如果字符串中有字符,则用括号里面的符合(")代替引号。
//我这里用的xml文件直接为该程序layout的布局xml文件
// String result_str =
// "RelativeLayout " +
// " xmlns:tools="http://schemas.android.com/tools" " +
// " android:layout_width="match_parent" " +
// " android:layout_height="match_parent" " +
//
// " Button " +
// " android:id="@+id/start" " +
// " android:layout_width="fill_parent" " +
// " android:layout_height="wrap_content" " +
// " android:layout_alignParentBottom="true" " +
// " android:text="Start XML Parse" " +
// " / " +
// " TextView " +
// " android:id="@+id/display" " +
// " android:layout_width="fill_parent" " +
// " android:layout_height="fill_parent" " +
// " android:gravity="center" " +
// " android:layout_alignParentLeft="true" " +
// " android:layout_above="@+id/start" " +
// " android:text="The XML Parse Example!!" " +
// " / " +
// " /RelativeLayout";
String result_str =
" Button " +
" id="@+id/start" " +
" layoutwidth="fillparent" " +
" layoutheight="wrapcontent" " +
" layoutalignParentBottom="true" " +
" text="Start XML Parse" " +
" /Button " ;
System.out.println(result_str);
try{
//创建SAX工厂
SAXParserFactory factory = SAXParserFactory.newInstance();
XMLReader reader = factory.newSAXParser().getXMLReader();
reader.setContentHandler(new MyContentHandler());
reader.parse(new InputSource(new StringReader(result_str)));
}
catch(Exception e) {
e.printStackTrace();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
MyContentHandler.java:
代码如下:
package com.example.xml;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class MyContentHandler extends DefaultHandler {
String tagname, sid, swidth, sheight, text, salign;
//当解析到标签的属性栏时调用该方法
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
if(tagname.equals("id"))
sid = new String(ch, start, length);
else if(tagname.equals("layoutwidth"))
swidth = new String(ch, start, length);
else if(tagname.equals("layoutheight"))
sheight = new String(ch, start, length);
else if(tagname.equals("layoutalignParentBottom"))
salign = new String(ch, start, length);
else if(tagname.equals("text"))
text = new String(ch, start, length);
super.characters(ch, start, length);
}
//解析文档结束调用
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
System.out.println("........end.......");
super.endDocument();
}
//解析标签结束时调用
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
if(tagname.equals("Button")) {
this.printout();
}
super.endElement(uri, localName, qName);
}
//解析文档开始时调用
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
System.out.println("........begin.......");
super.startDocument();
}
//标签开始时执行
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
tagname = localName;
if(tagname.equals("Button")) {
for(int i = 0; i attributes.getLength(); i++) {
//得到第i个属性的名字和值
System.out.println(attributes.getLocalName(i) + "=" + attributes.getValue(i));
}
}
super.startElement(uri, localName, qName, attributes);
}
//输出标签属性解析结果
private void printout() {
System.out.print("id: ");
System.out.println(sid);
System.out.print("layout_width: ");
System.out.println(swidth);
System.out.print("layout_height: ");
System.out.println(sheight);
System.out.print("layout_alignParentBottom: ");
System.out.println(salign);
System.out.print("text: ");
System.out.println(text);
}
}