zoukankan      html  css  js  c++  java
  • 将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>
  • 相关阅读:
    VisualSVN-Server windows 版安装时报错 "Service 'VisualSVN Server' failed to start. Please check VisualSVN Server log in Event Viewer for more details."
    Pytest 单元测试框架之初始化和清除环境
    Pytest 单元测试框架入门
    Python(email 邮件收发)
    Python(minidom 模块)
    Python(csv 模块)
    禅道简介
    2020年最好的WooCommerce主题
    Shopify网上开店教程(2020版)
    WooCommerce VS Magento 2020:哪个跨境电商自建站软件更好?
  • 原文地址:https://www.cnblogs.com/james-roger/p/5437687.html
Copyright © 2011-2022 走看看