java里面4种关于xml的解析方法

以下资料来源于 imooc
imooc学习资料:http://www.imooc.com/learn/171

推荐大家使用第四种方法;;;
4中关于解析xml的方法:
DOM,SAX ( java 里面提供包 )

JDOM,DOM4J ( 需要自己导入包文件 )

1:DOM

package com.sdingba.xmlparser;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.List;
/** * Created by su on 16-4-30. * * DOM解析 xml文件 * * 一次性加载内存 * 优点: * 形成树的结构,直观好理解,代码更容易编写 * 解析过程中树结构保留在内存中,方便修改 * * 缺点: * 当xml文件较大时,对内存的消耗不比较大, * 容易影响解析性能,并赵成内存溢出 */
public class DOM {

    public static void main(String[] args) {
        //创建一个DocumentBuilderFactory对象
        DocumentBuilderFactory dbf =
                DocumentBuilderFactory.newInstance();


        //创建一个DocumentBuilder对象
        try {
            DocumentBuilder db = dbf.newDocumentBuilder();

            //通过DocumentBuilder对象的parse解析,加载xml文件
            Document document = db.parse("new.xml");

            //获取所有book的集合
            NodeList bookList = document.getElementsByTagName("book");

            for (int i=0;i<bookList.getLength();i++) {
//                通过  获取一个book的数据
                Node books = (Node) bookList.item(i);
                //获取book节点的所有属性的集合
                NamedNodeMap attrs = books.getAttributes();
                //遍历book的属性,
                for (int j = 0; j < attrs.getLength(); j++) {
                    //通过item获取某一个属性
                    attrs.item(j).getNodeName();
                    //获取属性值  getNodeValue();
                    System.out.println("id == "+attrs.item(j).getNodeValue());
                }

//                //前提book节点 有且只有一个:
//                Element book = (Element) bookList.item(i);
//                 //sttrValue 为 book里面的id值
//                String attrValue = book.getAttribute("id");
//                System.out.println(attrValue);

                NodeList chileNodes = books.getChildNodes();
                //遍历chileNode的节点和节点名
                for (int k = 0; k < chileNodes.getLength(); k++) {
                    //区分text类型的node以及element的类型
                    if (chileNodes.item(k).getNodeType() == Node.ELEMENT_NODE) {
                        //获取的element类型的节点的节点名
                        String aa = chileNodes.item(k).getNodeName();
                        System.out.print(aa+" ===== : ");
                        //bb2   为空;
                        String bb2 = chileNodes.item(k).getNodeValue();

                        String bb = chileNodes.item(k).getFirstChild().getNodeValue();
//                        String bb = chileNodes.item(k).getTextContent();
                        System.out.println(bb);
                    }
                }
            }
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2,SAX

package com.sdingba.xmlparser;

import com.sdingba.javabean.Book;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import com.sun.xml.internal.bind.v2.model.core.ID;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
import java.util.ArrayList;

/** * Created by su on 16-4-30. * Handler 解析原理 * * 基于事件的解析,一条一条的解析 * * 优点: * 采用事件驱动模式,对内存的消耗比较小 * 适合于只需要处理xml数据时 * 缺点,不易编码 * 很难同时访问一个xml中的多个不同的数据 */
public class SAX {
    public static void main(String[] args) {

        //1.通过SAXparserFactory 的实例
        SAXParserFactory factory = SAXParserFactory.newInstance();

        try {
            //通过factory获取SAXParser的实例
            SAXParser parser = factory.newSAXParser();

            SAXParserHandler handler = new SAXParserHandler();
            parser.parse("new.xml", handler);

            int a = handler.getBookList().size();
            for (Book book : handler.getBookList()) {
                System.out.println("id = "+book.getId());
                System.out.println(book.getName());
                System.out.println(book.getAuthor());
                System.out.println(book.getYear());
                System.out.println(book.getPrice());
                System.out.println(book.getLanguage());
                System.out.println("-------finish-------");
            }

        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    static class SAXParserHandler extends DefaultHandler {
        Book book;

        public ArrayList<Book> getBookList() {
            return bookList;
        }

        public void setBookList(ArrayList<Book> bookList) {
            this.bookList = bookList;
        }

        private  ArrayList<Book> bookList = new ArrayList<Book>();
        String value = null;
        private int bookIndex = 0;

        /** * 解析开始 * * @throws SAXException */
        @Override
        public void startDocument() throws SAXException {
            super.startDocument();
            System.out.println("解析开始");
        }

        /** * 解析结束 * * @throws SAXException */
        @Override
        public void endDocument() throws SAXException {
            super.endDocument();
            System.out.println("解析结束");
        }


        /** * 用来遍历xml文件的开始标签 * * @throws SAXException */
        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
            super.startElement(uri, localName, qName, attributes);
//
            if (qName.equals("book")) {
                bookIndex++;
                book = new Book();
                System.out.println("开始第" + bookIndex + "本书的解析======");
                //以知 book元素的属性
                String value = attributes.getValue("id");
                System.out.println("book+" + value);//book+2

                //如果不知道book元素的属性值
                int num = attributes.getLength();
                for (int i = 0; i < num; i++) {
                    System.out.println(attributes.getQName(i));//属性名 id
                    System.out.println("属性值“===" + attributes.getValue(i));
                    if (attributes.getQName(i).equals("id")) {
                        book.setId(attributes.getValue(i));
                    }
                }
            } else if (!qName.equals("book") && !qName.equals("bookstore")) {
                System.out.println("节点名”" + qName);

            }
        }

        /** * 用来遍历xml文件的结束标签 * * @throws SAXException */
        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            //调用DefaultHandler类的startElement方法;
            super.endElement(uri, localName, qName);
            //判断是否针对一本书进行遍历
            if (qName.equals("book")) {
                bookList.add(book);
                book = null;
                System.out.println("=====结束" + bookIndex + "的解析");
            } else if (qName.equals("name")) {
                book.setName(value);
            } else if (qName.equals("author")) {
                book.setAuthor(value);
            } else if (qName.equals("year")) {
                book.setYear(value);
            } else if (qName.equals("price")) {
                book.setPrice(value);
            } else if (qName.equals("language")) {
                book.setLanguage(value);
            }

        }


        /** * @param ch ,,整个共享xml文档的内容 * start 开始节点 * length 长度 */
        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            super.characters(ch, start, length);
            value = new String(ch, start, length);
            if (!value.trim().equals("")) {
                System.out.println("节点值是::" + value);
            }
        }
    }

}

3,JDOM

package com.sdingba.xmlparser;

import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/** * Created by su on 16-5-1. * 不是java官方提高的,。需要导入jar包 * * * */
public class JDOM {

    public static void main(String[] args) {

        //进行对new。xml文件的解析
        //1,
        SAXBuilder saxBuilder = new SAXBuilder();


        try {
            //2,创建一个输入流
            InputStream in = new FileInputStream("new.xml");
            //3,通过saxBuiler的build的方法,将输入流加载到sax里面
            Document document = saxBuilder.build(in);
            //通过decument对象,。获取xml文件的根节点
            Element rootElement = document.getRootElement();
            //获取根节点下的子节点,集合
            List<Element> booklist = rootElement.getChildren();
            //继续进行解析
            for (Element book : booklist) {
                System.out.println("开始解析第" +
                        booklist.indexOf(book) + 1 + "本书=====");

                //解析book的属性,
                List<Attribute> attrList = book.getAttributes();
                  /* //如果知道属性值的时候,可以选择下面这行代码获取属性值 String aaaa = book.getAttributeValue("id"); System.out.println(aaaa); //aaaa = 1,和下面的for循环里面的一样效果 */
                //遍历属性名,针对不清楚book下的属性值
                for (Attribute attribute : attrList) {
                    //获取属性名
                    String attrName = attribute.getName();
                    //获取属性值
                    String attrValue = attribute.getValue();
                    System.out.println(attrName + "---" + attrValue);

                }
                //对book节点的子节点的节点名以及节点值进行遍历
                List<Element> bookChilds = book.getChildren();
                for (Element chile : bookChilds) {
                    String name = chile.getName();
                    String value = chile.getValue();
                    System.out.print(name+" ---- ");
                    System.out.println(value);
                }
                //对book节点的子节点的节点名和接地安置
                System.out.println("结束解析第" +
                        booklist.indexOf(book) + 1 + "本书=====");
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

4,DOM4J

package com.sdingba.xmlparser;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;


import java.io.File;
import java.util.Iterator;
import java.util.List;

/** * Created by su on 16-5-1. * jdom的一种智能分支, * */
public class Dom4J {
    public static void main(String[] args) {
        //创建SAXReader的duixaing
        SAXReader reader = new SAXReader();
        //通富哦reader对象read
        try {
//          Document document = reader.read("new.xml");
            Document document = reader.read(new File("new.xml"));


            //通过document对象获取根节点
            Element bookStore = document.getRootElement();

           /* //如果你知道很多属性,只要求获取某些属性 String bookname = bookStore.element("book").element("name").getText(); System.out.println(bookname);//直接打印书名:你是我的女神 */
            //通过elementIterator方法获取迭代器
            Iterator<Element> it = bookStore.elementIterator();
            //遍历迭代器
            while (it.hasNext()) {
                System.out.println("======开始遍历莫一本书");
                Element book = (Element) it.next();
                //获取book的属性名和属性值
                List<Attribute> bookAttrs = book.attributes();
                for (Attribute attr : bookAttrs) {
                    String name = attr.getName();
                    String vale = attr.getValue();
                    System.out.println(name + "---" + vale);
                }
                Iterator iit = book.elementIterator();
                while (iit.hasNext()) {
                    Element bookClild = (Element) iit.next();
                    String value = bookClild.getStringValue();
                    String name = bookClild.getName();
                    System.out.println(name + "=---=" + value);
                }
                System.out.println("======结束遍历某一本书");
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
}

DOM4J 其他的方式:


     //创建SAXReader的duixaing
        SAXReader reader = new SAXReader();
        //通富哦reader对象read
        try {
//          Document document = reader.read("new.xml");
            Document document = reader.read(new File("new.xml"));


            //通过document对象获取根节点
            Element bookStore = document.getRootElement();
 List<Element> elementList = bookStore.elements();
            for (Element ele : elementList) {
                List<Element> elementss = ele.elements();
//                String value1 = ele.getText();
//                String value = ele.getStringValue();//
//                String name = ele.getName();
//                System.out.println(value);
                System.out.println();
                for (Element eles : elementss) {
                    String val = eles.getText();
                    String namenode = eles.getName();
                    System.out.println(namenode + " ==== " + val);
                }
            }

下面的xml文件是上面的四段代码解析的文件;

<?xml version="1.0" encoding="UTF-8" ?>
<bookstore>
    <book id="1">
        <name>你是我的女神</name>
        <author>sdingba</author>
        <year>2016</year>
        <price>89</price>
    </book>
    <book id="2">
        <name>你是我的女神2</name>
        <author>sdingba2</author>
        <year>2017</year>
        <price>88</price>
    </book>
</bookstore>

ssssssssssssssssssssssssssssssssssssssssssss

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

<书架> 
  <> 
    <书名>Java就业培训教程</书名>  
    <作者></作者>  
    <售价>39.00元</售价> 
  </>  
  <> 
    <书名>JavaScript网页开发</书名>  
    <作者></作者>  
    <售价>28.00元</售价> 
  </> 
</书架>
    public static void main(String[] args) throws Exception {
        //如果实在 web中
// 1, InputStream aaInputStream = request.getInputStream();
// 2, SAXReader reader = new SAXReader();
// 3, Document dom = reader.read(aaInputStream);
        //1.获取解析器
        SAXReader reader = new SAXReader();
        //2.解析xml获取代表整个文档的dom对象
        Document dom = reader.read("book.xml");

        //3.获取根节点
        Element root = dom.getRootElement();

        //4.获取书名进行打印
        String bookName = root.element("书").element("书名").getText();
        System.out.println(bookName);
    }
    @Test
    public void attr() throws Exception{
        SAXReader reader = new SAXReader();
        Document dom = reader.read("book.xml");
        Element root = dom.getRootElement();

        Element bookEle = root.element("书");
        //bookEle.addAttribute("出版社", "传智出版社");
//      String str = bookEle.attributeValue("出版社");
//      System.out.println(str);
        Attribute attr = bookEle.attribute("出版社");
        attr.getParent().remove(attr);

        XMLWriter writer = new XMLWriter(new FileOutputStream("book.xml"),OutputFormat.createPrettyPrint());
        writer.write(dom);
        writer.close();
    }

    @Test
    public void del() throws Exception{
        SAXReader reader = new SAXReader();
        Document dom = reader.read("book.xml");
        Element root = dom.getRootElement();

        Element price2Ele = root.element("书").element("特价");
        price2Ele.getParent().remove(price2Ele);

        XMLWriter writer = new XMLWriter(new FileOutputStream("book.xml"),OutputFormat.createPrettyPrint());
        writer.write(dom);
        writer.close();
    }

    @Test
    public void update()throws Exception{
        SAXReader reader = new SAXReader();
        Document dom = reader.read("book.xml");
        Element root = dom.getRootElement();

        root.element("书").element("特价").setText("4.0元");

        XMLWriter writer = new XMLWriter(new FileOutputStream("book.xml"),OutputFormat.createPrettyPrint());
        writer.write(dom);
        writer.close();
    }

    @Test
    public void add()throws Exception{
        SAXReader reader = new SAXReader();
        Document dom = reader.read("book.xml");
        Element root = dom.getRootElement();
        //凭空创建<特价>节点,设置标签体
        Element price2Ele = DocumentHelper.createElement("特价");
        price2Ele.setText("40.0元");
        //获取父标签<书>将特价节点挂载上去
        Element bookEle = root.element("书");
        bookEle.add(price2Ele);

        //将内存中的dom树会写到xml文件中,从而使xml中的数据进行更新
//      FileWriter writer = new FileWriter("book.xml");
//      dom.write(writer);
//      writer.flush();
//      writer.close();
        XMLWriter writer = new XMLWriter(new FileOutputStream("book.xml"),OutputFormat.createPrettyPrint());
        writer.write(dom);
        writer.close();
    }

    @Test
    public void find() throws Exception{
        SAXReader reader = new SAXReader();
        Document dom = reader.read("book.xml");
        Element root = dom.getRootElement();

        List<Element> list =  root.elements();
        Element book2Ele = list.get(1);
        System.out.println(book2Ele.element("书名").getText());

    }

你可能感兴趣的:(java)