修改xml文件的值
package com.test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
public class JavaReadXml {
/**
* @param args
*/
// Document可以看作是XML在内存中的一个镜像,那么一旦获取这个Document 就意味着可以通过对
// 内存的操作来实现对XML的操作,首先第一步获取XML相关的Document
private Document doc = null;
public void init(String xmlFile) throws Exception {
// 很明显该类是一个单例,先获取产生DocumentBuilder工厂
// 的工厂,在通过这个工厂产生一个DocumentBuilder,
// DocumentBuilder就是用来产生Document的
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
// 这个Document就是一个XML文件在内存中的镜像
doc = db.parse(new File(xmlFile));
doc.normalize();
}
// 该方法负责把XML文件的内容显示出来
public void viewXML(String xmlFile) throws Exception {
this.init(xmlFile);
// 在xml文件里,只有一个根元素,先把根元素拿出来看看
Element element = doc.getDocumentElement();
System.out.println("根元素为:" + element.getTagName());
NodeList nodeList = doc.getElementsByTagName("person");
System.out.println("book节点链的长度:" + nodeList.getLength());
for(int i = 0;i<nodeList.getLength();i++) {
Node fatherNode = nodeList.item(i);
NodeList childNodes = fatherNode.getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++) {
Node childNode = childNodes.item(j);
// 如果这个节点属于Element ,再进行取值
if (childNode instanceof Element) {
if((childNode.getNodeName().equals("version"))&&(childNode.getFirstChild().getNodeValue()).equals("清华大学出版社")) {
System.out.println("------"+childNode.getFirstChild().getNodeName());
childNode.getFirstChild().setNodeValue("电子工业出版社");
}
System.out.println("子节点名为:" + childNode.getNodeName()
+ "相对应的值为" + childNode.getNodeValue());
}}
writeToXml(doc,"d:\\002.xml");
}
/*Node fatherNode = nodeList.item(0);
System.out.println("父节点为:" + fatherNode.getNodeName());
// 把父节点的属性拿出来
NamedNodeMap attributes = fatherNode.getAttributes();
System.out.println(""+attributes.getLength());
for (int i = 0; i < attributes.getLength(); i++) {
Node attribute = attributes.item(i);
System.out.println("book的属性名为:" + attribute.getNodeName()
+ " 相对应的属性值为:" + attribute.getNodeValue());
}
NodeList childNodes = fatherNode.getChildNodes();
System.out.println(childNodes.getLength());
for (int j = 0; j < childNodes.getLength(); j++) {
Node childNode = childNodes.item(j);
// 如果这个节点属于Element ,再进行取值
if (childNode instanceof Element) {
// System.out.println("子节点名为:"+childNode.getNodeName()+"相对应的值为"+childNode.getFirstChild().getNodeValue());
System.out.println("子节点名为:" + childNode.getNodeName()
+ "相对应的值为" + childNode.getFirstChild().getNodeValue());
}}*/
}
public static boolean doc2XmlFile(Document document,String filename)
{
boolean flag = true;
try
{
/** 将document中的内容写入文件中 */
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
/** 编码 */
//transformer.setOutputProperty(OutputKeys.ENCODING, "GB2312");
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(filename);
transformer.transform(source, result);
}catch(Exception ex)
{
flag = false;
ex.printStackTrace();
}
return flag;
}
public void writeToXml(Document doc,String rptdesign){
try{
OutputStream fileoutputStream = new FileOutputStream(rptdesign);
TransformerFactory tFactory =TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(fileoutputStream);
transformer.transform(source, result);
}
catch(Exception e){
System.out.print("Can't write to file: "+ rptdesign);
return;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
JavaReadXml parse = new JavaReadXml();
// 我的XML文件
try {
parse.viewXML("d:\\001.xml");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<book>
<person>
<first>wang</first>
<last>laohu</last>
<age>25</age>
<version>清华大学出版社</version>
</person>
<person>
<first>li</first>
<last>junjia</last>
<age>24</age>
<version>机械工业出版社</version>
</person>
</book>
总是有错,提示这句有错。transformer.transform(source, result);
Exception in thread "main" java.lang.AbstractMethodError: org.apache.xerces.dom.DeferredDocumentImpl.getXmlStandalone()Z
[解决办法]
dom4j是神器
噢啦啦