08-06
21
java通过DOM修改xml文件
作者:Java伴侣 日期:2008-06-21
package updatexml;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
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;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
public class UpdateXml {
public void update(Document doc, String name , String score){
NodeList nl=doc.getElementsByTagName("name");
for(int i=0;i<nl.getLength(); i++){
Element e=(Element) nl.item(i);
if(e.getFirstChild().getNodeValue().equals(name)){
Element eScore=(Element) e.getNextSibling().getNextSibling().getNextSibling().getNextSibling();
System.out.println(eScore.getFirstChild().getNodeValue());
eScore.getFirstChild().setNodeValue(score);
System.out.println(eScore.getFirstChild().getNodeValue());
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
UpdateXml u=new UpdateXml();
File f=new File("d:\\jcode\\student.xml");
try {
DocumentBuilderFactory dFac=DocumentBuilderFactory.newInstance();
DocumentBuilder dBud=dFac.newDocumentBuilder();
Document doc=dBud.parse(f);
u.update(doc, "笑林", "33");
//更新文件
TransformerFactory tfFac=TransformerFactory.newInstance();
Transformer tf=tfFac.newTransformer();
StreamResult result=new StreamResult(f);
DOMSource source=new DOMSource(doc);
tf.setOutputProperty(OutputKeys.VERSION, "1.0");
tf.setOutputProperty(OutputKeys.ENCODING, "gb2312");
tf.setOutputProperty(OutputKeys.INDENT, "yes");
tf.transform(source, result);
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
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;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
public class UpdateXml {
public void update(Document doc, String name , String score){
NodeList nl=doc.getElementsByTagName("name");
for(int i=0;i<nl.getLength(); i++){
Element e=(Element) nl.item(i);
if(e.getFirstChild().getNodeValue().equals(name)){
Element eScore=(Element) e.getNextSibling().getNextSibling().getNextSibling().getNextSibling();
System.out.println(eScore.getFirstChild().getNodeValue());
eScore.getFirstChild().setNodeValue(score);
System.out.println(eScore.getFirstChild().getNodeValue());
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
UpdateXml u=new UpdateXml();
File f=new File("d:\\jcode\\student.xml");
try {
DocumentBuilderFactory dFac=DocumentBuilderFactory.newInstance();
DocumentBuilder dBud=dFac.newDocumentBuilder();
Document doc=dBud.parse(f);
u.update(doc, "笑林", "33");
//更新文件
TransformerFactory tfFac=TransformerFactory.newInstance();
Transformer tf=tfFac.newTransformer();
StreamResult result=new StreamResult(f);
DOMSource source=new DOMSource(doc);
tf.setOutputProperty(OutputKeys.VERSION, "1.0");
tf.setOutputProperty(OutputKeys.ENCODING, "gb2312");
tf.setOutputProperty(OutputKeys.INDENT, "yes");
tf.transform(source, result);
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
评论: 1 | 引用: 0 | 查看次数: 2277
发表评论
<number/>