将String类型的XML解析并设置到实体类中
复制代码
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>
复制代码

 

posted on   james-roger  阅读(7410)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示