dom4j 解析 XML

dom4j 解析 XML

使用 domj4 API 创建与修改 XML 文档

创建xml文档

例:public static void main(String[] args) throws IOException {

        // documentHelpercreateDocument()方法创建document对象

        Document document = DocumentHelper.createDocument();

        // 增加一个根节点

        Element root = document.addElement("books");

        // 添加4个元素

        //根元素

        Element book = root.addElement("book");

        book.addAttribute("ss", "123");

        //子元素

        Element name = book.addElement("name");

        name.setText("java开发");

        //子元素

        Element author = book.addElement("author");

        author.setText("kou_xiao_lin");

        //子元素

        Element price = book.addElement("price");

        price.setText("200");

        //设置输出格式

        OutputFormat format=new OutputFormat("       ",true,"utf-8");

        XMLWriter xw=new XMLWriter(new FileWriter("src//bk.xml"),format);

        xw.write(document);

        xw.close();

}运行效果如下:

<?xml version="1.0" encoding="utf-8"?>

 

<books>

                  <book ss="123">

                      <name>java开发</name>

                      <author>kou_xiao_lin</author>

                      <price>200</price>

                  </book>

</books>

解析元素和元素的属性

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE books[

    <!ELEMENT books (book*)>

    <!ELEMENT book (name,author,price)>

    <!ELEMENT name (#PCDATA)>

    <!ELEMENT author (#PCDATA)>

    <!ELEMENT price (#PCDATA)>

    <!ATTLIST author address CDATA #IMPLIED>

]>

<books>

   <book>

       <name>Java开发</name>

       <author>m_j</author>

       <price>10000</price>

   </book>

   <book>

       <name>Java开发1</name>

       <author address="保定 何软 ">m_j1</author>

       <price>10000</price>

   </book>

   <book>

       <name>Java开发</name>

       <author>m_j2</author>

       <price>10000</price>

   </book>

   <book>

       <name>Java开发</name>

       <author>m_j3</author>

       <price>10000</price>

   </book>

</books>

解析如下:

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

        // 读取器

        SAXReader reader = new SAXReader();

        // 读取文件

        Document doc = reader.read(new File("src//book.xml"));

        // 获取根结点

        Element root = doc.getRootElement();

        // 输出根结点的名字

        System.out.println(root.getName());

        parse(root);

    }

    /*

     * //解析元素

     *  public static void parse(Element root){

     *  List<Element>list=root.elements();

     *  for(Element element:list){

     *        if(element.isTextOnly()){

     *                parseAttr(element);

     *                System.out.println("----"+element.getName());

     *                System.out.println("----------"+element.getText());

     *        }else{

     *               parse(element);

     *               }

     *         }

     * }

     *  //解析元素的属性

     *  public static void parseAttr(Element root){

     *        List<Attribute> attr=root.attributes();

     *        for(Attribute attribute:attr){

     *              System.out.println(attribute.getName()+"--------"

*                 +attribute.getValue());

     *        }

     *  }

     */

    // 解析元素  public static void parse(Element root) {

        parseAttr(root);

 

        for (Iterator<Element> it = root.elementIterator(); it.hasNext();) {

            Element element = it.next();

            // 判断是否只是文本

            if (element.isTextOnly()) {

                parseAttr(element);

                System.out.println("----" + element.getName());

                System.out.println("----------" + element.getText());

            } else {

                parse(element);

            }

        }

    }

    // 解析元素的属性

    private static void parseAttr(Element root) {

       for (Iterator<Attribute> it = root.attributeIterator(); it.hasNext();) {

            Attribute entity = it.next();

            System.out.println(entity.getName() + "--------"

                    + entity.getValue());

        }

    }

}

/*

 * 运行结果如下:

* books

*----name

* ----------Java开发

* ----author

* ----------m_j

* ----price

 * ----------10000

* ----name

* ----------Java开发1 address--------保定 何软

 * ----author

*----------m_j1

*----price

*----------10000

*----name

*----------Java开发

 * ----author

*----------m_j2

*----price

*----------10000

*----name

*----------Java开发

 * ----author

*----------m_j3

*----price

*----------10000

 */

增加元素

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

        // 获取读取器

        SAXReader reader = new SAXReader();

        // 读文件

        Document doc = reader.read(new File("src//bk.xml"));

        // 获取根元素

        Element root = doc.getRootElement();

        // 创建add元素并设置它的文本

        Element address = DocumentHelper.createElement("address");

        address.setText("保定");

        //获取book下的所有元素

        List list = root.element("book").elements();

        list.add(2, address);

        OutputFormat format = new OutputFormat("      ", true, "UTF-8");

        XMLWriter xw = new XMLWriter(new FileWriter("src//bk.xml"), format);

        xw.write(doc);

        xw.close();

    }

在指定的位置增加元素

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

        // 获取读取器

        SAXReader reader = new SAXReader();

        // 读文件

        Document doc = reader.read(new File("src//bk.xml"));

        // 获取根元素

        Element root = doc.getRootElement();

        // 创建add元素并设置它的文本

        Element address = DocumentHelper.createElement("address");

        address.setText("衡水");

        //获取book下的所有元素

        Element book=(Element)root.elements("book").get(2);

        List list =book.elements();

        list.add(2, address);

        OutputFormat format = new OutputFormat("      ", true, "UTF-8");

        XMLWriter xw = new XMLWriter(new FileWriter("src//bk.xml"), format);

        xw.write(doc);

        xw.close();

    }

删除元素

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

        // 获取读取器

        SAXReader reader = new SAXReader();

        // 读文件

        Document doc = reader.read(new File("src//bk.xml"));

        // 获取根元素

        Element root = doc.getRootElement();

        // 创建add元素并设置它的文本

        //获取book下的所有元素

        Element book=(Element)root.elements("book").get(2);

        Element ww=(Element) book.elements().get(1);

        book.remove(ww);

        OutputFormat format = new OutputFormat("      ", true, "UTF-8");

        XMLWriter xw = new XMLWriter(new FileWriter("src//bk.xml"), format);

        xw.write(doc);

        xw.close();

    }

你可能感兴趣的:(exception,list,String,File,encoding,books)