android中XMl文件的读取

废话不多说了直接上代码:

新建一个Xml文件用于读取:

test.xml

<Languages cat="1">
    <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>
后台:

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

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.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;


public class MainActivity extends ActionBarActivity {

    private TextView tv1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv1= (TextView) findViewById(R.id.tv1);

        try {

            //读取与解析Xml文件:
            DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();
            DocumentBuilder builder=builderFactory.newDocumentBuilder();
            Document document=builder.parse(getAssets().open("test.xml")); //记住导的是import org.w3c.dom.Document;不要导错
            Element element=document.getDocumentElement();
            NodeList  list=element.getElementsByTagName("lan");//过滤读取lan所对应的内容
            for (int i=0;i<list.getLength();i++){
                Element lan= (Element) list.item(i);
                tv1.append(lan.getAttribute("id")+"\n");
                tv1.append(lan.getElementsByTagName("name").item(0).getTextContent()+"\n");
                tv1.append(lan.getElementsByTagName("ide").item(0).getTextContent()+"\n");

            }
                DocumentBuilderFactory builderFactory1=DocumentBuilderFactory.newInstance();
                DocumentBuilder builder1=builderFactory1.newDocumentBuilder();
                Document newxml=builder1.newDocument();
                Element languages=newxml.createElement("Languages");//根元素
                languages.setAttribute("cat","it");  //设置属性

                       //生成与输出XMl数据
                Element lan1=newxml.createElement("lan");
 //                <lan id="1">
//                <name>java</name>
//                <ide>Eclipse</ide>
//                </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);

//                <lan id="2">
//                <name>Swift</name>
//                <ide>Xcode</ide>
//                </lan>
                Element lan2=newxml.createElement("lan");
                lan2.setAttribute("id", "1");
                Element name2=newxml.createElement("name");
                name2.setTextContent("Swift");
                Element ide2=newxml.createElement("ide");
                ide2.setTextContent("Xcode");
                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));
                sw.toString();//输出查看结果
                sw.close();
                tv1.append("\n输出结果:"+sw.toString());

        } catch (IOException e) {
            e.printStackTrace();
            Log.i("e1",e.toString());
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
            Log.i("e2", e.toString());
        } catch (SAXException e) {
            e.printStackTrace();
            Log.i("e3", e.toString());
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }
    }
}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。