在系统之间交互传输的数据大都采用XML格式,在接到XML格式的字符串时我们需要将字符串转化为我们需要的格式(大多是对象),这时就需要解析XML,Java中用dom4j插件比较多。这里先进行简单的学习下。

xml格式的字符串转换为文档,并保存为文件

主要步骤如下:

1)XML字符串通过DocumentHelper.parseText()方法转换为Document对象

2)通过XMLWriter对象的write()方法生成xml文档

package test;


import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;


import org.dom4j.Document;

import org.dom4j.DocumentException;

import org.dom4j.DocumentHelper;

import org.dom4j.io.OutputFormat;

import org.dom4j.io.XMLWriter;


public class XMLParse {

    public static void main(String[]args) throws DocumentException {

        String xmlStr=""+

               ""+

                   ""+

                       "Java编程思想"+

                       "埃克尔"+

                       "机械工业出版社"+

                       "108"+

                   ""+

                   ""+

                       "数据结构与算法分析"+

                       "韦斯"+

                       "机械工业出版社"+

                       "55"+

                   ""+

               "";

       createDocumentFile(xmlStr,"book.xml");

    }


    /**

     * 功能描述: 生成xml文档

     * 〈功能详细描述〉

     *

     * @param document

     * @param fileName

     */

    /**

     * 功能描述: 生成xml文档

     * 〈功能详细描述〉

     *

     * @param xmlStr xml格式的字符串

     * @param fileName 生产文档的文件名

     */

    public static voidcreateDocumentFile(String xmlStr,String fileName){

        try {

            //XML字符串转换为Document对象

            Document document =DocumentHelper.parseText(xmlStr);

            //带有换行符的输出格式

            OutputFormat format =OutputFormat.createPrettyPrint();

            OutputStream out= newFileOutputStream(fileName);

            XMLWriter writer = newXMLWriter(out, format);

            //保存到本地文件

            writer.write(document);

           System.out.println("write end");

        } catch (DocumentExceptione) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }


}

运行结果:

DOM4j学习(一)_第1张图片

DOM4j学习(一)_第2张图片


XML格式的字符串转换为Java对象

package bean;

 

public class Book {

   private String name;

   private String author;

   private String publisher;

   private Double price;

 

   public Book() {

       super();

    }

 

    publicBook(String name, String author, String publisher, Double price) {

       super();

       this.name = name;

       this.author = author;

       this.publisher = publisher;

       this.price = price;

    }

   @Override

   public String toString() {

       return "Book [name=" + name + ", author=" + author +", publisher=" + publisher + ", price=" + price +"]";

    }

 

}

 

 publicclass XMLParse {

   public static void main(String[] args) throws DocumentException {

       String xmlStr =""+

                ""+

                    ""+

                        "Java编程思想"+

                       "埃克尔"+

                        "机械工业出版社"+

                       "108"+

                    ""+

                    ""+

                        "数据结构与算法分析"+

                        "韦斯"+

                        "机械工业出版社"+

                       "55"+

                    ""+

                "";

       List bookList =getBooksFromXmlStr(xmlStr);

       System.out.println(bookList);

    }

 

   @SuppressWarnings("unchecked")

   private static List getBooksFromXmlStr(String xmlStr) {

       List bookList = new ArrayList();

       try {

           Document document = DocumentHelper.parseText(xmlStr);

           List bookEles =document.selectNodes("//books/book");

           for (Element bookEle : bookEles) {

                String name =bookEle.elementText("name");

                String author =bookEle.elementText("author");

                String publisher =bookEle.elementText("publisher");

                Double price =Double.parseDouble(bookEle.elementText("price"));

                Book book = new Book(name,author, publisher, price);

                bookList.add(book);

           }

       } catch (DocumentException e) {

           e.printStackTrace();

       }

       return bookList;

}

}

运行结果:

wKiom1Sjpm6hykEpAAEmNr68EZQ329.jpg