读取与解析XML数据
language.xml数据一定要在assets文件中
<?xml version="1.0" encoding="utf-8"?> <Languages> <lan id="1"> <name>Java</name> <ide>eclipse</ide> </lan> <lan id="2"> <name>swift</name> <ide>xcode</ide> </lan> <lan id="3"> <name>C#</name> <ide>visual Studio</ide> </lan> </Languages>MainActivity.java
<span style="font-size:14px;">package com.example.xmltest; import java.io.IOException; import java.io.StringWriter; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { TextView text; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text=(TextView) findViewById(R.id.tv); try { DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance(); DocumentBuilder build=builderFactory.newDocumentBuilder(); /*Document document=build.parse(getAssets().open("language.xml"));//这部分注释的代码是读取assets文件里的xml数据 Element element=document.getDocumentElement(); //获取根元素 NodeList list= element.getElementsByTagName("lan");//获取跟结点 for(int i=0;i<list.getLength();i++){ Element lan= (Element) list.item(i); //获取xml中第一条数据 text.append(lan.getAttribute("id")+"\n"); text.append(lan.getElementsByTagName("name").item(0).getTextContent()+"\n"); text.append(lan.getElementsByTagName("ide").item(0).getTextContent()+"\n"); }*/ //下面这部分创建xmL数据 Document newxml=build.newDocument(); Element languages=newxml.createElement("Languages"); languages.setAttribute("cat","it"); //创建跟结点 Element lan1=newxml.createElement("lan"); lan1.setAttribute("id","1"); Element name1=newxml.createElement("name"); name1.setTextContent("Java"); Element ide1=newxml.createElement("ide"); ide1.setTextContent("Eclipse"); lan1.appendChild(name1); lan1.appendChild(ide1); languages.appendChild(lan1); Element lan2=newxml.createElement("lan"); lan2.setAttribute("id","2"); Element name2=newxml.createElement("name"); name2.setTextContent("Swift"); Element ide2=newxml.createElement("ide"); ide2.setTextContent("C#"); lan2.appendChild(name2); lan2.appendChild(ide2); languages.appendChild(lan2); newxml.appendChild(languages); TransformerFactory transformerFactory=TransformerFactory.newInstance(); Transformer transformer=transformerFactory.newTransformer(); transformer.setOutputProperty("encoding","utf-8"); StringWriter sw=new StringWriter();//创建一个输出流 transformer.transform(new DOMSource(newxml),new StreamResult(sw)); text.setText(sw.toString());//编写完xml后,输出太页面上 } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main,menu); return true; } } </span>activity_main.xml
<span style="font-size:14px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="140dp" android:textSize="20dp" /> </RelativeLayout></span>