• dom4j解析xml字符串


    import java.util.Iterator;
    import java.util.List;
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.DocumentHelper;
    import org.dom4j.Element;
    
    
    /**
     *
     * @author y
     */
    public class Test {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws DocumentException {
            test3();
        }
        
        public static void test3() throws DocumentException{
            String str = "<?xml version="1.0" encoding="UTF-8"?>" +
                        "<books>" +
                        "    <book>" +
                        "        <name>Think in Java</name>" +
                        "        <price>120.0</price>" +
                        "           <chapters>"+
                        "               <c>001</c>"+
                        "               <c>002</c>"+
                        "               <c>003</c>"+
                        "           </chapters>"+
                        "    </book>" +
                        "    <book>" +
                        "        <name>Think in Java2</name>" +
                        "        <price>220.0</price>" +
                        "    </book>" +
                        "</books>";
            Document doc = DocumentHelper.parseText(str);
            Element books = doc.getRootElement();
            List<Element> childEles = books.elements();
            Iterator<Element> iter = childEles.iterator();
            while(iter.hasNext()){
                Element book = iter.next();
                
                Element name = book.element("name");
                Element price = book.element("price");
                
                System.out.println("name:"+name.getText()+",price:"+price.getText());
                
                Element chapters = book.element("chapters");
                if(null!=chapters){
                    Iterator<Element> chaptersIter= chapters.elementIterator();
                    if(null!=chaptersIter){
                        while(chaptersIter.hasNext()){
                            Element c = chaptersIter.next();
                            System.out.println("===>"+c.getText());
                        }
                    }
                }
                
            }
        }
        
        public static void test2() throws DocumentException{
            String str = "<?xml version="1.0" encoding="UTF-8"?>" +
                        "<books>" +
                        "    <book>" +
                        "        <name>Think in Java</name>" +
                        "        <price>120.0</price>" +
                        "    </book>" +
                        "    <book>" +
                        "        <name>Think in Java2</name>" +
                        "        <price>220.0</price>" +
                        "    </book>" +
                        "</books>";
            
            Document doc = DocumentHelper.parseText(str);
            
            Element books = doc.getRootElement();
            
            List<Element> childEles = books.elements();
            Iterator<Element> iter = childEles.iterator();
            while(iter.hasNext()){
                Element book = iter.next();
                
                Element name = book.element("name");
                Element price = book.element("price");
                
                System.out.println("name:"+name.getText()+",price:"+price.getText());
            }
        }
        
        public static void test1() throws DocumentException{
            String str = "<?xml version="1.0" encoding="UTF-8"?>" +
                        "<dzswdjz>" +
                        "    <qr_code>" +
                        "   <nsrsbh>nsrsbh</nsrsbh >" +
                        "   <retStatus >retStatus</retStatus>" +
                        "   <img_name>img_name</img_name>" +
                        "      <img_byteString>img_byteString</img_byteString>" +
                        "   </qr_code>" +
                        "</dzswdjz>";
            Document doc = DocumentHelper.parseText(str);
            
            //获取到父节点
            Element dzswdjz = doc.getRootElement();
            
           //定位到qr_code节点
            Element qr_code = dzswdjz.element("qr_code");
            
            Element nsrsbh = qr_code.element("nsrsbh");
            Element retStatus = qr_code.element("retStatus");
            Element img_name = qr_code.element("img_name");
            Element img_byteString = qr_code.element("img_byteString");
            
            System.out.println("nsrsbh:"+nsrsbh.getText());
            System.out.println("retStatus:"+retStatus.getText());
            System.out.println("img_name:"+img_name.getText());
            System.out.println("img_byteString:"+img_byteString.getText());
        }
    }
  • 相关阅读:
    程序员这生必须掌握的两种图形
    用一张组织架构图说清楚类和对象
    简单工厂、工厂方法、抽象工厂的比较与分析
    rabbitmq系列(一)初识rabbitmq
    【最新】经典面试100问,附答案
    使用wordPress搭建个人博客
    调试接口你还在用postman吗
    Token ,Cookie、Session傻傻分不清楚?
    你不可不知的自定义注解
    使用aop加解密http接口
  • 原文地址:https://www.cnblogs.com/yshyee/p/4353782.html
Copyright © 2020-2023  润新知