1
回答
安卓程序,一个解析xml文件的问题
终于搞明白,存储TCO原来是这样算的>>>   

 最近照例程写了个xml文件解析到的程序解析结果有异常,调试了很久也没调试出来。功能就是把个标签里面的内容输出。代码如下:

MyContentHandler.java:
package com.example.xml;
 
 
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
 
 
import android.util.Log;
 
 
public class MyContentHandler extends DefaultHandler{
String tagName = null;
String linearLayout,textView,button ;
 
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
 
 
if(tagName.equals("TextView")){
 
 
textView = new String(ch,start,length);
Log.d("yinan", textView);
}
 
if(tagName.equals("Button")){
 
 
button = new String(ch,start,length);
Log.d("yinan", button);
}
 
}
 
 
@Override
public void endDocument() throws SAXException {
Log.d("yinan","end!");
}
 
 
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
Log.d("yinan", "endElement:"+tagName);
if(tagName.equals("Button")){
 
 
this.print();
}
 
}
 
 
@Override
public void startDocument() throws SAXException {
Log.d("yinan","start!");
}
 
 
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
tagName = localName;
Log.d("yinan", "start Element:"+tagName);
}
 
public void print(){
Log.d("yinan","TextView:");
Log.d("yinan",textView);
Log.d("yinan","Button:");
Log.d("yinan",button);
}
 
}
 
 
 
 
XMLActivity.java
package com.example.xml;
 
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
 
 
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
 
 
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
 
 
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 
 
public class XMLActivity extends Activity {
Button button = null;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xml);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new ParseButtonListener());
}
 
 
class ParseButtonListener implements OnClickListener{
 
 
@Override
public void onClick(View arg0) {
//Log.d("yinan", "!!!!!!!!!!!!!!!!!!"); 
try {
String str = getXml("/sdcard/main.xml");
 
//Log.d("yinan", "#############");
 
SAXParserFactory factory = SAXParserFactory.newInstance();
 
try {
XMLReader reader = factory.newSAXParser().getXMLReader();
reader.setContentHandler(new MyContentHandler());
reader.parse(new InputSource(new StringReader(str)));
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
 
} catch (IOException e) {
 
e.printStackTrace();
}
 
}
 
}
 
public String getXml(String s) throws IOException{
String str = null;
StringBuffer sb = new StringBuffer();
File file = new File(s);
if(!file.exists())
Log.d("yinan", "file does not exist!!");
FileInputStream input = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(input));
 
while((str = br.readLine()) != null){
sb.append(str);
}
input.close();
br.close();
return sb.toString();
}
 
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.xml, menu);
return true;
}
 
 
}



被解析的xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout>
<TextView>android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:text="@string/hello"
  </TextView>  
     
  <Button>   
    android:id="@+id/parseButton"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="开始解析XML"
   </Button>
 
</LinearLayout>



得到输出结果:


   可以看出只是输出了一个标签的内容,而且重复检测到了Button结束标签。 
请热心贴吧友帮助~~~~

举报
易水南风
发帖于3年前 1回/394阅
顶部