一、SAX解析

1、将要解析的xml文件为TestDom.xml

<?xml version="1.0" encoding="utf-8"?>
<bookStore>
    <book id="1" class="tonghua">
        <name>安徒生童话</name>
        <author>安徒生</author>
        <year>2008</year>
    </book>
    <book id="2" class="xiyouji">
        <name>西游记</name>
        <author>罗贯中</author>
    </book>
</bookStore>

2、Book类

package youth.hong.TestDom;

public class Book {
    private String id = "";
    private String className = "";
    private String name = "";
    private String author = "";
    private String year = "";
    public Book(String id, String name, String author, String year) {
        super();
        this.id = id;
        this.name = name;
        this.author = author;
        this.year = year;
    }
    
    public Book() {
        
    }
    

    @Override
    public String toString() {
        return "Book [id=" + id + ", className=" + className + ", name=" + name + ", author=" + author + ", year="
                + year + "]";
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }
    
    
    
}

 

3、解析xml的主类

package youth.hong.Handler;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import youth.hong.TestDom.Book;

public class SAXHandler extends DefaultHandler{
    int bookIndex = 0;
    private List<Book> bookList = new ArrayList<Book>();
    private Book book = null;
    private String value = null;
    //用来标志解析开始
    @Override
    public void startDocument() throws SAXException {
        
        System.out.println("解析开始");
    }
    
    //用来标志解析结束
    @Override
    public void endDocument() throws SAXException {
        System.out.println("解析结束");
    }
    
    //用来遍历元素
    //开始标签
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        
        
        if(qName.equals("book")) {
            bookIndex++;
            book = new Book();
            System.out.println("============================第" + bookIndex + "本书开始了=========================");
            //已知book下属性元素的名称
            //System.out.println(attributes.getValue("id"));
            for(int i = 0; i < attributes.getLength(); i++) {
                String name = attributes.getQName(i);
                if(name.equals("id")) {
                    book.setId(attributes.getValue(i));
                }else if(name.equals("class")) {
                    book.setClassName(attributes.getValue(i));
                }
                book.setId(attributes.getValue(i));
                //System.out.println(name);
                System.out.println("第" + bookIndex + "本书的" + attributes.getQName(i) + "是:" + attributes.getValue(i)/*attributes.getValue(Qname)*/);
            }
        }else if(!qName.equals("bookStore")) {
            System.out.print("第" + bookIndex + "本书的" + qName + "是:");
        }
        
    }
    //遍历标签内的内容
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        value = new String(ch,start,length);
        //如果标签下没有内容,如bookstore与book之间就没有内容,将不打印
        if(!value.trim().equals("")) {
            System.out.println(value);

        }
        
    }
    
    //遍历元素的结束标签
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        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);
        }
        
    }
    
    

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

4、测试类(包括创建xml文件)

package youth.hong.TestDom;

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

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;

import org.xml.sax.helpers.AttributesImpl;

import youth.hong.Handler.SAXHandler;

public class SAXTest {
    
    public List<Book> parseXml() {
        SAXHandler saxhandler = null;
        SAXParserFactory parserFactory = SAXParserFactory.newInstance();
        try {
            SAXParser saxParser = parserFactory.newSAXParser();
            saxhandler = new SAXHandler();
            saxParser.parse("src/TestJDom/TestDom.xml", saxhandler);
            for (Book book : saxhandler.getBookList()) {
                System.out.println(book);
            }
            System.out.println();
        } catch (Exception e) {
            e.printStackTrace();
        };
        return saxhandler.getBookList();
        
    }
    /**
     * 生成xml文件
     */
    public void createXml() {
        List<Book> bookList = this.parseXml();
        //转化工厂
        SAXTransformerFactory tff = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
        try {
            TransformerHandler handler = tff.newTransformerHandler();
            //转换器
            Transformer tf = handler.getTransformer();
            //设置输出编码为UTF-8
            tf.setOutputProperty(OutputKeys.ENCODING, "utf-8");
            //输出格式缩进
            tf.setOutputProperty(OutputKeys.INDENT, "yes");
            //要输出的文件
            File file = new File("src/TestJDom/newBook.xml");
            //如果文件不存在就创建一个
            if(!file.exists()) {
                file.createNewFile();
            }
            Result result = new StreamResult(new FileOutputStream(file));
            //必须在result定义完再加入内容才有效
            handler.setResult(result);
            handler.startDocument();
            AttributesImpl atts = new AttributesImpl();
            handler.startElement("", "", "bookStore", atts);
            //遍历所有书籍将他们加入到xml中
            for(Book book : bookList) {
                atts.clear();
                /**
                 * public void addAttribute(String uri,
                         String localName,命名空间本地名
                         String qName,元素名
                         String type,元素类型
                         String value)值
                 */
                atts.addAttribute("", "", "id", null, book.getId());
                atts.addAttribute("", "", "class", null, book.getClassName());
                //注意按照SAX模式解析xml文件的顺序去定义标签
                handler.startElement("", "", "book", atts);
                atts.clear();
                handler.startElement("", "", "name", atts);
                char[] ch = book.getName().toCharArray();
                handler.characters(ch, 0, ch.length);
                handler.endElement("", "", "name");
                
                handler.startElement("", "", "author", atts);
                ch = book.getAuthor().toCharArray();
                handler.characters(ch, 0, ch.length);
                handler.endElement("", "", "author");
                
                handler.startElement("", "", "year", atts);
                ch = book.getYear().toCharArray();
                handler.characters(ch, 0, ch.length);
                handler.endElement("", "", "year");
                
                handler.endElement("", "", "book");
            }
            
            handler.endElement("", "", "bookStore");
            handler.endDocument();
            
        } catch (Exception e) {
            e.printStackTrace();
        } 
        
    }
    
    public static void main(String[] args) {
        SAXTest saxTest = new SAXTest();
        
        //saxTest.createXml();
        saxTest.parseXml();
    }
}

5、生成newBook.xml文件

<?xml version="1.0" encoding="utf-8"?><bookStore>
<book id="tonghua" class="tonghua">
<name>安徒生童话</name>
<author>安徒生</author>
<year>2008</year>
</book>
<book id="xiyouji" class="xiyouji">
<name>西游记</name>
<author>罗贯中</author>
<year/>
</book>
</bookStore>

 

二、jdom解析

 1、解析与创建

package youth.hong.TestJDom;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

import youth.hong.TestDom.Book;

public class TestJDom {

    private static List<Book> bookList = new ArrayList<Book>();
    
    public void parseXML() {
        SAXBuilder saxBuilder = new SAXBuilder();
        try {
            Document document = saxBuilder
                    .build(new InputStreamReader(new FileInputStream("src/TestJDom/TestDom.xml"),"utf-8"));
            /*Document document2 = saxBuilder
                    .build(new FileReader("src/TestJDom/TestDom.xml"));*///没有字符编码,所以最好使用上面那种
            Element element = document.getRootElement();
            int i = 0;
            // elment.getAttributeValue()返回一个list对象
            List<Element> bookList = element.getChildren();
            // 遍历每个book的属性
            for (Element book : bookList) {
                Book bookEntity = new Book();
                i++;
                System.out.println("======================第" + i + "开始==========================");
                // 已知属性名
                // System.out.println(book.getAttributeValue("class"));
                List<Attribute> attrList = book.getAttributes();
                for (Attribute attribute : attrList) {
                    System.out.println("属性名称:" + attribute.getName() + "----属性值是:" + attribute.getValue());
                    if(attribute.getName().equals("id")) {
                        bookEntity.setId(attribute.getValue());
                    }
                    else if(attribute.getName().equals("class")) {
                        bookEntity.setClassName(attribute.getValue());
                    }
                }
                List<Element> children = book.getChildren();
                for (Element element2 : children) {
                    System.out.println("节点名--" + element2.getName() + "--节点值--" + element2.getValue());
                    if(element2.getName().equals("name")) {
                        bookEntity.setName(element2.getValue());
                    }
                    else if(element2.getName().equals("author")) {
                        bookEntity.setAuthor(element2.getValue());
                    }
                    else if(element2.getName().equals("year")) {
                        bookEntity.setYear(element2.getValue());
                    }
                }
                System.out.println("======================第" + i + "结束==========================");
                TestJDom.bookList.add(bookEntity);
                bookEntity = null;
                
                
            }
//            System.out.println(TestJDom.bookList.size());
        } catch (JDOMException | IOException e) {

            e.printStackTrace();
        }

    }
    
    public void createXML() {
        //创建一个root节点rss
        Element rss = new Element("rss");
        //定义version属性
        rss.setAttribute("version","2.0");
        //创建document
        Document document = new Document(rss);
        
        Element channel = new Element("channel");
        rss.addContent(channel);
        Element title = new Element("title");
        
        channel.addContent(title);
        
        title.setText("<java你好!!!>");
        //title.addContent(new CDATA("<!--java你好!!!-->"));
        Format format = Format.getCompactFormat();
        format.setIndent("");
        format.setEncoding("gbk");
        XMLOutputter outputer = new XMLOutputter(format);
        try {
            
            outputer.output(document, new FileOutputStream("src/TestJDom/JDomnews.xml"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        TestJDom jd = new TestJDom();
        jd.parseXML();
        jd.createXML();
    }

}

2、Jdomnews.xml

<?xml version="1.0" encoding="gbk"?>
<rss version="2.0">
<channel>
<title>&lt;java你好!!!&gt;</title>
</channel>
</rss>

 

三、dom4j解析

1、解析与创建

package youth.hong.TestDom4J;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import youth.hong.TestDom.Book;

public class Dom4JTest {
    
    private static List<Book> bookList = new ArrayList<Book>();
    //dom4j解析xml文档
    public void parseXML() {
        SAXReader reader = new SAXReader();
        try {
            Document document = reader.read(new File("src/TestJDom/TestDom.xml"));
            System.out.println(new File("src/TestJDom/TestDom.xml"));
            Element bookStore = document.getRootElement();
            Iterator<Element> it = bookStore.elementIterator();
            while(it.hasNext()) {
                System.out.println("===================某本书开始==================");
                Book bookEntity = new Book();
                Element book = it.next();
                List<Attribute> attrList = book.attributes();
                System.out.println("当前结点下节点的个数(包括空白的文本节点)" + book.nodeCount());
                for(Attribute attribute : attrList) {
                    System.out.println("属性名:" + attribute.getName() + "属性值:" + attribute.getValue());
                    if(attribute.getName().equals("id")) {
                        bookEntity.setId(attribute.getValue());
                    }
                    else if(attribute.getName().equals("class")) {
                        bookEntity.setClassName(attribute.getValue());
                    }
                }
                Iterator<Element> itt = book.elementIterator();
                while(itt.hasNext()) {
                    Element childNode = itt.next();
                    System.out.println("节点名:" + childNode.getName() + "节点值:" + childNode.getStringValue());
                    if(childNode.getName().equals("name")) {
                        bookEntity.setName(childNode.getStringValue());
                    }
                    else if(childNode.getName().equals("author")) {
                        bookEntity.setAuthor(childNode.getStringValue());
                    }
                    else if(childNode.getName().equals("year")) {
                        bookEntity.setYear(childNode.getStringValue());
                    }
                    
                }
                bookList.add(bookEntity);
                bookEntity = null;
                System.out.println("===================某本书结束==================");
                System.out.println(bookList.size());
            }
        } catch (DocumentException e) {
            
            e.printStackTrace();
        }
        
    }
    
    //生成XML文档
    
    public void createXML() {
        Document document = DocumentHelper.createDocument();
        Element rss = document.addElement("rss");
        rss.addAttribute("version", "2.0");
        Element channel = rss.addElement("channel");
        Element title = channel.addElement("title");
        title.setText("<!--新闻要点-->");
        Element image = channel.addElement("image");
        Element imageTitle = image.addElement("title");
        imageTitle.setText("news.baidu.com");
        Element link = image.addElement("link");
        link.setText("http://news.baidu.com");
        Element url = image.addElement("url");
        url.setText("http://img.baidu.com/img/logo-news.gif");
        File file = new File("src/TestJDom/news.xml");
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("GBK");
        try {
            XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);
            writer.setEscapeText(false);
            writer.write(document);
        } catch (UnsupportedEncodingException | FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Dom4JTest d4j = new Dom4JTest();
        d4j.parseXML();
        d4j.createXML();

    }

}

2、创建的news.xml

<?xml version="1.0" encoding="GBK"?>

<rss version="2.0">
  <channel>
    <title><!--新闻要点--></title>
    <image>
      <title>news.baidu.com</title>
      <link>http://news.baidu.com</link>
      <url>http://img.baidu.com/img/logo-news.gif</url>
    </image>
  </channel>
</rss>

 四、dom解析

1、解析与创建

package youth.hong.TestDom;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class TestDom {
    
    private static List<Book> bookSuffle = new ArrayList<Book>();
    
    /**
     * 获取DocumentBuilder对象
     */
    
    public DocumentBuilder getDocumentBuilder() {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = null;
        try {
            documentBuilder = dbf.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return documentBuilder;
    }
    
    /**
     * dom方法解析Xml文件
     */
    public void domParse() {
        try {
            Document document = getDocumentBuilder().parse("src/TestJDom/TestDom.xml");
            NodeList bookList = document.getElementsByTagName("book");
            for(int i = 0; i < bookList.getLength(); i++) {
                String id = null;
                String name = null;
                String author = null;
                String year = null;
                //在不知道属性名称的情况下使用
                /*Node book = bookList.item(i);
                NamedNodeMap attrs = book.getAttributes();
                for(int j = 0; j < attrs.getLength(); j++) {
                    Node attr = attrs.item(j);
                    System.out.println(attr.getNodeName() + "  " + attr.getNodeValue());
                }*/
                
                //在知道属性名称的情况下使用
                /*Element book = (Element)bookList.item(i);
                String id = book.getAttribute("id");
                String className = book.getAttribute("class"); 
                System.out.println(id + "  " + className);
                */
                Node book = bookList.item(i);
                Element bookElement = (Element)book;
                id = bookElement.getAttribute("id");
                NodeList childNodes = book.getChildNodes();
                System.out.println(childNodes.getLength());
                for(int k = 0; k < childNodes.getLength(); k++) {
                    Node childNode = childNodes.item(k);
                    if(childNode.getNodeType() == Node.ELEMENT_NODE) {
                        String value = childNode.getTextContent();
                        String nodeName = childNode.getNodeName();
                        switch(nodeName) {
                        case "name" : name = value; value = null; break;
                        case "author" : author = value; value = null; break;
                        case "year" :  year = value; value = null; break;
                        }
                        //System.out.println(childNode.getNodeName() + "  " + childNode.getFirstChild().getNodeValue());
                        
                    }
                    
                }
                bookSuffle.add(new Book(id,name,author,year));
                //System.out.println();
            }
            for(int i = 0; i < bookSuffle.size(); i++) {
                System.out.println(bookSuffle.get(i));
            }
        }  catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    
    /**
     * 生成Xml文件,要创建多本书可以通过for循环来生成,但内容应当定义一个book的类去存储内容
     * @param args
     */
    
    public void creatXml() {
        DocumentBuilder documentBuilder = this.getDocumentBuilder();
        //生成document
        Document document = documentBuilder.newDocument();
        //去掉standalone属性
        document.setXmlStandalone(true);
        //创建bookStore节点
        Element bookStore = document.createElement("bookStore");
        //创建book节点
        Element book1 = document.createElement("book");
        Element book2 = document.createElement("book");
        Element name1 = document.createElement("name");
        Element author1 = document.createElement("author");
        Element year1 = document.createElement("year");
        Element price1 = document.createElement("price");
        Element name2 = document.createElement("name");
        Element author2 = document.createElement("author");
        Element year2 = document.createElement("year");
        Element price2 = document.createElement("price");
        /*//创建文本节点
        Node nameText = document.createTextNode("七龙珠");
        //添加文本节点
        name.appendChild(nameText);*/
        name1.setTextContent("犬夜叉");
        year1.setTextContent("2008");
        author1.setTextContent("高桥留美子");
        price1.setTextContent("200$");
        book1.appendChild(name1);
        book1.appendChild(author1);
        book1.appendChild(year1);
        book1.appendChild(price1);
        //为book节点添加属性id
        book1.setAttribute("id", "1");
        //把book节点加入到bookStore节点中
        bookStore.appendChild(book1);
        name2.setTextContent("唐人街探案");
        year2.setTextContent("2016");
        author2.setTextContent("陈思成");
        price2.setTextContent("VIP会员免费");
        book2.appendChild(name2);
        book2.appendChild(author2);
        book2.appendChild(year2);
        book2.appendChild(price2);
        //添加第二个book
        bookStore.appendChild(book2);
        //把节点bookStore加入到document中
        document.appendChild(bookStore);
        //使用TransformerFactory
        TransformerFactory factory = TransformerFactory.newInstance();
        try {
            Transformer transformer = factory.newTransformer();
            //输入节点之间的换行
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            //传入document,输出流生成xml文件
            transformer.transform(new DOMSource(document), new StreamResult(new File("bookCreate.xml")));
        } catch (TransformerConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (TransformerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    /**
     * 主程序的入口
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        TestDom testDom = new TestDom();
        testDom.domParse();
        testDom.creatXml();
    }
}

2、生成的bookCreate.xml

<?xml version="1.0" encoding="UTF-8"?><bookStore>
<book id="1">
<name>犬夜叉</name>
<author>高桥留美子</author>
<year>2008</year>
<price>200$</price>
</book>
<book>
<name>唐人街探案</name>
<author>陈思成</author>
<year>2016</year>
<price>VIP会员免费</price>
</book>
</bookStore>