Jdom读写XML文件

XML文件:

<?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");
}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值