java操作xml(sax,dom,jdom,dom4j)增删改查

1:DOM4J性能最好,连Sun的JAXM也在用DOM4J.目前许多开源项目中大量采用DOM4J,例如大名鼎鼎的Hibernate也用DOM4J来读取XML配置文件。如果不考虑可移植性,那就采用DOM4J.

2:JDOM和DOM在性能测试时表现不佳,在测试10M文档时内存溢出。在小文档情况下还值得考虑使用DOM和JDOM.虽然JDOM的开发者已经说明他们期望在正式发行版前专注性能问题,但是从性能观点来看,它确实没有值得推荐之处。另外,DOM仍是一个非常好的选择。DOM实现广泛应用于多种编程语言。它还是许多其它与XML相关的标准的基础,因为它正式获得W3C推荐(与基于非标准的Java模型相对),所以在某些类型的项目中可能也需要它(如在JavaScript中使用DOM)。

3:SAX表现较好,这要依赖于它特定的解析方式-事件驱动。一个SAX检测即将到来的XML流,但并没有载入到内存(当然当XML流被读入时,会有部分文档暂时隐藏在内存中)。

接下来我来展示以下用这四种方式处理xml的一个简单的例子,希望能给大家一些帮助。

1:要操作的xml文件:student.xml

<?xmlversion="1.0"encoding="UTF-8"?>

<students>

<student>

<name>TigerLee</name>

<age>26</age>

<sex>男</sex>

<address>北京海淀</address>

</student>

<student>

<name>Snow</name>

<age>24</age>

<sex>女</sex>

<address>山东</address>

</student>

</students>

2:处理xml的java类HandleXML.java

packagecom.tigerlee.xml;

importjava.io.File;

importjava.io.FileWriter;

importjava.io.Writer;

importjava.util.Iterator;

importjava.util.LinkedHashMap;

importjava.util.List;

importjava.util.Map;

importjava.util.Set;

importjava.util.Stack;

importjavax.xml.parsers.DocumentBuilder;

importjavax.xml.parsers.DocumentBuilderFactory;

importjavax.xml.transform.Transformer;

importjavax.xml.transform.TransformerFactory;

importjavax.xml.transform.dom.DOMSource;

importjavax.xml.transform.stream.StreamResult;

importorg.dom4j.Element;

importorg.dom4j.io.OutputFormat;

importorg.dom4j.io.SAXReader;

importorg.dom4j.io.XMLWriter;

importorg.w3c.dom.Document;

importorg.w3c.dom.Node;

importorg.w3c.dom.NodeList;

importorg.xml.sax.Attributes;

importorg.xml.sax.SAXException;

importorg.xml.sax.helpers.DefaultHandler;

/**

*

*在XML文档中实现增、删、改、查

*@authorTigerLee

*

*/

publicclassHandleXML{

/**

*采用DOM方式

*@authorTigerLee

*

*/

publicclassDOMForXml{

/**

*获得doc对象

*@paramfileName

*@return

*/

publicDocumentgetDocument(StringfileName){

Documentdocument=null;

try{

DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();

DocumentBuilderbuilder=factory.newDocumentBuilder();

document=builder.parse(newFile(fileName));

}catch(Exceptione){

e.printStackTrace();

}

returndocument;

}

/**

*将改动持久到文件

*@paramdoc

*@paramdistFileName

*/

publicvoidmodifyFile(Documentdoc,StringdistFileName){

try{

TransformerFactorytf=TransformerFactory.newInstance();

Transformertfer=tf.newTransformer();

DOMSourcedsource=newDOMSource(doc);

StreamResultsr=newStreamResult(newFile("student.xml"));

tfer.transform(dsource,sr);

}catch(Exceptione){

e.printStackTrace();

}

}

/**

*解析

*@paramfileName

*/

publicvoidpaseXml(StringfileName){

Documentdocument=getDocument(fileName);

NodeListnodeList=document.getElementsByTagName("student");

for(inti=0;i<nodeList.getLength();i++){

StringBuildersb=newStringBuilder();

sb.append("姓名:"+document.getElementsByTagName("name").item(i).getFirstChild().getNodeValue());

sb.append(",");

sb.append("年龄:"+document.getElementsByTagName("age").item(i).getFirstChild().getNodeValue());

sb.append(",");

sb.append("性别:"+document.getElementsByTagName("sex").item(i).getFirstChild().getNodeValue());

sb.append(",");

sb.append("地址:"+document.getElementsByTagName("address").item(i).getFirstChild().getNodeValue());

System.out.println(sb.toString());

}

}

/**

*创建一个新的学生

*@paramname

*@paramage

*@paramsex

*@paramaddress

*/

publicvoidaddNewNode(Stringname,Stringage,Stringsex,Stringaddress){

try{

Documentdocument=getDocument("student.xml");

NodeListnodeList=document.getElementsByTagName("students");

//创建新的节点

NodestudentNode=document.createElement("student");

NodenameNode=document.createElement("name");

nameNode.appendChild(document.createTextNode(name));

NodeageNode=document.createElement("age");

ageNode.appendChild(document.createTextNode(age));

NodesexNode=document.createElement("sex");

sexNode.appendChild(document.createTextNode(sex));

NodeaddressNode=document.createElement("address");

addressNode.appendChild(document.createTextNode(address));

//添加节点

studentNode.appendChild(nameNode);

studentNode.appendChild(ageNode);

studentNode.appendChild(sexNode);

studentNode.appendChild(addressNode);

nodeList.item(0).appendChild(studentNode);

//此时真正的处理将新数据添加到文件中(磁盘)

TransformerFactorytf=TransformerFactory.newInstance();

Transformertfer=tf.newTransformer();

DOMSourcedsource=newDOMSource(document);

StreamResultsr=newStreamResult(newFile("student.xml"));

tfer.transform(dsource,sr);

}catch(Exceptione){

e.printStackTrace();

}

paseXml("student.xml");

}

/**

*删除一个节点

*@paramname

*/

publicvoiddeleteNode(Stringname){

Documentdocument=getDocument("student.xml");

NodeListnodeList=document.getElementsByTagName("name");

for(inti=0;i<nodeList.getLength();i++){

Stringvalue=nodeList.item(i).getFirstChild().getTextContent();

if(name!=null&&name.equalsIgnoreCase(value)){

NodeparentNode=nodeList.item(i).getParentNode();

document.getFirstChild().removeChild(parentNode);

}

}

modifyFile(document,"student.xml");

}

/**

*根据name修改某个节点的内容

*@paramname

*/

publicvoidupdateNode(Stringname){

Documentdocument=getDocument("student.xml");

NodeListnodeList=document.getElementsByTagName("name");

for(inti=0;i<nodeList.getLength();i++){

Stringvalue=nodeList.item(i).getFirstChild().getTextContent();

if(name!=null&&name.equalsIgnoreCase(value)){

NodeparentNode=nodeList.item(i).getParentNode();

NodeListnl=parentNode.getChildNodes();

for(intj=0;j<nl.getLength();j++){

StringmodifyNode=nl.item(j).getNodeName();

if(modifyNode.equalsIgnoreCase("age")){

nl.item(j).getFirstChild().setTextContent("25");

}

}

}

}

modifyFile(document,"student.xml");

}

}

/**

*采用SAX方式

*@authorTigerLee

*

*/

publicclassSAXForXmlextendsDefaultHandler{

privateStacktags=newStack();

privateMapcontents=newLinkedHashMap();

publicintcount=0;

@Override

publicvoidcharacters(char[]ch,intstart,intlength)

throwsSAXException{

Stringtag=(String)tags.peek();

if("name".equals(tag)){

Stringname=newString(ch,start,length);

contents.put("name"+count,name);

}

if("age".equals(tag)){

contents.put("age"+count,newString(ch,start,length));

}

if("sex".equals(tag)){

contents.put("sex"+count,newString(ch,start,length));

}

if("address".equals(tag)){

contents.put("address"+count,newString(ch,start,length));

}

}

@Override

publicvoidstartElement(Stringuri,StringlocalName,StringqName,

Attributesattributes)throwsSAXException{

if("student".equals(qName)){

count++;

}

tags.push(qName);

}

@Override

publicvoidendElement(Stringuri,StringlocalName,StringqName)

throwsSAXException{

tags.pop();

}

publicMapgetContents(){

returncontents;

}

}

/**

*采用JDOM方式

*@authorTigerLee

*

*/

publicclassJDOMForXml{

}

/**

*采用DOM4J方式

*@authorTigerLee

*

*/

publicclassDOM4JForXml{

/**

*获取doc对象(org.dom4j.Document)

*@paramfileName

*@return

*/

publicorg.dom4j.DocumentgetDocument(StringfileName){

SAXReadersr=newSAXReader();

org.dom4j.Documentdoc=null;

try{

doc=sr.read(newFile(fileName));

}catch(Exceptione){

e.printStackTrace();

}

returndoc;

}

/**

*将文件保存到硬盘

*@paramdoc

*@paramfileName

*/

publicvoidwriteToFile(org.dom4j.Documentdoc,StringfileName){

try{

Writerwriter=newFileWriter(fileName);

OutputFormatformat=OutputFormat.createPrettyPrint();

format.setEncoding("UTF-8");

XMLWriterxmlWriter=newXMLWriter(writer,format);

xmlWriter.write(doc);

xmlWriter.close();

System.out.println("文件已经钝化!");

}catch(Exceptione){

e.printStackTrace();

}

}

/**

*遍历xml文件输出节点值

*

*@paramfileName

*/

publicvoidreadXml(StringfileName){

org.dom4j.Documentdoc=getDocument(fileName);

ListnameList=doc.selectNodes("/students/student/name");

ListageList=doc.selectNodes("/students/student/age");

ListsexList=doc.selectNodes("/students/student/sex");

ListaddressList=doc.selectNodes("/students/student/address");

for(inti=0;i<nameList.size();i++){

StringBuildersb=newStringBuilder();

sb.append("name="+((Element)nameList.get(i)).getTextTrim());

sb.append(",age="+((Element)ageList.get(i)).getTextTrim());

sb.append(",sex="+((Element)sexList.get(i)).getTextTrim());

sb.append(",address="+((Element)addressList.get(i)).getTextTrim());

System.out.println(sb.toString());

}

}

/**

*根据姓名修改一个学生的信息

*@paramname

*@paramnewValue

*@paramfileName

*/

publicvoidupdateXml(Stringname,MapnewValue,StringfileName){

org.dom4j.Documentdoc=getDocument(fileName);

ListnameList=doc.selectNodes("/students/student/name");

Iteratoriterator=nameList.iterator();

while(iterator.hasNext()){

Elementelement=(Element)iterator.next();

if(name!=null&&name.equals(element.getText())){

Elementpe=element.getParent();

ListchildList=pe.elements();

for(inti=0;i<childList.size();i++){

IteratorvalueSet=newValue.entrySet().iterator();

while(valueSet.hasNext()){

Map.Entryentry=(Map.Entry)valueSet.next();

StringnodeName=((Element)childList.get(i)).getName();

Stringkey=entry.getKey().toString();

if(key!=null&&key.equals(nodeName)){

((Element)childList.get(i)).setText((String)entry.getValue());

}

}

}

}

}

writeToFile(doc,fileName);

}

}

}

3:接下来是测试的java文件TestXml.java

packagecom.tigerlee.xml;

importjava.io.File;

importjava.util.HashMap;

importjava.util.Iterator;

importjava.util.Map;

importjava.util.Set;

importjavax.xml.parsers.SAXParserFactory;

importorg.junit.Test;

publicclassTestXml{

@Test

publicvoidtestDomForXml(){

HandleXMLhandleXml=newHandleXML();

HandleXML.DOMForXmldomForXml=handleXml.newDOMForXml();

//解析

//domForXml.paseXml("student.xml");

//新增

//domForXml.addNewNode("Sun","20","Man","USA");

//删除

//domForXml.deleteNode("sun");

domForXml.updateNode("TigerLee");

}

@Test

publicvoidtestSAXForXml()throwsException{

HandleXMLhandleXml=newHandleXML();

HandleXML.SAXForXmlsaxForXml=handleXml.newSAXForXml();

SAXParserFactorysaxFactory=SAXParserFactory.newInstance();

javax.xml.parsers.SAXParsersp=saxFactory.newSAXParser();

sp.parse(newFile("student.xml"),saxForXml);

Mapcontents=saxForXml.getContents();

Iteratorkeys=contents.keySet().iterator();

while(keys.hasNext()){

Stringkey=(String)keys.next();

System.out.println(key+":"+contents.get(key));

}

}

@Test

publicvoidtestDom4jForXml(){

HandleXMLhandleXml=newHandleXML();

HandleXML.DOM4JForXmldom4j=handleXml.newDOM4JForXml();

//解析xml

dom4j.readXml("student.xml");

MapnewValue=newHashMap();

newValue.put("age","26");

dom4j.updateXml("TigerLee",newValue,"student.xml");

}

}

以上是自己总结的一些处理xml的方式,~~在处理xml的类中使用了内部类,测试类使用的是简单的单元测试方式。

例子中使用的jar包:

dom.jar

xercesImpl.jar

dom4j-1.6.1.jar

jaxen-1.1.1.jar

相关推荐