android解析xml文件方法之一-----DOM

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.Log;
import com.analysisxml.ych.analysisxml.entity.Hello;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
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;
public class Dom {
    public Hello domToxml(InputStream is) throws ParserConfigurationException, IOException, SAXException {

        //初始化
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        //获得Document对象
Document document = builder.parse(is);
        //获得Hello的list
NodeList helloList = document.getElementsByTagName("dict");
        //遍历Hello标签
NodeList nodeList = helloList.item(0).getChildNodes();
        Hello hello = new Hello();
        List<String> psList = new ArrayList<>();
        List<String> pronList = new ArrayList<>();
        List<String> posList = new ArrayList<>();
        List<String> acceptationList = new ArrayList<>();
        for (int i = 0; i < nodeList.getLength(); i++) {
            if ("key".equals(nodeList.item(i).getNodeName())) {
                hello.setKey(nodeList.item(i).getTextContent());
            }
            if ("ps".equals(nodeList.item(i).getNodeName())) {
                psList.add(nodeList.item(i).getTextContent());
            }
            if ("pron".equals(nodeList.item(i).getNodeName())) {
                pronList.add(nodeList.item(i).getTextContent());
            }
            if ("pos".equals(nodeList.item(i).getNodeName())) {
                posList.add(nodeList.item(i).getTextContent());
            }
            if ("acceptation".equals(nodeList.item(i).getNodeName())) {
                acceptationList.add(nodeList.item(i).getTextContent());
            }
            Log.e("tag", nodeList.item(i).getTextContent());
        }
        hello.setPs(psList);
        hello.setPron(pronList);
        hello.setPos(posList);
        hello.setAcceptation(acceptationList);
        return hello;


    }

}
 

入口 :

try {
    Hello hello=new Dom().domToxml(getResources().getAssets().open("Hello.xml"));
    Log.e("tag1",hello.toString());
} catch (ParserConfigurationException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} catch (SAXException 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,如有不对的地方请见谅和指正。

猜你喜欢

转载自www.cnblogs.com/2484ydv/p/9008095.html