步骤:
1、得到Document对象
2、得到Document对象后,根据Document对象提供的一些方法获取所需要的数据
下面从一个示例来看:
1、在工程中建xml文件
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book>
<title>面向对象设计</title>
<author>张三</author>
<price>50.00</price>
</book>
<book>
<title>Oracle数据库</title>
<author>李四</author>
<price>78.00</price>
</book>
<book>
<title>征服AJAX2.0</title>
<author>王五</author>
<price>58.00</price>
</book>
</books>
2、创建一个类似JavaBean的类,用于传递XML节点的值
public class Book {
private String title;
private String author;
private String price;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
3、建一个类读写XML文件(具体用到的类查看API)
import java.io.FileWriter;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
/**
* 在java中读写xml文件
* @author Yangtao
*
*/
public class XMLToNewXML {
public static void main(String[] args) {
try{
String uri = "books.xml";
ArrayList list = getBookList(uri);
FileWriter fs = new FileWriter("NewBook.xml");
fs.write("<?xml version=\"1.0\"?>");
[ft=#ffffff,,][ft=,4,
最新评论