JAVA操作XML的完整例子——W3C DOM篇
2008-03-03 22:23
507 查看
导读:
Java操作XML学习笔记(二)2006-06-19 fatcatman
struts开发实践—读写xml实例(上)2004-04-13 smilelily
Java与XML(二)用java编写xml的读写程序2005-01-17 delva
dom解析xml2004-11-01 zhaoqiubo
Javascript 操作XML类.2005-10-22 dxmdxs
更多内容...
这是一个用JAVA W3C DOM 进行XML的的例子,包含了查询、增加、修改、删除、保存的基本操作。较完整的描述了一个XML的整个操作流程。适合刚入门JAVA XML操作的朋友参考和学习。
假设有XML文件:test1.xml
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="UTF-8"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<books>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<book>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<name>哈里波特name>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<price>10price>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<memo>这是一本很好看的书。memo>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
book>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<book id="B02">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<name>三国演义name>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<price>10price>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<memo>四大名著之一。memo>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
book>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<book id="B03">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<name>水浒name>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<price>6price>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<memo>四大名著之一。memo>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
book>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<book id="B04">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<name>红楼name>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<price>5price>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<memo>四大名著之一。memo>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
book>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
books>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
下面是为Test.java
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.File;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.FileNotFoundException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.FileOutputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.IOException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.w3c.dom.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.xml.sax.SAXException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.xml.parsers.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.xml.transform.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.xml.transform.dom.DOMSource;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.xml.transform.stream.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.xml.xpath.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Test ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void main(String[] args) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Element theBook=null, theElem=null, root=null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
factory.setIgnoringElementContentWhitespace(true);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DocumentBuilder db=factory.newDocumentBuilder();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Document xmldoc=db.parse(new File("Test1.xml"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
root=xmldoc.getDocumentElement();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//--- 新建一本书开始 ----
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
theBook=xmldoc.createElement("book");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
theElem=xmldoc.createElement("name");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
theElem.setTextContent("新书");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
theBook.appendChild(theElem);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
theElem=xmldoc.createElement("price");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
theElem.setTextContent("20");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
theBook.appendChild(theElem);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
theElem=xmldoc.createElement("memo");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
theElem.setTextContent("新书的更好看。");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
theBook.appendChild(theElem);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
root.appendChild(theBook);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("--- 新建一本书开始 ----");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
output(xmldoc);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//--- 新建一本书完成 ----
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//--- 下面对《哈里波特》做一些修改。 ----
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//--- 查询找《哈里波特》----
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
theBook=(Element) selectSingleNode("/books/book[name='哈里波特']", root);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("--- 查询找《哈里波特》 ----");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
output(theBook);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//--- 此时修改这本书的价格 -----
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
theBook.getElementsByTagName("price").item(0).setTextContent("15");//getElementsByTagName返回的是NodeList,所以要跟上item(0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("--- 此时修改这本书的价格 ----");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
output(theBook);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//--- 另外还想加一个属性id,值为B01 ----
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
theBook.setAttribute("id", "B01");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("--- 另外还想加一个属性id,值为B01 ----");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
output(theBook);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//--- 对《哈里波特》修改完成。 ----
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//--- 要用id属性删除《三国演义》这本书 ----
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
theBook=(Element) selectSingleNode("/books/book[@id='B02']", root);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("--- 要用id属性删除《三国演义》这本书 ----");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
output(theBook);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
theBook.getParentNode().removeChild(theBook);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("--- 删除后的XML ----");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
output(xmldoc);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//--- 再将所有价格低于10的书删除 ----
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
NodeList someBooks=selectNodes("/books/book[price<10]", root);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("--- 再将所有价格低于10的书删除 ---");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("--- 符合条件的书有 "+someBooks.getLength()+"本。 ---");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(int i=0;i<someBooks.getLength();i++) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
someBooks.item(i).getParentNode().removeChild(someBooks.item(i));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
output(xmldoc);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
saveXml("Test1_Edited.xml", xmldoc);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (ParserConfigurationException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (SAXException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (IOException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void output(Node node) ...{//将node的XML字符串输出到控制台
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TransformerFactory transFactory=TransformerFactory.newInstance();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Transformer transformer = transFactory.newTransformer();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
transformer.setOutputProperty("encoding", "gb2312");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
transformer.setOutputProperty("indent", "yes");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DOMSource source=new DOMSource();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
source.setNode(node);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
StreamResult result=new StreamResult();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
result.setOutputStream(System.out);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
transformer.transform(source, result);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (TransformerConfigurationException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (TransformerException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static Node selectSingleNode(String express, Object source) ...{//查找节点,并返回第一个符合条件节点
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Node result=null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
XPathFactory xpathFactory=XPathFactory.newInstance();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
XPath xpath=xpathFactory.newXPath();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
result=(Node) xpath.evaluate(express, source, XPathConstants.NODE);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (XPathExpressionException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return result;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static NodeList selectNodes(String express, Object source) ...{//查找节点,返回符合条件的节点集。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
NodeList result=null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
XPathFactory xpathFactory=XPathFactory.newInstance();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
XPath xpath=xpathFactory.newXPath();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
result=(NodeList) xpath.evaluate(express, source, XPathConstants.NODESET);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (XPathExpressionException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return result;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void saveXml(String fileName, Document doc) ...{//将Document输出到文件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TransformerFactory transFactory=TransformerFactory.newInstance();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Transformer transformer = transFactory.newTransformer();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
transformer.setOutputProperty("indent", "yes");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DOMSource source=new DOMSource();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
source.setNode(doc);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
StreamResult result=new StreamResult();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
result.setOutputStream(new FileOutputStream(fileName));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
transformer.transform(source, result);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (TransformerConfigurationException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (TransformerException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (FileNotFoundException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
本文转自
http://blog.csdn.net/cds27/archive/2008/03/02/2139110.aspx
Java操作XML学习笔记(二)2006-06-19 fatcatman
struts开发实践—读写xml实例(上)2004-04-13 smilelily
Java与XML(二)用java编写xml的读写程序2005-01-17 delva
dom解析xml2004-11-01 zhaoqiubo
Javascript 操作XML类.2005-10-22 dxmdxs
更多内容...
这是一个用JAVA W3C DOM 进行XML的的例子,包含了查询、增加、修改、删除、保存的基本操作。较完整的描述了一个XML的整个操作流程。适合刚入门JAVA XML操作的朋友参考和学习。
假设有XML文件:test1.xml
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="UTF-8"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<books>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<book>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<name>哈里波特name>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<price>10price>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<memo>这是一本很好看的书。memo>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
book>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<book id="B02">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<name>三国演义name>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<price>10price>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<memo>四大名著之一。memo>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
book>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<book id="B03">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<name>水浒name>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<price>6price>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<memo>四大名著之一。memo>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
book>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<book id="B04">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<name>红楼name>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<price>5price>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<memo>四大名著之一。memo>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
book>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
books>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
下面是为Test.java
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.File;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.FileNotFoundException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.FileOutputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.IOException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.w3c.dom.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.xml.sax.SAXException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.xml.parsers.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.xml.transform.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.xml.transform.dom.DOMSource;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.xml.transform.stream.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.xml.xpath.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Test ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void main(String[] args) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Element theBook=null, theElem=null, root=null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
factory.setIgnoringElementContentWhitespace(true);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DocumentBuilder db=factory.newDocumentBuilder();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Document xmldoc=db.parse(new File("Test1.xml"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
root=xmldoc.getDocumentElement();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//--- 新建一本书开始 ----
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
theBook=xmldoc.createElement("book");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
theElem=xmldoc.createElement("name");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
theElem.setTextContent("新书");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
theBook.appendChild(theElem);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
theElem=xmldoc.createElement("price");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
theElem.setTextContent("20");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
theBook.appendChild(theElem);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
theElem=xmldoc.createElement("memo");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
theElem.setTextContent("新书的更好看。");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
theBook.appendChild(theElem);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
root.appendChild(theBook);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("--- 新建一本书开始 ----");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
output(xmldoc);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//--- 新建一本书完成 ----
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//--- 下面对《哈里波特》做一些修改。 ----
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//--- 查询找《哈里波特》----
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
theBook=(Element) selectSingleNode("/books/book[name='哈里波特']", root);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("--- 查询找《哈里波特》 ----");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
output(theBook);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//--- 此时修改这本书的价格 -----
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
theBook.getElementsByTagName("price").item(0).setTextContent("15");//getElementsByTagName返回的是NodeList,所以要跟上item(0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("--- 此时修改这本书的价格 ----");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
output(theBook);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//--- 另外还想加一个属性id,值为B01 ----
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
theBook.setAttribute("id", "B01");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("--- 另外还想加一个属性id,值为B01 ----");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
output(theBook);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//--- 对《哈里波特》修改完成。 ----
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//--- 要用id属性删除《三国演义》这本书 ----
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
theBook=(Element) selectSingleNode("/books/book[@id='B02']", root);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("--- 要用id属性删除《三国演义》这本书 ----");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
output(theBook);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
theBook.getParentNode().removeChild(theBook);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("--- 删除后的XML ----");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
output(xmldoc);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//--- 再将所有价格低于10的书删除 ----
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
NodeList someBooks=selectNodes("/books/book[price<10]", root);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("--- 再将所有价格低于10的书删除 ---");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("--- 符合条件的书有 "+someBooks.getLength()+"本。 ---");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(int i=0;i<someBooks.getLength();i++) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
someBooks.item(i).getParentNode().removeChild(someBooks.item(i));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
output(xmldoc);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
saveXml("Test1_Edited.xml", xmldoc);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (ParserConfigurationException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (SAXException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (IOException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void output(Node node) ...{//将node的XML字符串输出到控制台
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TransformerFactory transFactory=TransformerFactory.newInstance();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Transformer transformer = transFactory.newTransformer();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
transformer.setOutputProperty("encoding", "gb2312");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
transformer.setOutputProperty("indent", "yes");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DOMSource source=new DOMSource();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
source.setNode(node);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
StreamResult result=new StreamResult();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
result.setOutputStream(System.out);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
transformer.transform(source, result);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (TransformerConfigurationException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (TransformerException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static Node selectSingleNode(String express, Object source) ...{//查找节点,并返回第一个符合条件节点
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Node result=null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
XPathFactory xpathFactory=XPathFactory.newInstance();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
XPath xpath=xpathFactory.newXPath();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
result=(Node) xpath.evaluate(express, source, XPathConstants.NODE);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (XPathExpressionException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return result;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static NodeList selectNodes(String express, Object source) ...{//查找节点,返回符合条件的节点集。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
NodeList result=null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
XPathFactory xpathFactory=XPathFactory.newInstance();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
XPath xpath=xpathFactory.newXPath();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
result=(NodeList) xpath.evaluate(express, source, XPathConstants.NODESET);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (XPathExpressionException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return result;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void saveXml(String fileName, Document doc) ...{//将Document输出到文件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TransformerFactory transFactory=TransformerFactory.newInstance();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Transformer transformer = transFactory.newTransformer();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
transformer.setOutputProperty("indent", "yes");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DOMSource source=new DOMSource();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
source.setNode(doc);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
StreamResult result=new StreamResult();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
result.setOutputStream(new FileOutputStream(fileName));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
transformer.transform(source, result);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (TransformerConfigurationException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (TransformerException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (FileNotFoundException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
本文转自
http://blog.csdn.net/cds27/archive/2008/03/02/2139110.aspx
相关文章推荐
- JAVA操作XML的完整例子——W3C DOM篇
- JAVA操作XML的完整例子——W3C DOM篇
- JAVA操作XML的完整例子——W3C DOM篇
- JAVA操作XML的完整例子——W3C DOM篇
- JAVA操作XML的完整例子——W3C DOM篇
- JAVA操作XML的完整例子――W3C DOM篇
- JAVA操作XML的完整例子——W3C DOM篇
- JAVA操作XML的完整例子——W3C DOM篇
- JAVA操作XML的完整例子——W3C DOM篇 转帖
- JAVA操作XML的完整例子——W3C DOM篇
- JAVA操作XML的完整例子——W3C DOM篇
- JAVA操作XML的完整例子——W3C DOM篇
- JAVA操作XML的完整例子
- JAVA操作XML的完整例子
- JAVA操作XML的完整例子
- JAVA操作XML的完整例子(查询、增加、修改、删除)
- C#操作XML的完整例子——XmlDocument篇
- C#操作XML的完整例子——XmlDocument篇
- C#操作XML的完整例子——XmlDocument篇
- C#操作XML的完整例子——XmlDocument篇