android中XMl文件的讀取

廢話不多說瞭直接上代碼:

新建一個Xml文件用於讀取:

test.xml

 


    
        java
    Eclipse
    
    
        Swift
        Xcode
    
    
        C#
        Visual Studio
    

後臺:

 

 

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
//                java
//                Eclipse
//                
                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);

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

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。