android解析xml方法之一------PULL

Hello.xml文件 

<dict num="219" id="219" name="219">
<key>hello</key>
<ps>hə'ləʊ</ps>
<pron>
http://res-tts.iciba.com/5/d/4/5d41402abc4b2a76b9719d911017c592.mp3
</pron>
<ps>həˈloʊ</ps>
<pron>
http://res.iciba.com/resource/amp3/1/0/5d/41/5d41402abc4b2a76b9719d911017c592.mp3
</pron>
<pos>int.</pos>
<acceptation>哈喽,喂;你好,您好;表示问候;打招呼;</acceptation>
<pos>n.</pos>
<acceptation>“喂”的招呼声或问候声;</acceptation>
<pos>vi.</pos>
<acceptation>喊“喂”;</acceptation>
<sent>
<orig>
This document contains Hello application components of each document summary of the contents.
</orig>
<trans>此文件包含组成Hello应用程序的每个文件的内容摘要.</trans>
</sent>
<sent>
<orig>
In the following example, CL produces a combined source and machine - code listing called HELLO. COD.
</orig>
<trans>在下面的例子中, CL将产生一个命名为HELLO. COD的源代码与机器代码组合的清单文件.</trans>
</sent>
<sent>
<orig>Hello! Hello! Hello! Hello! Hel - lo!</orig>
<trans>你好! 你好! 你好! 你好! 你好!</trans>
</sent>
<sent>
<orig>Hello! Hello! Hello! Hello ! I'm glad to meet you.</orig>
<trans>你好! 你好! 你好! 你好! 见到你很高兴.</trans>
</sent>
<sent>
<orig>Hello Marie. Hello Berlioz. Hello Toulouse.</orig>
<trans>你好玛丽, 你好柏里欧, 你好图鲁兹.</trans>
</sent>
</dict>

Hello实体

package com.analysisxml.ych.analysisxml.entity;
import java.util.List;
public class Hello {
    private String key;
    private List<String> ps;
    private List<String> pron;
    private List<String> pos;
    private List<String> acceptation;
    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public List<String> getPs() {
        return ps;
    }

    public void setPs(List<String> ps) {
        this.ps = ps;
    }

    public List<String> getPron() {
        return pron;
    }

    public void setPron(List<String> pron) {
        this.pron = pron;
    }

    public List<String> getPos() {
        return pos;
    }

    public void setPos(List<String> pos) {
        this.pos = pos;
    }

    public List<String> getAcceptation() {
        return acceptation;
    }

    public void setAcceptation(List<String> acceptation) {
        this.acceptation = acceptation;
    }

    public String toString(){
        return "Hello{"+"key="+key+'\''
                +",ps="+ps+'\''
                +",pron="+pron+'\''
                +",pos="+pos+'\''
                +"acceptation="+acceptation+'\''+
                "}";
    }
}

DOM解析类

package com.analysisxml.ych.analysisxml.utils;
import android.util.Xml;
import com.analysisxml.ych.analysisxml.entity.Hello;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class Pull {
    public Hello pullToXML(InputStream is) throws XmlPullParserException, IOException {
        Hello hello = new Hello();
        List<String> psList = new ArrayList<>();
        List<String> pronList = new ArrayList<>();
        List<String> posList = new ArrayList<>();
        List<String> acceptationList = new ArrayList<>();
        //实例化Pull解析器
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(is,"UTF-8");
        //读取文件类型
        int type = parser.getEventType();
        //判断文件类型进行读取
        while (type!=XmlPullParser.END_DOCUMENT){
            switch (type){
                case XmlPullParser.START_TAG:
                    if ("dict".equals(parser.getName())){
                    }else  if ("key".equals(parser.getName())){
                        hello.setKey(parser.nextText());
                    }else if ("ps".equals(parser.getName())){
                        psList.add(parser.nextText());
                    }else if ("pron".equals(parser.getName())){
                        pronList.add(parser.nextText());
                    }else if ("pos".equals(parser.getName())){
                        posList.add(parser.nextText());
                    }else if ("acceptation".equals(parser.getName())){
                        acceptationList.add(parser.nextText());
                    }
                    break;
                case XmlPullParser.END_TAG:
                    if ("dict".equals(parser.getName())){
                        hello.setPs(psList);
                        hello.setPron(pronList);
                        hello.setPos(posList);
                        hello.setAcceptation(acceptationList);
                    }
                    break;
            }
            //继续往下读标签
            type=parser.next();
        }
        return hello;

    }
}

入口 :

try {
   Hello hello= new Pull().pullToXML(getResources().getAssets().open("Hello.xml"));
   Log.e("pull",hello.toString());
} catch (XmlPullParserException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
 
 

将xml文件放在如下位置:

\


解析结果:

Hello{key=hello',ps=[hə'ləʊ, həˈloʊ]',pron=[
        http://res-tts.iciba.com/5/d/4/5d41402abc4b2a76b9719d911017c592.mp3
        , 
        http://res.iciba.com/resource/amp3/1/0/5d/41/5d41402abc4b2a76b9719d911017c592.mp3
        ]',pos=[int., n., vi.]'acceptation=[哈喽,喂;你好,您好;表示问候;打招呼;, “喂”的招呼声或问候声;, 喊“喂”;]'}



初学解析xml,如有不对的地方请见谅和指正。

猜你喜欢

转载自blog.csdn.net/yangchanghong1995/article/details/80213555
今日推荐