zoukankan      html  css  js  c++  java
  • sax解析xml案例二

    ljq.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <persons>
    <person id="23">
    <name>李明</name>
    <age>30</age>
    </person>
    <person id="20">
    <name>李向梅</name>
    <age>25</age>
    </person>
    </persons>

       

    Person实体类

    package com.ljq.service;

    public class Person {

    private Integer id;
    private String name;
    private Short age;

    public Person() {
    }

    public Person(String name, Short age) {
    super();
    this.name = name;
    this.age = age;
    }

    public Integer getId() {
    return id;
    }

    public void setId(Integer id) {
    this.id = id;
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public Short getAge() {
    return age;
    }

    public void setAge(Short age) {
    this.age = age;
    }

    }

      

    XMLContentHandler

    package com.ljq.service;

    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 XMLContentHandler extends DefaultHandler {
    private List<Person> persons;
    private Person person;
    private String preTag;


    @Override
    public void startDocument() throws SAXException {
    persons
    = new ArrayList<Person>();
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
    if (person!=null){
    String date
    = new String(ch,start,length);
    if ("name".equals(preTag)) {
    person.setName(date);
    }
    else if ("age".equals(preTag)) {
    person.setAge(
    new Short(date));
    }
    }
    }

    @Override
    public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
    if ("person".equals(localName)) {
    person
    = new Person();
    person.setId(attributes.getIndex(
    "id"));
    }
    preTag
    = localName;
    }

    @Override
    public void endElement(String uri, String localName, String name) throws SAXException {
    if (person !=null && "person".equals(localName)) {
    persons.add(person);
    person
    = null;
    }
    preTag
    = null;
    }

    public List<Person> getPersons() {
    return persons;
    }

    }

       

    SaxPersonService

    package com.ljq.service;

    import java.io.InputStream;
    import java.util.List;

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

    import android.util.Log;

    public class SaxPersonService {
    // 业务层要对外抛出
    public static List<Person> readXML(InputStream inStream) throws Exception {
    SAXParserFactory spf
    = SAXParserFactory.newInstance();
    SAXParser saxParser
    = spf.newSAXParser(); // 创建解析器
    // 设置解析器的相关特性,http://xml.org/sax/features/namespaces = true
    // 表示开启命名空间特性
    // saxParser.setProperty("http://xml.org/sax/features/namespaces",true);
    XMLContentHandler handler = new XMLContentHandler();
    saxParser.parse(inStream, handler);
    inStream.close();
    return handler.getPersons();
    }

    }

        

    SaxPersonServiceTest

    package com.ljq.service;

    import java.io.InputStream;
    import java.util.List;

    import android.test.AndroidTestCase;
    import android.util.Log;

    public class SaxPersonServiceTest extends AndroidTestCase{
    private final String TAG = "SaxPersonServiceTest";
    public void testReadXML() throws Exception{
    InputStream inputStream
    = SaxPersonServiceTest.class.getClassLoader().getResourceAsStream("ljq.xml");
    List
    <Person> persons = SaxPersonService.readXML(inputStream);
    for(Person person : persons){
    Log.i(TAG, person.getId()
    + " : " + person.getName() + " : " + person.getAge());
    }
    }
    }
  • 相关阅读:
    viewController 不响应横竖屏转换相关消息的问题
    nsset排序
    内存相关
    技巧
    IOSTip
    iphone 资源
    IPhone 开发经验教训总结 仅供参考 (转载)
    WIN7控制面板假死
    Firefox添加web浏览端口:此地址使用了一个通常应该用于其他网页浏览的端口
    【WordPress】WXR version error when import into wordpress
  • 原文地址:https://www.cnblogs.com/linjiqin/p/1981083.html
Copyright © 2011-2022 走看看