在系统之间交互传输的数据大都采用XML格式,在接到XML格式的字符串时我们需要将字符串转化为我们需要的格式(大多是对象),这时就需要解析XML,在Java中用dom4j插件比较多。这里先进行简单的学习下。
主要步骤如下:
1)将XML字符串通过DocumentHelper.parseText()方法转换为Document对象
2)通过XMLWriter对象的write()方法生成xml文档
package test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class XMLParse {
public static void main(String[]args) throws DocumentException {
String xmlStr="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"+
"<books>"+
"<book>"+
"<name>Java编程思想</name>"+
"<author>埃克尔</author>"+
"<publisher>机械工业出版社</publisher>"+
"<price>108</price>"+
"</book>"+
"<book>"+
"<name>数据结构与算法分析</name>"+
"<author>韦斯</author>"+
"<publisher>机械工业出版社</publisher>"+
"<price>55</price>"+
"</book>"+
"</books>";
createDocumentFile(xmlStr,"book.xml");
}
/**
* 功能描述: 生成xml文档<br>
* 〈功能详细描述〉
*
* @param document
* @param fileName
*/
/**
* 功能描述: 生成xml文档<br>
* 〈功能详细描述〉
*
* @param xmlStr xml格式的字符串
* @param fileName 生产文档的文件名
*/
public static voidcreateDocumentFile(String xmlStr,String fileName){
try {
//将XML字符串转换为Document对象
Document document =DocumentHelper.parseText(xmlStr);
//带有换行符的输出格式
OutputFormat format =OutputFormat.createPrettyPrint();
OutputStream out= newFileOutputStream(fileName);
XMLWriter writer = newXMLWriter(out, format);
//保存到本地文件
writer.write(document);
System.out.println("write end");
} catch (DocumentExceptione) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果:
package bean;
public class Book {
private String name;
private String author;
private String publisher;
private Double price;
public Book() {
super();
}
publicBook(String name, String author, String publisher, Double price) {
super();
this.name = name;
this.author = author;
this.publisher = publisher;
this.price = price;
}
@Override
public String toString() {
return "Book [name=" + name + ", author=" + author +", publisher=" + publisher + ", price=" + price +"]";
}
}
publicclass XMLParse {
public static void main(String[] args) throws DocumentException {
String xmlStr ="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"+
"<books>"+
"<book>"+
"<name>Java编程思想</name>"+
"<author>埃克尔</author>"+
"<publisher>机械工业出版社</publisher>"+
"<price>108</price>"+
"</book>"+
"<book>"+
"<name>数据结构与算法分析</name>"+
"<author>韦斯</author>"+
"<publisher>机械工业出版社</publisher>"+
"<price>55</price>"+
"</book>"+
"</books>";
List<Book> bookList =getBooksFromXmlStr(xmlStr);
System.out.println(bookList);
}
@SuppressWarnings("unchecked")
private static List<Book> getBooksFromXmlStr(String xmlStr) {
List<Book> bookList = new ArrayList<Book>();
try {
Document document = DocumentHelper.parseText(xmlStr);
List<Element> bookEles =document.selectNodes("//books/book");
for (Element bookEle : bookEles) {
String name =bookEle.elementText("name");
String author =bookEle.elementText("author");
String publisher =bookEle.elementText("publisher");
Double price =Double.parseDouble(bookEle.elementText("price"));
Book book = new Book(name,author, publisher, price);
bookList.add(book);
}
} catch (DocumentException e) {
e.printStackTrace();
}
return bookList;
}
}
运行结果: