*******************************************文件放在assets中***和DOM解析不同的是:SAX解析需要写Bean一个类***********************************************************
***************************************************Bean*********************************************************************
public class Languages { private String id; private String name; private String ide; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getIde() { return ide; } public void setIde(String ide) { this.ide = ide; } @Override public String toString() { return "Languages [id=" + id + ",name=" + name + ",ide=" + ide + "]"; } }
import java.io.IOException; import java.util.List; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.SAXException; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends Activity { private TextView text; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text = (TextView) findViewById(R.id.text); /** try { // 创建工厂类 DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); // 创建DocumentBuilder DocumentBuilder builder = factory.newDocumentBuilder(); //创建Document Document document = builder.parse(getAssets().open("languages.xml")); //获得根元素 Element element = document.getDocumentElement(); //获取到列表 String category = element.getAttribute("category"); text.append(category+"\n"); //获取到子节点 NodeList list = element.getElementsByTagName("item"); for (int i = 0; i < list.getLength(); i++) { Element element2 = (Element) list.item(i); String id = element2.getAttribute("id"); text.append(id+"\n"); String name = element2.getElementsByTagName("name").item(0).getTextContent(); text.append(name+"\n"); String ide = element2.getElementsByTagName("ide").item(0).getTextContent(); text.append(ide+"\n"); } } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } */ try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); SAXHandler handler = new SAXHandler(); parser.parse(getAssets().open("languages.xml"),handler); List<Languages> languageList = handler.getLanguageList(); for (Languages language : languageList) { text.append(language.toString() + "\n"); } } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
import java.util.ArrayList; import java.util.List; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class SAXHandler extends DefaultHandler { private List<Languages> languageList; private Languages language; private String tagName; @Override public void startDocument() throws SAXException { // TODO Auto-generated method stub super.startDocument(); System.out.println("===================开始解析文档=================="); languageList = new ArrayList<Languages>(); } @Override public void startElement(String uri,String localName,String qName,Attributes attributes) throws SAXException { // TODO Auto-generated method stub super.startElement(uri,localName,qName,attributes); tagName = qName; System.out.println("-------------开始解析" + qName + "元素----------"); if (qName.equals("item")) { language = new Languages(); language.setId(attributes.getValue("id")); } } @Override public void characters(char[] ch,int start,int length) throws SAXException { // TODO Auto-generated method stub super.characters(ch,start,length); String value = new String(ch,length); if (tagName.equals("name") && !value.trim().equals("")) { language.setName(value); }else if(tagName.equals("ide") && !value.trim().equals("")){ language.setIde(value); } } @Override public void endElement(String uri,String qName) throws SAXException { // TODO Auto-generated method stub super.endElement(uri,qName); System.out.println("-------------结束解析" + qName + "元素----------"); if (qName.equals("item")) { languageList.add(language); } } @Override public void endDocument() throws SAXException { // TODO Auto-generated method stub super.endDocument(); System.out.println("===================结束解析文档=================="); } public List<Languages> getLanguageList() { return languageList; } }***********************************************************以上的XML解析都是很简单的,也容易忘记,希望可以帮助大家********************************************************** 原文链接:https://www.f2er.com/xml/295053.html