欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JDOM解析处理xml

程序员文章站 2022-05-28 08:05:20
...
[size=large][color=red]1.解析xml[/color][/size]
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]http://dl.iteye.com/upload/attachment/449423/59326d1c-57a7-32e4-ab39-eb2ed87859e8.png[/img]
[/img]
[size=large][color=red]2.创建xml[/color][/size]
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]http://dl.iteye.com/upload/attachment/449425/cd7069db-8089-310a-92cb-15bb523d97e5.png[/img]
[/img]
[size=large][color=red]3.修改xml[/color][/size]
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]http://dl.iteye.com/upload/attachment/449429/f26353d9-0e8d-3908-9c52-d00a92637aa9.png[/img]
[/img]

生成如下文件
[img]http://dl.iteye.com/upload/attachment/449427/702d2c5b-6802-3ea8-9656-1ca6335f184a.png[/img]
[/img]
将jdom-jar导入到项目中去:
[img]
[img]http://dl.iteye.com/upload/attachment/449432/fcbadaf4-e7c7-3e58-891d-b242f0e268e5.png[/img]
[/img]