使用SAXReader解析xml数据

使用SAXReader解析xml数据//XML转ListList<Map<String,String>>list=newArrayList<Map<String,String>>();SAXReaderreader=newSAXReader();ReaderstringReader=newStringReader(strReturn);Documentdocument=reader.read(strin.

大家好,欢迎来到IT知识分享网。使用SAXReader解析xml数据"

// XML转List
List<Map<String,String>> list =new ArrayList<Map<String,String>>();

SAXReader reader = new SAXReader();
            
Reader stringReader = new StringReader(strReturn);
            
Document document = reader.read(stringReader);
            
Element rootElement = document.getRootElement();
            
listNodes(rootElement,list,"0");
public static List<Map<String, String>> listNodes(Element node, List<Map<String,String>> list, String flag) {
        if(node.getName().equals("L_VALUESET_TAB_ITEM")){
            Map<String,String> para=new HashMap<String, String>();
            Element FLEX_VALUE = node.element("FLEX_VALUE");
            Element FLEX_DESC = node.element("FLEX_DESC");
            Element ENABLED_FLAG = node.element("ENABLED_FLAG");
            if (StringUtils.equalsIgnoreCase("Y", ENABLED_FLAG.getText())) {
                para.put("EBS_SHOP_ID", FLEX_VALUE.getText());
                para.put("EBS_SHOP_NM", FLEX_DESC.getText());
                para.put("ENABLED_FLAG", ENABLED_FLAG.getText());
                para.put("FLAG", flag);
                list.add(para);
            }
        }
        // 当前节点下面子节点迭代器
        Iterator<Element> it = node.elementIterator();
        // 遍历
        while (it.hasNext()) {
            // 获取某个子节点对象
            Element e = it.next();
            // 对子节点进行遍历
            listNodes(e,list,flag);
        }
        return list;
    }

 

另外一个例子

book.xml数据如下:

<books>
    <book>
        <author>Thomas</author>
        <title>Java从入门到放弃</title>
        <publisher>UCCU</publisher>
    </book>
    <book>
        <author>小白</author>
        <title>MySQL从删库到跑路</title>
        <publisher>Go Die</publisher>
    </book>
    <book>
        <author>PHPer</author>
        <title>Best PHP</title>
        <publisher>PHPchurch</publisher>
    </book>
</books>
package com;
 
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
 
import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.List;
 
public class SAXReaderXML {
    public static void main(String[] args) throws Exception {
        SAXReader reader = new SAXReader();
        File xmlfile = new File("D:/books.xml");
        String xml = "<books><book><author>Thomas</author><title>Java从入门到放弃</title><publisher>UCCU</publisher>" +
                "</book><book><author>小白</author><title>MySQL从删库到跑路</title><publisher>GoDie</publisher></book>" +
                "<book><author>PHPer</author><title>BestPHP</title><publisher>PHPchurch</publisher></book></books>";
        Document fileDocument = reader.read(xmlfile);//从xml文件获取数据
        Document document = reader.read(new ByteArrayInputStream(xml.getBytes("utf-8")));//读取xml字符串,注意这里要转成输入流
        Element root = document.getRootElement();//获取根元素
        List<Element> childElements = root.elements();//获取当前元素下的全部子元素
 
        for (Element child : childElements) {//循环输出全部book的相关信息
            List<Element> books = child.elements();
            for (Element book : books) {
                String name = book.getName();//获取当前元素名
                String text = book.getText();//获取当前元素值
                System.out.println(name + ":" + text);
            }
        }
        //获取第二条书籍的信息
        Element book2 = childElements.get(1);
        Element author = book2.element("author");//根据元素名获取子元素
        Element title = book2.element("title");
        Element publisher = book2.element("publisher");
        System.out.println("作者:" + author.getText());//获取元素值
        System.out.println("书名:" + title.getText());
        System.out.println("出版社:"+publisher.getText());
    }
}

 

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/15817.html

(0)

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信