转自http://qingbyqing.iteye.com/blog/978113
1.解析xml
- package com.qingbyqing.xmlParse;
- import java.io.IOException;
- import java.util.List;
- import org.jdom.Attribute;
- import org.jdom.Document;
- import org.jdom.Element;
- import org.jdom.JDOMException;
- import org.jdom.input.SAXBuilder;
- /**
- * 解析xml
- * @author qingbyqing
- * @date 2011-3-29
- */
- public class XmlPasreUtil {
- Element root=null ;
- Document doc=null ;
- /**
- * 获得根元素
- * @param fileName 用于指定的xml文件名
- * @return
- */
- public Element getRoot(String fileName){
- //1.创建解析对象
- //2.解析对象调用build方法建立文档对象
- //3.文档对象获得根元素
- SAXBuilder builder = new SAXBuilder( false );
- try {
- doc = builder.build(fileName);
- } catch (JDOMException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- Element root = doc.getRootElement();
- return root;
- }
- /**
- * 获取xml的所有信息
- * @param root 传入一个根元素对象
- */
- public void getElementInfo(Element root){
- //遍历跟元素
- List<?> flist = root.getChildren("floor" );
- for ( int i= 0 ;i<flist.size();i++){
- Element floor =(Element) flist.get(i);
- String level = floor.getAttributeValue("level" );
- p("第" +level+ "层" );
- List<?> rlist = floor.getChildren("room" );
- for ( int j= 0 ;j<rlist.size();j++){
- Element room = (Element)rlist.get(j);
- String id=room.getAttributeValue("id" );
- String rep=room.getChild("represent" ).getText();
- String tel=room.getChild("telephone" ).getText();
- p("寝室编号:" +id+ " 负责人:" +rep+ " 寝室号码:" +tel);
- }
- }
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- XmlPasreUtil pasre = new XmlPasreUtil();
- Element root=pasre.getRoot("I:\\xml\\flats.xml" );
- pasre.getElementInfo(root);
- }
- public void p(String str){
- System.out.println(str);
- }
- }
[img]
[/img]
2.创建xml
- package com.qingbyqing.xmlParse;
- import java.io.FileWriter;
- import java.io.IOException;
- import org.jdom.Attribute;
- import org.jdom.Comment;
- import org.jdom.Document;
- import org.jdom.Element;
- import org.jdom.output.Format;
- import org.jdom.output.XMLOutputter;
- /**
- * 创建一个xml
- * @author qingbyqing
- * @date 2011-3-29
- */
- public class XmlCreateUtil {
- Element root=null ; //跟元素
- Document doc=null ; //文档对象
- /**
- * 创建xml
- * @param ajax
- */
- public void createRootElement(String[] ajaxs){
- //1.根据数组创建跟元素
- //2.建立解释内容
- //3.建立子元素
- root = new Element( "ajax" );
- //建立解释内容
- Comment comment = new Comment( "简单的xml示例" );
- root.addContent(comment);
- //建立子元素
- for ( int i= 0 ;i<ajaxs.length;i++){
- Element ajax = new Element( "ajax" ); //子元素标签
- ajax.addContent(ajaxs[i]);//子元素内容
- Attribute id = new Attribute( "id" , new Integer(i).toString()); //子元素属性
- ajax.setAttribute(id);
- root.addContent(ajax);
- }
- }
- public void outputXml(String fileName){
- //1.根据跟元素创建文档
- //2.创建输出对象
- //3.创建输出格式对象
- //4.格式化文档
- //5.创建文件输出对象
- //6.输出对象
- doc =new Document(root);
- XMLOutputter out = new XMLOutputter();
- Format format = Format.getPrettyFormat();
- //设置输出格式
- format.setIndent(" " );
- format.setEncoding("gbk" );
- out.setFormat(format);
- try {
- FileWriter fwt = new FileWriter(fileName);
- out.output(doc, fwt);
- fwt.flush();
- fwt.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public static void main(String args[]){
- String[] ajaxs={"JAVASCRIPT" , "HTML" , "CSS" , "DOM" , "XMLHTTPREQUEST" };
- XmlCreateUtil cu = new XmlCreateUtil();
- cu.createRootElement(ajaxs);
- cu.outputXml("I:\\xml\\ajaxs.xml" );
- }
- }
[img]
[/img]
3.修改xml
- package com.qingbyqing.xmlParse;
- import java.io.FileWriter;
- import java.io.IOException;
- import org.jdom.Document;
- import org.jdom.Element;
- import org.jdom.JDOMException;
- import org.jdom.input.SAXBuilder;
- import org.jdom.output.Format;
- import org.jdom.output.XMLOutputter;
- /**
- * 对xml进行修改删除
- * @author qingbyqing
- * @date 2011-3-29
- */
- public class XmlMendUtil {
- private Document doc= null ;
- //根据需要修改的文件创建文档
- public void setRootNode(String fileName){
- SAXBuilder builder = new SAXBuilder();
- try {
- doc=builder.build(fileName);
- } catch (JDOMException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void mendXml(){
- if (doc!= null ){
- Element root = doc.getRootElement();
- root.removeContent(root.getChildren().size()-1 ); //删除最后一个元素
- //删除第一个元素下的id属性
- root.getChild("ajax" ).setAttribute( "id" , "3" );
- root.getChild("ajax" ).removeContent();
- //修改第二个元素的内容为:DHTML
- root.getChild("ajax" ).addContent( "DTML" );
- }
- return ;
- }
- public void outputXML(String fileName){
- XMLOutputter out=new XMLOutputter();
- Format format = Format.getPrettyFormat();
- format.setEncoding("UTF-8" );
- format.setIndent(" " );
- out.setFormat(format);
- try {
- FileWriter fwt = new FileWriter(fileName);
- out.output(doc, fwt);
- fwt.flush();
- fwt.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- XmlMendUtil mend=new XmlMendUtil();
- mend.setRootNode("I:\\xml\\ajaxs.xml" );
- mend.mendXml();
- mend.outputXML("I:\\xml\\mended.xml" );
- }
- }
[img]
[/img]
生成如下文件
[/img]
将jdom-jar导入到项目中去:
[img]
[/img]