• 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());
        }
    }
  • 相关阅读:
    字符串-01. 在字符串中查找指定字符(15)
    数组-14. 数字加密(15)
    软考笔记第一天之数制
    基于c#开发的简易点名器
    软考笔记之存储管理
    IO inputStream和outputStream
    java可变参数
    Map集合总结
    Collection集合总结
    Struts 获得前台传参几种方式
  • 原文地址:https://www.cnblogs.com/yshyee/p/4353782.html
Copyright © 2020-2023  润新知