JDOM 创造 读取 修改 删除 XML文件
JDOM 创建 读取 修改 删除 XML文件转自:http://takeme.iteye.com/blog/1825967?加入?D:\Soft\jar\jdom.jar
JDOM 创建 读取 修改 删除 XML文件
转自:http://takeme.iteye.com/blog/1825967
?
加入?
D:\Soft\jar\jdom.jar?
Java代码??
- package?com.itheima.xml;??
- ??
- import?java.io.File;??
- import?java.io.FileWriter;??
- import?java.util.Iterator;??
- import?java.util.List;??
- ??
- import?org.jdom.Attribute;??
- import?org.jdom.Document;??
- import?org.jdom.Element;??
- import?org.jdom.input.SAXBuilder;??
- import?org.jdom.output.Format;??
- import?org.jdom.output.XMLOutputter;??
- ??
- public?class?TextJDOMparseXML?{//操作使用JDOM解析XML文件的类??
- ??
- ????/**?
- ?????*?@param?args?
- ?????*/??
- ????public?static?void?main(String[]?args)?{??
- ????????//?TODO?Auto-generated?method?stub??
- ????????String?fileName="F:/test1.xml";??
- ????????TextJDOMparseXML?jdomXml=new?TextJDOMparseXML();??
- ????????System.out.println("===============1.运用jdom创建XML文件");??
- ????????if(jdomXml.createXMLFile(fileName,?"GB2312")==1)??
- ????????????System.out.println("运用JDOM创建XML文件成功!");??
- ????????else??
- ????????????System.out.println("运用JDOM创建XML文件失败!");??
- ????????System.out.println("===============2.运用JDOM读取XML文件");??
- ????????jdomXml.read(jdomXml.getDocument(fileName));??
- ????????System.out.println("===============3.修改XML内容,输出修改后的文档");??
- ????????jdomXml.update(fileName,?"rongshu",?"liushu");??
- ????????System.out.println("===============4.删除编号为002的节点,输出修改后的文件");??
- ????????jdomXml.delete(fileName,?"002");??
- ??????????
- ????}??
- ??????
- ????//生成一个新的Document????
- ????public?Document?createDocument(){??
- ????????Document?document=new?Document();??
- ????????return?document;??
- ????}??
- ????//创建一个节点??
- ????public?Element?createElement(String?paramName,String?paramValue){??
- ????????Element?element=new?Element(paramName);??
- ????????element.setText(formatTextForXML(paramValue));??
- ????????return?element;??
- ????}??
- ????//创建?XML文件??
- ????public?int?createXMLFile(String?fileName,String?encoding){??
- ????????int?returnValue=0;??
- ????????Document?document=createDocument();??
- ????????Element?treesElement=createElement("trees",?null);??
- ????????Element?treeElement=new?Element("tree");??
- ????????treeElement.setAttribute("id","001");??
- ????????treeElement.setAttribute("name","rongshu");??
- ??????????
- ????????Element?typeElement=new?Element("type");??
- ????????typeElement.setText("style");??
- ????????typeElement.setAttribute("id","xiaoshu");??
- ????????treeElement.addContent(typeElement);??//添加?子节点??
- ??????????
- ??????????
- ????????Element?descElement=new?Element("description");??
- ????????descElement.setText(formatTextForXML("It?is?a?big?tree"));??
- ????????treeElement.addContent(descElement);??
- ??????????
- ????????treesElement.addContent(treeElement);??
- ??????????
- ????????Element?tree1Element=new?Element("tree");??
- ????????tree1Element.setAttribute("id","002");??
- ????????tree1Element.setAttribute("name","yangshu");??
- ????????Element?type1Element=new?Element("type");??
- ????????type1Element.setText("style");??
- ????????type1Element.setAttribute("id","dashu");??
- ????????tree1Element.addContent(type1Element);??
- ????????Element?desc1Element=new?Element("description");??
- ????????desc1Element.setText(formatTextForXML("The?tree?grows?taller?from?day?to?day"));??
- ????????tree1Element.addContent(desc1Element);??
- ????????treesElement.addContent(tree1Element);??
- ??????????
- ??????????
- ????????Element?tree2Element=new?Element("tree");??
- ????????tree2Element.setAttribute("id","003");??
- ????????tree2Element.setAttribute("name","songshu");??
- ????????Element?desc2Element=new?Element("description");??
- ????????desc2Element.setText(formatTextForXML("The?tree?is?very?beautiful"));??
- ????????tree2Element.addContent(desc2Element);??
- ????????treesElement.addContent(tree2Element);??
- ??????????
- ????????Element?flowerElement=new?Element("flower");??
- ????????flowerElement.setText("rose");??
- ????????treesElement.addContent(flowerElement);??
- ??????????
- ????????document.addContent(treesElement);??
- ????????try?{??
- ????????????saveFile(fileName,document,encoding);??
- ????????????returnValue=1;??
- ????????}?catch?(Exception?e)?{??
- ????????????//?TODO:?handle?exception??
- ????????????System.out.println("创建XML文件失败:"+e.getMessage());??
- ????????}??
- ????????return?returnValue;??
- ??????????
- ????}??
- ????//读取xml??
- ????public?void?read(Document?document){??
- ????????try?{??
- ????????????Format?format=Format.getPrettyFormat();???
- ????????????format.setEncoding("GB2312");??
- ??????????????
- ????????????XMLOutputter?xmlOutputter=new?XMLOutputter(format);??
- ????????????xmlOutputter.output(document,?System.out);??
- ????????}?catch?(Exception?e)?{??
- ????????????System.out.println("读取XML文件失败:"+e.getMessage());??
- ????????}??
- ????}??
- ????//修改?xml文档节点??
- ????public?int?update(String?fileName,String?oldName,String?newName){??
- ????????int?returnValue=0;??
- ????????Document?document=getDocument(fileName);??
- ????????List?list=document.getRootElement().getChildren();??
- ????????for?(int?i?=?0;?i?<?list.size();?i++)?{??
- ????????????Element?element=(Element)list.get(i);??
- ????????????Attribute?attribute=element.getAttribute("name");??
- ????????????if(attribute!=null){??
- ????????????????if(attribute.getValue().equals(oldName)){??
- ????????????????????attribute.setValue(newName);??
- ????????????????}??
- ????????????}??
- ????????}??
- ??????????
- ????????list=document.getRootElement().getChildren("flower");??
- ????????Iterator?iter=list.iterator();??
- ????????if(iter.hasNext()){??
- ????????????Element?el=(Element)iter.next();??
- ????????????el.setText("lily");??
- ????????????Element?date=new?Element("date");??
- ????????????date.setText("2009-04-10");??
- ????????????date.setAttribute("type","calendar");??
- ????????????el.addContent(date);??
- ????????}??
- ??????????
- ????????try?{??
- ????????????showFile(fileName,document);??
- ????????????returnValue=1;??
- ????????}?catch?(Exception?e)?{??
- ????????????//?TODO:?handle?exception??
- ????????????System.out.println("修改XML文件出错:"+e.getMessage());??
- ????????}??
- ??????????
- ????????return?returnValue;??
- ??????????
- ??????????
- ????}??
- ????//删除XML?节点??
- ????public?boolean?delete(String?fileName,String?id){??
- ????????boolean?returnVlalue=false;??
- ????????Document?doc=getDocument(fileName);??
- ????????Element?root=doc.getRootElement();??
- ????????List?list=root.getChildren("tree");??
- ????????for?(int?i?=?0;?i?<?list.size();?i++)?{??
- ????????????Element?element=(Element)list.get(i);??
- ????????????if?(element.getAttribute("id").getValue().equals(id))?{??
- ????????????????root.removeContent(element);??
- ????????????}??
- ????????}??
- ????????try?{??
- ????????????showFile(fileName,?doc);??
- ????????????returnVlalue=true;??
- ????????}?catch?(Exception?e)?{??
- ????????????//?TODO:?handle?exception??
- ????????????e.printStackTrace();??
- ????????}??
- ????????return?returnVlalue;??
- ??????????????????
- ????}??
- ??????
- ????private?void?showFile(String?fileName,?Document?document)?{??
- ????????//?TODO?Auto-generated?method?stub??
- ????????Format?format=Format.getPrettyFormat();??
- ????????format.setEncoding("GB2312");??
- ????????format.setIndent(" ");??
- ????????try?{??
- ????????????XMLOutputter?xmlOutputter=new?XMLOutputter(format);??
- ????????????FileWriter?fileWriter=new?FileWriter(fileName);??
- ????????????xmlOutputter.output(document,?fileWriter);??
- ????????????xmlOutputter.output(document,?System.out);??
- ????????}?catch?(Exception?e)?{??
- ????????????System.out.println("XML文件输出到控制台出错:"+e.getMessage());??
- ????????}??
- ????}??
- ??
- ????private?Document?getDocument(String?fileName)?{??
- ????????//?TODO?Auto-generated?method?stub??
- ????????Document?document=null;??
- ????????try?{??
- ????????????SAXBuilder?saxBuilder=new?SAXBuilder();??
- ????????????document=saxBuilder.build(new?File(fileName));??
- ????????}?catch?(Exception?e)?{??
- ????????????//?TODO:?handle?exception??
- ????????????System.out.println("载入失败:"+e.getMessage());??
- ????????????return?null;??
- ????????}??
- ????????return?document;??
- ????}??
- ??
- ????private?boolean?saveFile(String?fileName,?Document?document,?String?encoding)?{??
- ????????//?TODO?Auto-generated?method?stub??
- ????????Format?format=Format.getPrettyFormat();??
- ????????if(encoding!=null){??
- ????????????format.setEncoding(encoding);??
- ????????}??
- ????????format.setIndent("?");?//设置xml文件的缩进??
- ????????try?{??
- ????????????XMLOutputter?xmlOutputter=new?XMLOutputter(format);??
- ????????????FileWriter?fileWriter=new?FileWriter(fileName);??
- ????????????xmlOutputter.output(document,?fileWriter);??
- ????????}?catch?(Exception?e)?{??
- ????????????System.out.println("保存文件出错:"+e.getMessage());??
- ????????????return?false;??
- ????????}??
- ????????return?true;??
- ????}??
- ??
- ????private?String?formatTextForXML(String?sourceString)?{??
- ????????//?TODO?Auto-generated?method?stub??
- ????????if?(sourceString==null)?{??
- ????????????return?null;??
- ????????}??
- ????????int?stLen=0;??
- ????????StringBuffer?reString=?new?StringBuffer();??
- ????????String?deString="";??
- ????????stLen=sourceString.length();??
- ????????for?(int?i?=?0;?i?<?stLen;?i++)?{??
- ????????????char?ch=sourceString.charAt(i);??
- ????????????switch?(ch)?{??
- ????????????case?'<':??
- ????????????????deString="<";??
- ????????????????break;??
- ????????????case?'>':??
- ????????????????deString=">";??
- ????????????????break;??
- ????????????case?'"':??
- ????????????????deString=""";??
- ????????????????break;??
- ????????????case?'&':??
- ????????????????deString="&";??
- ????????????????break;??
- ????????????case?13:??
- ????????????????deString="\n";??
- ????????????????break;??
- ????????????default:??
- ????????????????deString=""+ch;??
- ????????????????break;??
- ????????????}??
- ????????????reString.append(deString);??
- ????????}??
- ????????return?reString.toString();??
- ????}?????
- ??????
- ??????
- } ?