1、程序代码
package jdom_3;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
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;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
/**
* @description :JDom生成、解析MXL文件实例
* @date : 2013-12-26下午02:17:44
* @author :黑马白浪
*/
public class JdomXML {
/**
* 生成XML文件
*/
public void buildXml(){
//创建XML文档
Document doc=new Document();
//创建跟节点root,并加到doc中去
Element root=new Element("books");
doc.setRootElement(root);
//给root跟节点添加两个属性company、department
root.setAttribute("company", "DHC");
root.setAttribute("department", "zhongdian");
//在根节点root下添加第一个元素
Element bookA=new Element("book");
bookA.setAttribute("version", "3.3");
bookA.setAttribute("email", "wbj990936@163.com");
Element bookNameA=new Element("name");
bookNameA.setText("java基础教程");
bookA.addContent(bookNameA);
Element bookAuthorA=new Element("author");
bookAuthorA.setText("wangbaojian");
bookA.addContent(bookAuthorA);
Element bookPriceA=new Element("price");
bookPriceA.setText("60RMB");
bookA.addContent(bookPriceA);
root.addContent(bookA);
//在根节点root下添加第二个元素
Element bookB=new Element("book");
bookB.setAttribute("version", "4.3");
bookB.setAttribute("email", "yanghui@163.com");
bookB.addContent(new Element("name").setText("ORACLE高级教程"));
bookB.addContent(new Element("author").setText("yanghui"));
bookB.addContent(new Element("price").setText("120RMB"));
root.addContent(bookB);
//在根节点root下添加第三个元素
Element bookC=new Element("person");
bookC.setAttribute("version", "5.3");
bookC.setAttribute("email", "yanming@163.com");
bookC.addContent(new Element("name").setText("Linux高级教程"));
bookC.addContent(new Element("author").setText("yanming"));
bookC.addContent(new Element("price").setText("100RMB"));
root.addContent(bookC);
XMLOutputter XMLOut = new XMLOutputter(FormatXML());
try {
String xmlPath=System.getProperty("user.dir")+"/src/jdom_3/output/library.xml";
XMLOut.output(doc, new FileOutputStream(xmlPath));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 格式化生成的xml文件,如果不进行格式化的话,生成的xml文件将会是很长的一行...
*/
public Format FormatXML(){
Format format = Format.getCompactFormat();
format.setEncoding("utf-8");
format.setIndent(" ");
return format;
}
/*
* 解析XML文件
*/
public void parseXml(){
String xmlPath=System.getProperty("user.dir")+"/src/jdom_3/output/library.xml";
SAXBuilder builder = new SAXBuilder(false);
try{
//得到Document,我们以后进行的所有操作都是对这个Document操作的.
Document doc=builder.build(xmlPath);
//取到根元素,xml文件里只能有一个根元素
Element books=doc.getRootElement();
List<Element> list=(List<Element>)books.getChildren();
//List list=books.getChildren("book");
for(Iterator<Element> iterator=list.iterator();iterator.hasNext();){
Element element=(Element)iterator.next();
//遍历元素的属性并输出
List<Attribute> attributes=element.getAttributes();
for(Iterator<Attribute> iter=attributes.iterator();iter.hasNext();){
Attribute attr=(Attribute)iter.next();
System.out.println(attr.getQualifiedName()+" : "+attr.getValue());
}
//取出book元素的子元素
List<Element> elems=element.getChildren();
for(Iterator<Element> iter=elems.iterator();iter.hasNext();){
Element elem=(Element)iter.next();
System.out.println(elem.getName()+" : "+elem.getTextTrim());
}
System.out.println("------------------------------");
}
}catch(JDOMException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
public static void main(String[] args) {
JdomXML jdomXml=new JdomXML();
jdomXml.buildXml();
jdomXml.parseXml();
}
}
2、生成的XML文件
<?xml version="1.0" encoding="utf-8"?>
<books company="DHC" department="zhongdian">
<book version="3.3" email="wbj990936@163.com">
<name>java基础教程</name>
<author>wangbaojian</author>
<price>60RMB</price>
</book>
<book version="4.3" email="yanghui@163.com">
<name>ORACLE高级教程</name>
<author>yanghui</author>
<price>120RMB</price>
</book>
<person version="5.3" email="yanming@163.com">
<name>Linux高级教程</name>
<author>yanming</author>
<price>100RMB</price>
</person>
</books>