Android 解析、修改xml文件
一、DOM解析XML文件
[java]
public static void main(String[] args) {
File file = new File("d:\\WordChronos.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder docbuder;
try {
docbuder = dbf.newDocumentBuilder();
Document doc = docbuder.parse(file);
NodeList nl = doc.getElementsByTagName_r("Set");
for(int i = 0;i < nl.getLength();i ++){
doc.getElementsByTagName_r("Time1").item(0).getFirstChild().setNodeValue("2010-10-30");
String s1 = doc.getElementsByTagName_r("Time1").item(0).getFirstChild().getNodeValue();
String s2 = doc.getElementsByTagName_r("Time2").item(0).getFirstChild().getNodeValue();
String s3 = doc.getElementsByTagName_r("Time3").item(0).getFirstChild().getNodeValue();
String s4 = doc.getElementsByTagName_r("Music1").item(0).getFirstChild().getNodeValue();
System.out.println(s1 + ";" + s2 + ";" + s3 + ";" + s4);
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
二、DOM4J解析XML文件
[java]
SAXReader reader = new SAXReader();
Document document;
try {
document = reader.read(new File("d:\\WordChronos.xml"));
Element root = ((org.dom4j.Document) document).getRootElement();
List<Element> worktimes = root.elements("Set");
for (Element worktime : worktimes) {
worktime.element("Time1").setText("07:00");
worktime.element("Time2").setText("08:00");
worktime.element("Time3").setText("09:00");
worktime.element("Music1").setText("我知道我爱你.mp3");
time1 = worktime.element("Time1").getText();
time2 = worktime.element("Time2").getText();
time3 = worktime.element("Time3").getText();
musicPath1 = worktime.element("Music1").getText();
System.out.println(time1+"\t"+time2+"\t"+time3+"\t"+musicPath1);
}
XMLWriter writer = new XMLWriter(new FileWriter("d:\\WordChronos.xml"));
writer.write(document);
writer.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
三、Android自带的pull解析方式
********************************xml文件格式***************************************
[html]
<?xml version="1.0" encoding="utf-8" ?>
<LunarFeast>
<Feast>
<Date>0101</Date>
<Content>春节</Content>
</Feast>
<Feast>
<Date>0115</Date>
<Content>元宵节</Content>
</Feast>
<Feast>
<Date>0202</Date>
<Content>青龙节</Content>
</Feast>
。。。。。。。
</LunarFeast>
**********************************************************************************
[java]
//解析XML文件
public List<Message> parser2(){
//引用资源中的xml文件,如下方式获取:
//XmlPullParser parser = this.getResources().getXml(R.xml.solarfeastfile);
//引用sdcard里面的xml文件,如下方式获取
XmlPullParser parser = Xml.newPullParser();
InputStream is = new FileInputStream(path);
parser.setInput(is,"utf-8");
try {
int eventType = parser.getEventType();
Message currentMessage = null;
boolean done = false;
while(eventType!= XmlPullParser.END_DOCUMENT && !done){
String name = null;
switch(eventType){
case XmlPullParser.START_DOCUMENT:
message2 = new ArrayList<Message>();
break;
case XmlPullParser.START_TAG:
name = parser.getName();
if(name.equalsIgnoreCase("Feast")){
currentMessage = new Message();
&nbs
补充:移动开发 , Android ,