修改前:
<?xml version="1.0" encoding="GBK"?>
<bookList>
<book>
<name>Java</name>
<author>張三</author>
<publishDate>2002-6-6</publishDate>
<price>35.0</price>
</book>
<book>
<name>XML</name>
<author>李四</author>
<publishDate>2002-9-16</publishDate>
<price>92.0</price>
</book>
</bookList>
修改后:
<?xml version="1.0" encoding="UTF-8"?>
<bookList>
<book hot="true">
<name>Java</name>
<author>redtroy</author>
<publishDate>2002-6-6</publishDate>
<price>100.0</price>
</book>
<book>
<name>XML</name>
<author>李四</author>
<publishDate>2002-9-16</publishDate>
<price>92.0</price>
</book>
</bookList>
操作類:
package com.Gavin.xml;

import org.jdom.*;
import org.jdom.output.*;
import org.jdom.input.*;
import java.io.*;
import java.util.List;

public class JDOMDemo {

public static void main(String[] args) throws Exception {
SAXBuilder sb = new SAXBuilder();
Document doc = sb.build(new FileInputStream("d:/exampleA.xml"));
Element root = doc.getRootElement();
List books = root.getChildren();
Element book = (Element) books.get(0);
Attribute newattribute = new Attribute("hot", "true");
book.setAttribute(newattribute);
Element author = (Element) book.getChild("author");
author.setText("redtroy");
Element price = book.getChild("price");
price.setText(Float.toString(100));

Format format = Format.getCompactFormat();
//設置文檔字符編碼
format.setEncoding("GB18030");
//設置縮進字符串
format.setIndent(" ");

// XMLOutputter out = new XMLOutputter();
XMLOutputter out = new XMLOutputter(format);
out.output(doc, new FileOutputStream("d:/exampleB.xml"));
}
}































操作類:

































