One of the methods of android parsing xml file-----DOM

Hello.xml file 

 

<dict num="219" id="219" name="219">
<key>hello</key>
<ps> solution </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>Hello, hello; hello, hello; say hello; say hello;</acceptation>
<pos>n.</pos>
<acceptation>"Hello" or greeting;</acceptation>
<pos>vi.</pos>
<acceptation>Shout "hello";</acceptation>
<sent>
<orig>
This document contains Hello application components of each document summary of the contents.
</orig>
<trans>This file contains a summary of the contents of each of the files that make up the Hello application.</trans>
</sent>
<sent>
<orig>
In the following example, CL produces a combined source and machine - code listing called HELLO. COD.
</orig>
<trans>In the example below, CL will generate a combined source and machine code manifest file named HELLO.COD.</trans>
</sent>
<sent>
<orig>Hello! Hello! Hello! Hello! Hel - lo!</orig>
<trans>Hello! Hello! Hello! Hello! Hello!</trans>
</sent>
<sent>
<orig>Hello! Hello! Hello! Hello ! I'm glad to meet you.</orig>
<trans>Hello! Hello! Hello! Hello! Nice to meet you.</trans>
</sent>
<sent>
<orig>Hello Marie. Hello Berlioz. Hello Toulouse.</orig>
<trans>Hello Marie, hello Barrio, hello Toulouse.</trans>
</sent>
</dict>

Hello entity

 

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.acceptance = acceptance;
    }

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

DOM parsing class

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();
        //Get the Document object
 Document document = builder.parse(is);
         //Get the list
 NodeList of Hello helloList = document.getElementsByTagName("dict");
         // Traverse the Hello tag
 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;


    }

}
 

Entrance:

 

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 ();
}
 

Put the xml file in the following location:

\

 

Parsing result:

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=[Hello, hello; hello, hello; greetings ; to say hello;, "hello" greeting or greeting;, shout "hello";]'}

 

 

Beginner to parse xml, please forgive me and correct me if I am wrong.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325816763&siteId=291194637