将String类型的XML解析并设置到实体类中

时间:2022-08-07 16:26:28
package com.mooc.string;

import java.util.ArrayList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element; import com.mooc.sax.Book; public class XMLString2 { public static Element getChildElement(Element e,String element_name){ Element returnElement = null;
List<Element> elements = e.elements();
if(elements!=null&&!elements.isEmpty()){
for (Element element : elements) {
if(element.getName().equals(element_name)){
returnElement = element;
break;
}
}
}
return returnElement;
} public static void main(String[] args) {
String resp = "<?xml version='1.0' encoding='UTF-8'?><bookstore><book id='1'><name>冰与火之歌</name><author>乔治马丁</author><year>2014</year><price>89</price></book><book id='2'><name>安徒生童话</name><author>安徒生</author><year>2014</year><price>69</price></book></bookstore>";
List<Book> books = new ArrayList<Book>();
Book kBook = null;
try {
Document doc = DocumentHelper.parseText(resp);
//指向根节点
Element root = doc.getRootElement();
// Element book = XMLString2.getChildElement(root, "book");
//获取book的属性值
// String id = book.attributeValue("id");
// kBook.setId(id);
List<Element> elements = root.elements();
for (Element element : elements) {
kBook = new Book();
kBook.setId(element.attributeValue("id"));
kBook.setName(element.element("name").getStringValue());
kBook.setAuthor(element.element("author").getStringValue());
kBook.setPrice(element.element("price").getStringValue());
kBook.setYear(element.element("year").getStringValue());
books.add(kBook);
kBook = null;
} } catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (Book book : books) {
System.out.println(book);
} } }
package com.mooc.sax;

public class Book {

    private String id;
private String name;
private String author;
private String year;
private String price;
private String language;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
@Override
public String toString() {
return "Book [id=" + id + ", name=" + name + ", author=" + author + ", year=" + year + ", price=" + price
+ ", language=" + language + "]";
} }
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book id="1">
<name>冰与火之歌</name>
<author>乔治马丁</author>
<year>2014</year>
<price>89</price>
</book>
<book id="2">
<name>安徒生童话</name>
<year>2004</year>
<price>77</price>
<language>English</language>
</book>
</bookstore>