解析Xml檔案,以及新建asserts檔案目錄
阿新 • • 發佈:2019-02-20
新建asserts檔案目錄:
選中app目錄右鍵,new > Folder > Asserts Floder,就OK了。
接下來解析xml檔案,還有解析json的,這裡提供一個解析xml的工具類:
在Asserts下新建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>
新建實體Bean類:
package com.cypoem.retrofit.util; import java.util.List; /** * 作者:jiuqunZhang on 2018/7/26 20:04 * 郵箱:[email protected] */ 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.cypoem.retrofit.util; import android.util.Log; 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; /** * 作者:jiuqunZhang on 2018/7/26 20:02 * 郵箱:[email protected] */ 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.d("tag1",hello.toString()); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); }
這行就行了