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,如有不对的地方请见谅和指正。

android解析xml文件方法之一-----DOM的更多相关文章

  1. android解析xml文件的方式

    android解析xml文件的方式   作者:东子哥 ,发布于2012-11-26,来源:博客园   在androd手机中处理xml数据时很常见的事情,通常在不同平台传输数据的时候,我们就可能使用xm ...

  2. Android 解析XML文件和生成XML文件

    解析XML文件 public static void initXML(Context context) { //can't create in /data/media/0 because permis ...

  3. Java是如何解析xml文件的(DOM)

    Java解析xml文件 在Java程序中读取xml文件的过程也称为"解析xml文件": 解析的目的: 获取 节点名和节点值 获取 属性名.属性值. 四中解析方式: DOM SAX ...

  4. Android解析XML文件

    XML文件和获取XML值 XML文件样例 <?xml version="1.0" encoding="utf-8"?> <citys> ...

  5. Android解析xml文件-采用DOM,PULL,SAX三种方法解析

    解析如下xml文件 <?xml version="1.0" encoding="UTF-8"?> <persons> <perso ...

  6. [android]解析XML文件的方法有三种:PULL,DOM,SAM

    PULL 的工作原理: XML pull提供了开始元素和结束元素.当某个元素开始时,可以调用parser.nextText从XML文档中提取所有字符数据.当解析到一个文档结束时,自动生成EndDocu ...

  7. 解析XML文件之使用DOM解析器

    在前面的文章中.介绍了使用SAX解析器对XML文件进行解析.SAX解析器的长处就是占用内存小.这篇文章主要介绍使用DOM解析器对XML文件进行解析. DOM解析器的长处可能是理解起来比較的直观,当然, ...

  8. JAVA中使用DOM解析XML文件

    XML是一种方便快捷高效的数据保存传输的格式,在JSON广泛使用之前,XML是服务器和客户端之间数据传输的主要方式.因此,需要使用各种方式,解析服务器传送过来的信息,以供使用者查看. JAVA作为一种 ...

  9. Java中使用DOM4J来生成xml文件和解析xml文件

    一.前言 现在有不少需求,是需要我们解析xml文件中的数据,然后导入到数据库中,当然解析xml文件也有好多种方法,小编觉得还是DOM4J用的最多最广泛也最好理解的吧.小编也是最近需求里遇到了,就来整理 ...

随机推荐

  1. 【一天一道LeetCode】#90. Subsets II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  2. 使用github管理你的代码

    关于为什么使用github,网上已经有很多讨论了.当然选择还有google code, Bitbucket,sourceforge.github有如下优势: 1. github更有利于开源项目的发展 ...

  3. javascript之BOM地址栏对象(Location)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. Oracle 11g Articles

    发现一个比较有意思的网站,http://www.oracle-base.com/articles/11g/articles-11g.php Oracle 11g Articles Oracle Dat ...

  5. 【一天一道LeetCode】#47. Permutations II

    一天一道LeetCode系列 (一)题目 Given a collection of numbers that might contain duplicates, return all possibl ...

  6. wing带你玩转自定义view系列(2) 简单模仿qq未读消息去除效果

    上一篇介绍了贝塞尔曲线的简单应用 仿360内存清理效果 这一篇带来一个  两条贝塞尔曲线的应用 : 仿qq未读消息去除效果. 转载请注明出处:http://blog.csdn.net/wingicho ...

  7. redis简单测试用例(内存不足,可以使用redis)

    Redis本质上是一个Key-Value类型的内存数据库,很像memcached,听说他的性能远高于memcached,所以想自己搞个玩下.看到底有什么好处. 在windows下使用redis首先要 ...

  8. hive基本的操作语句(实例简单易懂,create table XX as select XX)

    hive建表语句DML:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-Cr ...

  9. 制药企业BI系统方案整体设计分享

    制药企业全面预算系统蓝图 全面掌控企业的各种业务活动,及时准确的展现它们的状况与趋势,评估其达成的效果.存在的问题与风险.支持数据的导入,多级上报等多种特色功能,同时通过统一的报表平台实现精细话的权限 ...

  10. struts form表单提交action处理之后没有跳转页面

    作为一个ssm新人,对struts的理解也仅仅是书本上一些简单的示例, 对如下这个form进行提交之后,执行完action,并return 一个字符串,在struts配置文件中配置了对应的jsp页面, ...