码迷,mamicode.com
首页 > 其他好文 > 详细

dom4j解析xml字符串

时间:2015-03-20 17:48:02      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

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());
    }
}

 

dom4j解析xml字符串

标签:

原文地址:http://www.cnblogs.com/yshyee/p/4353782.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!