adroid解析XML三种方式

废话少说,直接上代码:

Pull 解析     跳至 [1] [2] [3] [全屏预览]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
 
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
 
import android.util.Xml;
 
import com.leo.sax_parser.model.Person;
 
public class PullParser {
     public static List<Person> readXML(InputStream inputstream)
             throws XmlPullParserException, IOException {
         List<Person> persons = null ;
         XmlPullParser parser = Xml.newPullParser();
         parser.setInput(inputstream, "UTF-8" );
         int eventCode = parser.getEventType();
         Person person = null ;
         while (eventCode != XmlPullParser.END_DOCUMENT) {
             switch (eventCode) {
             case XmlPullParser.START_DOCUMENT:
                 persons = new ArrayList<Person>();
                 break ;
             case XmlPullParser.START_TAG:
                 if ( "person" .equals(parser.getName())) {
                     person = new Person();
                     person.setId(parser.getAttributeValue( 0 ));
                     
                 } else if (person != null ){
                     if ( "name" .equals(parser.getName())) {
                         person.setName(parser.nextText());
                     } else if ( "age" .equals(parser.getName())){
                         person.setAge(Integer.parseInt(parser.nextText()));
                     } else if ( "phoneNumber" .equals(parser.getName())){
                         person.setPhoneNumber(Integer.parseInt(parser.nextText()));
                     }
                 }
                 break ;
             case XmlPullParser.END_TAG:
                     if ( "person" .equals(parser.getName()) && person!= null ) {
                         persons.add(person);
                         person = null ;
                     }
                 break ;
             default :
                 break ;
             }
             eventCode = parser.next();
         }
 
         return persons;
     }
}

SAX 解析     跳至 [1] [2] [3] [全屏预览]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import java.util.ArrayList;
import java.util.List;
 
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
 
import android.util.Log;
 
import com.leo.sax_parser.model.Person;
 
public class SAX_handler extends DefaultHandler {
     private static final String Tag = "SAX_handler" ;
     private List<Person> persons;
     private String perTag;
     private Person person;
 
     public List<Person> getPersons() {
         return persons;
     }
 
     @Override
     public void startDocument() throws SAXException {
//初始化用于存放person对象的persons,用于存放读取到的相应的信息。
         persons = new ArrayList<Person>();
         Log.i(Tag, "startDocument" );
     }
 
     @Override
     public void startElement(String uri, String localName, String qName,
             Attributes attributes) throws SAXException {
         if ( "person" .equals(localName)) {
             for ( int i = 0 ; i < attributes.getLength(); i++) {
                 Log.i(Tag, "attributeName:" + attributes.getLocalName(i)
                         + "attributeValue:" + attributes.getValue(i));
                 person = new Person();
                 person.setId(attributes.getValue(i));
             }
         }
         perTag = localName;
         Log.i(Tag, qName + "startElement***" );
     }
 
     @Override
     public void characters( char [] ch, int start, int length)
             throws SAXException {
//       super.characters(ch, start, length);
         String data = new String(ch, start, length);
         if (! "" .equals(data.trim())) {
             Log.i(Tag, "Content:" + data);
         }
         if ( "name" .equals(perTag)) {
             person.setName(data);
         } else if ( "age" .equals(perTag)) {
             person.setAge(Integer.parseInt(data));
         } else if ( "phoneNumber" .equals(perTag)) {
 
             person.setPhoneNumber(Integer.parseInt(data));
         }
     }
 
     @Override
     public void endElement(String uri, String localName, String qName)
             throws SAXException {
         Log.i(Tag, qName + "endElement" );
         if (person != null && "person" .equals(localName)) {
             persons.add(person);
             person = null ;
         }
         perTag = null ;
     }
 
     @Override
     public void endDocument() throws SAXException {
         Log.i(Tag, "endDocument" );
     }
}

Dom 解析     跳至 [1] [2] [3] [全屏预览]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
 
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
 
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
 
import android.util.Log;
 
import com.leo.sax_parser.model.Person;
 
public class DomParser {
     private static final String Tag = "DomParser" ;
 
     public static List<Person> readXMLByDom(InputStream input)
             throws ParserConfigurationException, SAXException, IOException {
         List<Person> persons = new ArrayList<Person>();
         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
         DocumentBuilder builder = factory.newDocumentBuilder();
         Document document = builder.parse(input);
         Element root = document.getDocumentElement();
         NodeList nodes = root.getElementsByTagName( "person" );
         for ( int i = 0 ; i < nodes.getLength(); i++) {
             Element element = (Element) nodes.item(i);
             Person person = new Person();
             person.setId(element.getAttribute( "id" ));
             NodeList childNodes = element.getChildNodes();
             Log.i(Tag, childNodes.getLength() + "" );
             for ( int j = 0 ; j < childNodes.getLength(); j++) {
                 Node child = childNodes.item(j);
                 if (child.getNodeType() != Node.ELEMENT_NODE) { // 解决getChildNodes().getLength()与实际不符的问题
                     continue ;
                 }
                 Element childElement = (Element) child;
                 Log.i( "DomParser" , childElement.getNodeName() + ":"
                         + childElement.getTextContent().trim());
                 if ( "name" .equals(childElement.getNodeName())) {
                     person.setName(childElement.getTextContent().trim());
                 } else if ( "age" .equals(childElement.getNodeName())) {
                     person.setAge(Integer.parseInt(childElement
                             .getTextContent().trim()));
                 } else if ( "phoneNumber" .equals(childElement.getNodeName())) {
                     person.setPhoneNumber(Integer.parseInt(childElement
                             .getFirstChild().getNodeValue()));
                 }
             }
             persons.add(person);
         }
 
         return persons;
     }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值