XML文件:
Java通过JDom读取该文件:
Java通过JDom写入该文件:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<books>
<book bn="001">
<name>java test 001</name>
<author>赵大</author>
<publisher>清华大学出版社</publisher>
<publishdate>2011-01-02</publishdate>
</book>
<book bn="002">
<name>java test 002</name>
<author>赵大2</author>
<publisher>清华大学出版社2</publisher>
<publishdate>2011-01-03</publishdate>
</book>
</books>
Java通过JDom读取该文件:
package JDOMTEST;
import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
public class JdomReadXML
{
public static void getXMLContent(String strFile)
{
SAXBuilder builder = new SAXBuilder(false);
try
{
parse(builder.build(strFile));
}
catch (Exception ex)
{
System.out.println(ex);
}
}
public static void parse(Document doc) throws Exception
{
Element books = doc.getRootElement();
List bookList = books.getChildren("book");
for (Iterator<?> iter = bookList.iterator(); iter.hasNext();)
{
Element book = (Element)iter.next();
String strBN = book.getAttributeValue("bn");
System.out.println("bn : " + strBN);
String strName = book.getChildText("name");
System.out.println("name : " + strName);
String strAuthor = book.getChildText("author");
System.out.println("author : " + strAuthor);
String strPublisher = book.getChildText("publisher");
System.out.println("publisher : " + strPublisher);
String strPublishdate = book.getChildText("publishdate");
System.out.println("publishdate : " + strPublishdate);
System.out.println("===========================================");
System.out.println();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
getXMLContent("bookinfo.xml");
}
}
Java通过JDom写入该文件:
package JDOMTEST;
import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
public class JdomWriteXML {
public static void writeXMLContent(String strFile)
{
SAXBuilder builder = new SAXBuilder(false);
System.out.println("Start");
try
{
writeData(builder.build(strFile), strFile);
}
catch (Exception ex)
{
System.out.println(ex);
}
System.out.println("End");
}
public static void writeData(Document doc, String xmlpath) throws Exception
{
Element books = doc.getRootElement();
Element book = new Element("book");
book.setAttribute("bn", "003");
Element name = new Element("name");
name.setText("java test 003");
book.addContent(name);
Element author = new Element("author");
author.setText("王五");
book.addContent(author);
Element publisher = new Element("publisher");
publisher.setText("北京大学出版社");
book.addContent(publisher);
Element publishdate = new Element("publishdate");
publishdate.setText("2011-03-02");
book.addContent(publishdate);
books.addContent(book);
XMLOutputter outputter = new XMLOutputter();
outputter.output(doc, new FileOutputStream(xmlpath));
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
writeXMLContent("bookinfo.xml");
}
}