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

    }

}

 

你可能感兴趣的:(dom4j)