首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

java操作xml增删节查

2012-09-05 
java操作xml增删改查1:DOM4J性能最好,连Sun的JAXM也在用DOM4J.目前许多开源项目中大量采用DOM4J,例如大名

java操作xml增删改查
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

<?xml version="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

package com.tigerlee.xml; import java.io.File; import java.io.FileWriter; import java.io.Writer; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.Stack; 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.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; /** * * 在XML文档中实现增、删、改、查 * @author TigerLee * */ public class HandleXML { /** * 采用DOM方式 * @author TigerLee * */ public class DOMForXml{ /** * 获得doc对象 * @param fileName * @return */ public Document getDocument(String fileName){ Document document = null; try{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); document = builder.parse(new File(fileName)); }catch(Exception e){ e.printStackTrace(); } return document; } /** * 将改动持久到文件 * @param doc * @param distFileName */ public void modifyFile(Document doc,String distFileName){ try{ TransformerFactory tf = TransformerFactory.newInstance(); Transformer tfer = tf.newTransformer(); DOMSource dsource = new DOMSource(doc); StreamResult sr = new StreamResult(new File("student.xml")); tfer.transform(dsource, sr); }catch(Exception e){ e.printStackTrace(); } } /** * 解析 * @param fileName */ public void paseXml(String fileName){ Document document = getDocument(fileName); NodeList nodeList = document.getElementsByTagName("student"); for(int i=0;i<nodeList.getLength();i++){ StringBuilder sb = new StringBuilder(); 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()); } } /** * 创建一个新的学生 * @param name * @param age * @param sex * @param address */ public void addNewNode(String name,String age,String sex,String address){ try{ Document document = getDocument("student.xml"); NodeList nodeList = document.getElementsByTagName("students"); //创建新的节点 Node studentNode = document.createElement("student"); Node nameNode = document.createElement("name"); nameNode.appendChild(document.createTextNode(name)); Node ageNode = document.createElement("age"); ageNode.appendChild(document.createTextNode(age)); Node sexNode = document.createElement("sex"); sexNode.appendChild(document.createTextNode(sex)); Node addressNode = 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); // 此时真正的处理将新数据添加到文件中(磁盘) TransformerFactory tf = TransformerFactory.newInstance(); Transformer tfer = tf.newTransformer(); DOMSource dsource = new DOMSource(document); StreamResult sr = new StreamResult(new File("student.xml")); tfer.transform(dsource, sr); }catch(Exception e){ e.printStackTrace(); } paseXml("student.xml"); } /** * 删除一个节点 * @param name */ public void deleteNode(String name){ Document document = getDocument("student.xml"); NodeList nodeList = document.getElementsByTagName("name"); for(int i=0;i<nodeList.getLength();i++){ String value = nodeList.item(i).getFirstChild().getTextContent(); if(name!=null && name.equalsIgnoreCase(value)){ Node parentNode = nodeList.item(i).getParentNode(); document.getFirstChild().removeChild(parentNode); } } modifyFile(document,"student.xml"); } /** * 根据name修改某个节点的内容 * @param name */ public void updateNode(String name){ Document document = getDocument("student.xml"); NodeList nodeList = document.getElementsByTagName("name"); for(int i=0;i<nodeList.getLength();i++){ String value = nodeList.item(i).getFirstChild().getTextContent(); if(name!=null && name.equalsIgnoreCase(value)){ Node parentNode = nodeList.item(i).getParentNode(); NodeList nl = parentNode.getChildNodes(); for(int j=0;j<nl.getLength();j++){ String modifyNode= nl.item(j).getNodeName(); if(modifyNode.equalsIgnoreCase("age")){ nl.item(j).getFirstChild().setTextContent("25"); } } } } modifyFile(document,"student.xml"); } } /** * 采用SAX方式 * @author TigerLee * */ public class SAXForXml extends DefaultHandler{ private Stack tags = new Stack(); private Map contents = new LinkedHashMap(); public int count = 0; @Override public void characters(char[] ch, int start, int length) throws SAXException { String tag = (String)tags.peek(); if("name".equals(tag)){ String name = new String(ch,start,length); contents.put("name"+count,name); } if("age".equals(tag)){ contents.put("age"+count,new String(ch,start,length)); } if("sex".equals(tag)){ contents.put("sex"+count,new String(ch,start,length)); } if("address".equals(tag)){ contents.put("address"+count,new String(ch,start,length)); } } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if("student".equals(qName)){ count++; } tags.push(qName); } @Override public void endElement(String uri, String localName, String qName) throws SAXException { tags.pop(); } public Map getContents(){ return contents; } } /** * 采用JDOM方式 * @author TigerLee * */ public class JDOMForXml{ } /** * 采用DOM4J方式 * @author TigerLee * */ public class DOM4JForXml{ /** * 获取doc对象(org.dom4j.Document) * @param fileName * @return */ public org.dom4j.Document getDocument(String fileName){ SAXReader sr = new SAXReader(); org.dom4j.Document doc = null; try { doc = sr.read(new File(fileName)); } catch (Exception e) { e.printStackTrace(); } return doc; } /** * 将文件保存到硬盘 * @param doc * @param fileName */ public void writeToFile(org.dom4j.Document doc,String fileName){ try { Writer writer = new FileWriter(fileName); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); XMLWriter xmlWriter = new XMLWriter(writer, format); xmlWriter.write(doc); xmlWriter.close(); System.out.println(" 文件已经钝化!"); } catch (Exception e) { e.printStackTrace(); } } /** * 遍历xml文件输出节点值 * * @param fileName */ public void readXml(String fileName){ org.dom4j.Document doc = getDocument(fileName); List nameList = doc.selectNodes("/students/student/name"); List ageList = doc.selectNodes("/students/student/age"); List sexList = doc.selectNodes("/students/student/sex"); List addressList = doc.selectNodes("/students/student/address"); for(int i=0;i<nameList.size();i++){ StringBuilder sb = new StringBuilder(); 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()); } } /** * 根据姓名修改一个学生的信息 * @param name * @param newValue * @param fileName */ public void updateXml(String name,Map newValue,String fileName){ org.dom4j.Document doc = getDocument(fileName); List nameList = doc.selectNodes("/students/student/name"); Iterator iterator = nameList.iterator(); while(iterator.hasNext()){ Element element = (Element)iterator.next(); if(name!=null && name.equals(element.getText())){ Element pe = element.getParent(); List childList = pe.elements(); for(int i=0;i<childList.size();i++){ Iterator valueSet = newValue.entrySet().iterator(); while(valueSet.hasNext()){ Map.Entry entry = (Map.Entry)valueSet.next(); String nodeName = ((Element)childList.get(i)).getName(); String key = entry.getKey().toString(); if(key!=null && key.equals(nodeName)){ ((Element)childList.get(i)).setText((String)entry.getValue()); } } } } } writeToFile(doc, fileName); } } } 


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

package com.tigerlee.xml; import java.io.File; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; import javax.xml.parsers.SAXParserFactory; import org.junit.Test; public class TestXml{ @Test public void testDomForXml(){ HandleXML handleXml = new HandleXML(); HandleXML.DOMForXml domForXml = handleXml.new DOMForXml(); //解析 //domForXml.paseXml("student.xml"); // 新增 //domForXml.addNewNode("Sun", "20", "Man", "USA"); //删除 //domForXml.deleteNode("sun"); domForXml.updateNode("TigerLee"); } @Test public void testSAXForXml() throws Exception{ HandleXML handleXml = new HandleXML(); HandleXML.SAXForXml saxForXml = handleXml.new SAXForXml(); SAXParserFactory saxFactory = SAXParserFactory.newInstance(); javax.xml.parsers.SAXParser sp = saxFactory.newSAXParser(); sp.parse(new File("student.xml"), saxForXml); Map contents = saxForXml.getContents(); Iterator keys = contents.keySet().iterator(); while(keys.hasNext()){ String key = (String)keys.next(); System.out.println(key+":"+contents.get(key)); } } @Test public void testDom4jForXml(){ HandleXML handleXml = new HandleXML(); HandleXML.DOM4JForXml dom4j = handleXml.new DOM4JForXml(); //解析xml dom4j.readXml("student.xml"); Map newValue = new HashMap(); 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
1 楼 jamesji 2012-01-06   Try XML library Simple or XStream, they are way better than DOM or SAX.

热点排行