Chinaunix首页 | 论坛 | 博客
  • 博客访问: 896120
  • 博文数量: 322
  • 博客积分: 6688
  • 博客等级: 准将
  • 技术积分: 3626
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-19 11:26
文章分类

全部博文(322)

文章存档

2013年(5)

2012年(66)

2011年(87)

2010年(164)

分类: Java

2010-12-31 11:17:49

解析文件

src下wp.

代码

1
2
3
4
5
6
7
8
9
10
11
 version="1.0" encoding="UTF-8"?>  
>  
     id="23">  
        >张三>  
        >20>  
    >  
     id="20">  
        >小明>  
        >26>  
    >  
>

org.wp.domain

Person

Java代码

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
package org.wp.domain;  
 
public class Person {  
 
    private Integer id;  
    private String name;  
    private Short age;  
 
    public Person() {  
 
    }  
 
    public Person(String name, Short age) {  
        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;  
    }  
 
    @Override  
    public String toString() {  
        return "Id=" + id + ",Name=" + name + ",Age=" + age;  
    }  
}

org.wp.service

SAXPersonService

Java代码

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
package org.wp.service;  
 
import java.io.InputStream;  
import java.util.List;  
import javax.xml.parsers.SAXParser;  
import javax.xml.parsers.SAXParserFactory;  
import org.wp.domain.Person;  
 
public class SAXPersonService {  
 
    public static List<Person> readXml(InputStream inStream) throws Exception {  
 
        // 得到SAX解析器的工厂实例  
        SAXParserFactory spf = SAXParserFactory.newInstance();  
 
        // 从SAX工厂实例中获得SAX解析器  
        SAXParser saxParser = spf.newSAXParser();  
 
        // 设置解析器的相关特性, = true  
        // 表示开启命名空间特性  
        // saxParser.setProperty("",true);  
 
        XMLContentHandler handler = new XMLContentHandler();  
        saxParser.parse(inStream, handler);  
        inStream.close();  
        return handler.getPersons();  
    }  
 
}

XMLContentHandler

Java代码

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
package org.wp.service;  
 
import java.util.ArrayList;  
import java.util.List;  
import org.wp.domain.Person;  
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 data = new String(ch, start, length);  
            if ("name".equals(preTAG)) {  
                person.setName(data);  
            } else if ("age".equals(preTAG)) {  
                person.setAge(new Short(data));  
            }  
        }  
    }  
 
    @Override  
    public void startElement(String uri, String localName, String qName,  
            Attributes attributes) throws SAXException {  
        if ("person".equals(localName)) {  
            person = new Person();  
            person.setId(new Integer(attributes.getValue(0)));  
        }  
        preTAG = localName;  
    }  
 
    @Override  
    public void endElement(String uri, String localName, String qName)  
            throws SAXException {  
        if ("person".equals(localName) && person != null) {  
            persons.add(person);  
            person = null;  
        }  
        preTAG = null;  
    }  
 
    public List<Person> getPersons() {  
        return persons;  
    }  
}

org.wp.

SAXPersonServiceTest

Java代码

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
package org.wp.xml;  
 
import java.io.InputStream;  
import java.util.List;  
import org.wp.domain.Person;  
import org.wp.service.SAXPersonService;  
import android.test.AndroidTestCase;  
import android.util.Log;  
 
/** 
 * Java中的getResourceAsStream有以下几种 
 *  
 * 1. Class.getResourceAsStream(String path) 
 * path 不以’/'开头时默认是从此类所在的包下取资源,以’/'开头则是从ClassPath根下获取 
 * 其只是通过path构造一个绝对路径,最终还是由ClassLoader获取资源 
 *  
 * 2. Class.getClassLoader.getResourceAsStream(String path) 
 * 默认则是从ClassPath根下获取,path不能以’/'开头,最终是由ClassLoader获取资源 
 *  
 * @author wp 
 *  
 */  
 
public class SAXPersonServiceTest extends AndroidTestCase {  
    private static final String TAG = "SAXPersonServiceTest";  
 
    public void testReadXml() throws Exception {  
        InputStream inStream = SAXPersonServiceTest.class.getClassLoader()  
                .getResourceAsStream("wp.xml");  
        List<Person> persons = SAXPersonService.readXml(inStream);  
        for (Person person : persons) {  
            Log.i(TAG, person.toString());  
        }  
    }  
}

XMLActivity

Java代码

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
package org.wp.xml;  
 
import java.io.InputStream;  
import java.util.List;  
import org.wp.domain.Person;  
import org.wp.service.SAXPersonService;  
import android.app.Activity;  
import android.os.Bundle;  
import android.util.Log;  
import android.widget.TextView;  
import android.widget.Toast;  
 
public class XMLActivity extends Activity {  
 
    private static final String TAG = "XMLActivity";  
    private TextView resultView;  
 
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        resultView = (TextView) findViewById(R.id.result);  
        InputStream inStream = XMLActivity.class.getClassLoader()  
                .getResourceAsStream("wp.xml");  
        try {  
            List<Person> persons = SAXPersonService.readXml(inStream);  
            StringBuilder sb = new StringBuilder();  
            for (Person person : persons) {  
                sb.append(person.toString()).append("\n");  
            }  
            resultView.setText(sb.toString());  
        } catch (Exception e) {  
            Log.e(TAG, e.toString());  
            Toast.makeText(this, "xml解析失败", 1).show();  
        }  
    }  
}

main.

代码
1
2
3
4
5
6
7
8
9
10
11
12
 version="1.0" encoding="utf-8"?>  
 xmlns:android=""  
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    >  
        
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content"   
        android:id="@+id/result"  
    />  
>

strings.

代码
1
2
3
4
5
 version="1.0" encoding="utf-8"?>  
>  
     name="hello">Hello World, XMLActivity!>  
     name="app_name">xml解析器>  
>

AndroidManifest.

代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 version="1.0" encoding="utf-8"?>  
 xmlns:android=""  
    package="org.wp.xml" android:versionCode="1" android:versionName="1.0">  
     android:icon="@drawable/icon" android:label="@string/app_name">  
         android:name="android.test.runner" />  
         android:name=".XMLActivity" android:label="@string/app_name">  
            >  
                 android:name="android.intent.action.MAIN" />  
                 android:name="android.intent.category.LAUNCHER" />  
            >  
        >  
    >  
     android:minSdkVersion="7" />  
     android:name="android.test.InstrumentationTestRunner"  
        android:targetPackage="org.wp.xml" android:label="Tests for My App" />  
>
阅读(591) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-12-31 19:03:57

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com