android的读取xml配置文件,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

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");

//

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

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值