<?xml version="1.0" encoding="utf-8"?><root> <node id="1" name="name1"> <node id="11" name="name11"> <node id="3122" name="name3"> <node id="2" name="新值" /> </node> </node> </node> <node id="4" name="name4" /></root>
/
/* * 创建日期 2007-2-8 * Author:Lulu * Msn:smildlzj@hotmail.com */package com.Try;import java.io.*;import java.util.*;
import org.jdom.Document;import org.jdom.Element;import org.jdom.input.SAXBuilder;import org.jdom.output.Format;import org.jdom.output.XMLOutputter;import org.jdom.xpath.XPath;
import com.Try.Item.Units;
public class XML {//转载请注明 static Document doc = null; public static String XMLFile="d:/web.xml"; public static int New_Id=1; public static Element root; /*############################################################ '* 创建日期 2007-2-8 '* 说明:自增编号 '* Msn:smildlzj@hotmail.com '* 作者: lulu '############################################################*/ public synchronized static String NewID() { New_Id++; return New_Id+""; } /*############################################################ '* 创建日期 2007-2-8 '* 说明:显示树,获得整棵树 '* Msn:smildlzj@hotmail.com '* 作者: lulu '############################################################*/
public static void ShowTree(LinkedList list,Element node,int Level) { Units unit=null; try{ List ls = node.getChildren(); Element el=null; for (Iterator iter = ls.iterator(); iter.hasNext(); ) { el = (Element) iter.next(); unit=new Units(); unit.setId(el.getAttributeValue("id")); unit.setValue(el.getAttributeValue("name")); unit.setLevel(Level); for(int i=0;i<Level;i++)System.out.print(" "); System.out.println(el.getAttributeValue("id")+"="+el.getAttributeValue("name")); ShowTree(list,el,Level+1); } }catch(Exception e){ System.out.println(e.toString()); } } /*############################################################ '* 创建日期 2007-2-8 '* 说明:查找节点,并返回 '* Msn:smildlzj@hotmail.com '* 作者: lulu '############################################################*/ public static Element FindNode(int id) {
try{ Element node=(Element) XPath.selectSingleNode(root,"//*[@id='"+id+"']"); //System.out.println(node.getAttributeValue("name")); return node; }catch(Exception e){ System.out.println(e.toString()); return null; } } /*############################################################ '* 创建日期 2007-2-8 '* 说明:插入新节点 '* Msn:smildlzj@hotmail.com '* 作者: lulu '############################################################*/ public static void InsertNode(int ParentId,String name) { try{ Element node=FindNode(ParentId); //找不到节点 if(node==null)return; Element newElement = new Element("node"); newElement.setAttribute("id",NewID()); newElement.setAttribute("name",name); node.addContent(newElement); }catch(Exception e){ System.out.println(e.toString()); } } /*############################################################ '* 创建日期 2007-2-8 '* 说明:删除节点 '* Msn:smildlzj@hotmail.com '* 作者: lulu '############################################################*/ public static Element DeleteNode(int Id) { try{ Element node=FindNode(Id); //找不到节点 if(node==null)return null;
node.getParent().removeContent(node); return node; }catch(Exception e){ System.out.println(e.toString()); return null; } } /*############################################################ '* 创建日期 2007-2-8 '* 说明:移动节点 '* Msn:smildlzj@hotmail.com '* 作者: lulu '############################################################*/ public static void MoveNode(int Id,int NewParentId) { try{ Element newParent=FindNode(NewParentId); //找不到新父节点 if(newParent==null)return; Element node=DeleteNode(Id); //找不到需要移动的节点 if(newParent==null)return; newParent.addContent(node); }catch(Exception e){ System.out.println(e.toString()); } } public static void main (String args[]) throws Exception {
SAXBuilder builder = new SAXBuilder(); try { LinkedList list=new LinkedList(); doc = builder.build(new File(XMLFile));; root = doc.getRootElement(); //ShowTree(list,root,0); //FindNode(31); //InsertNode(3122,"新值"); //DeleteNode(2); MoveNode(3122,11); Save(); } catch (Exception ex) { ex.printStackTrace(); }
} /*############################################################ '* 创建日期 2007-2-8 '* 说明:输出到XML '* Msn:smildlzj@hotmail.com '* 作者: lulu '############################################################*/ public static void Save() throws FileNotFoundException, IOException { Format format = Format.getCompactFormat();
format.setEncoding("utf-8"); //设置xml文件的字符为utf-8
format.setIndent(" "); //设置xml文件的缩进
XMLOutputter out = new XMLOutputter(format);//元素后换行一层元素缩进 out.output(doc,new FileOutputStream(XMLFile)); }}