北京行——xml解析之SAX

1、xml解析有三个方式:

DOMdocument object Model):整个类型装入

SAXSimple API for XML):这个是边解析边装入,很好的解决了性能问题,典型的推模式,这个是服务器端为主导

STAXStreaming API for XML):这个也是边解析边装入的,不过这个是拉模式,就是说客户端为主导,(安卓 个人理解,减轻了客户端的性能消耗)。

用到的开发包:

JAXP(Java API for XML Processing )是 Sun 提供的一套XML解析API,包含在javase中。

DOM解析思想:

1、  获取文档建立的总工厂

2、  得到工厂后,获取文档建立者

3、  用文档建立者加载文档到内存,获取到文档对象

4、  用API中的方法进行操作xml

5、 public class DomParse {

6、 @Test

7、 public void test() throws ParserConfigurationException, SAXException,

8、         IOException {

9、     DocumentBuilderFactory builderFactory = DocumentBuilderFactory

10、                    .newInstance();// 拿到工厂

11、             DocumentBuilder builder = builderFactory.newDocumentBuilder();// 获取解析器

12、             Document document = builder.parse("books.xml");// 装载文档到内存

13、             NodeList nodeList = document.getElementsByTagName("name");// 通过元素名称来获取元素

14、             String s = null;

15、             for (int i = 0; i < nodeList.getLength(); i++) {// 遍历得到的节点

16、                 Element e = (Element) nodeList.item(i);

17、                 System.out.println("------------这里没有强制转换类型---------------");

18、                 System.out.println(nodeList.item(i).getNodeName());// 获取元素的名称

19、                 System.out.println(nodeList.item(i).getNodeValue());// 获取value的值

20、                 System.out.println(nodeList.item(i).getTextContent());// 这个是用来获取文本内容

21、                 System.out.println("--------------这里进行了强制类型转换-----------");

22、                 System.out.println(e.getNodeName());

23、                 System.out.println(e.getNodeValue());

24、                 System.out.println(e.getTextContent());

25、                 if (e.getTextContent().equals("深入理解java")) {

26、                    System.out.println("==========================");

27、                    System.out.println(e.getNextSibling().getNextSibling()

28、                           .getTextContent());// 空格也是一个节点+++这个地方是个易错点,空格也是一个节点

29、                    System.out.println("=========================");

30、                 }

31、             }

32、       

33、          }

34、              }

CURD:creat 、updata、read、delete

 

用推模式进行SAX解析

import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;

 

import org.xml.sax.Attributes;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

 

public class SAXTest {

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

       SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();// 这个是获得工厂

       SAXParser saxParser = saxParserFactory.newSAXParser();// 这个是得到解析器

       saxParser.parse("server.xml", new MyHandler());// 对文档进行解析

    }

}

 

class MyHandler extends DefaultHandler {

 

    @Override

    public void characters(char[] ch, int start, int length)

           throws SAXException {

       String string = new String(ch, start, length);

       System.out.println("字符开始:" + string);

    }

 

    @Override

    public void endDocument() throws SAXException {

       System.out.println("文档结束");

    }

 

    @Override

    public void startDocument() throws SAXException {

       System.out.println("文档开始:");

    }

 

    @Override

    public void startElement(String uri, String localName, String qName,

           Attributes attributes) throws SAXException {

       System.out.println("元素开始:" + qName);

    }

 

}

DOM解析之CURDcreatupdatareaddelete)实例

 

package cn.binbin.xmlParse;

 

import java.io.File;

import java.io.IOException;

 

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerException;

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamResult;

 

import org.junit.Test;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

import org.xml.sax.SAXException;

 

public class CURDbook {

 

         @Test

         public void delet() throws Exception {

                   DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory

                                     .newInstance();

                   DocumentBuilder documentBuilder = documentBuilderFactory

                                     .newDocumentBuilder();

                   Document document = documentBuilder.parse("books.xml");

 

                   // 这个地方时删除节点,方法是先获取父节点,然后在用父节点来删除子节点,不允许自杀

                   NodeList nodeList = document.getElementsByTagName("name");

                   for (int i = 0; i < nodeList.getLength(); i++) {

                            // System.out.println(nodeList.item(i).getTextContent());

                            if (nodeList.item(i).getTextContent().contains("java")) {

                                     Element book = (Element) nodeList.item(i).getParentNode();

                                     System.out.println(book.getNodeName());

                                     System.out.println(book.getParentNode().getNodeName());

                                     book.getParentNode().removeChild(book);

                                     i--;

                            }

 

                   }

 

                   // 开始回写

                   TransformerFactory transformerFactory = TransformerFactory

                                     .newInstance();

                   Transformer transformer = transformerFactory.newTransformer();

                   DOMSource domSource = new DOMSource(document);

                   StreamResult result = new StreamResult("books_bak.xml");

                   transformer.transform(domSource, result);

         }

 

         @Test

         public void updata() throws ParserConfigurationException, SAXException,

                            IOException, TransformerException {

                   DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory

                                     .newInstance();

                   DocumentBuilder documentBuilder = documentBuilderFactory

                                     .newDocumentBuilder();

                   Document document = documentBuilder.parse("books.xml");

 

                   // 先获取节点,然后在修改节点,在回执节点

                   NodeList nodeList = document.getElementsByTagName("name");

                   for (int i = 0; i < nodeList.getLength(); i++) {

                            if (nodeList.item(i).getTextContent().equals("深入理解java")) {

                                     nodeList.item(i).getNextSibling().getNextSibling()

                                                        .setTextContent(

                                                                           Double.parseDouble(nodeList.item(i)

                                                                                             .getNextSibling().getNextSibling()

                                                                                             .getTextContent())

                                                                                             * 1.2 + "");

                            }

                   }

 

                   // 开始回写

                   TransformerFactory transformerFactory = TransformerFactory

                                     .newInstance();

                   Transformer transformer = transformerFactory.newTransformer();

                   DOMSource domSource = new DOMSource(document);

                   StreamResult result = new StreamResult("books_bak.xml");

                   transformer.transform(domSource, result);

         }

 

         @Test

         public void creat() throws ParserConfigurationException, SAXException,

                            IOException, TransformerException {

                   DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory

                                     .newInstance();

                   DocumentBuilder documentBuilder = documentBuilderFactory

                                     .newDocumentBuilder();

                   Document document = documentBuilder.parse("books.xml");

                   // 2、创建name节点

                   // 1、(创建book节点)创建节点元素,将元素加入到指定位置

                   Element newbook = document.createElement("book");

                   Element name = document.createElement("name");

                   name.setTextContent("编程高手");

                   newbook.appendChild(name);

                   // 找到位置

                   Element root = document.getDocumentElement();

                   root.appendChild(newbook);

 

                   // 开始回写

                   TransformerFactory transformerFactory = TransformerFactory

                                     .newInstance();

                   Transformer transformer = transformerFactory.newTransformer();

                   DOMSource domSource = new DOMSource(document);

                   StreamResult result = new StreamResult("books_bak.xml");

                   transformer.transform(domSource, result);

         }

 

         @Test

         // 修改就应该回写,这里是回写

         public void transform() throws ParserConfigurationException, SAXException,

                            IOException, TransformerException {

                   DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory

                                     .newInstance();

                   DocumentBuilder documentBuilder = documentBuilderFactory

                                     .newDocumentBuilder();

                   Document document = documentBuilder.parse("books.xml");

 

                   // 回写,有来有回

                   TransformerFactory transformerFactory = TransformerFactory

                                     .newInstance();

                   Transformer transformer = transformerFactory.newTransformer();

                   DOMSource domSource = new DOMSource(document);// 这个地方是用的DOMSource

                   StreamResult streamResult = new StreamResult(new File("book_bak.xml"));// 这个是用流式输出结果集

                   transformer.transform(domSource, streamResult);// 这个地方用到了回写器

         }

 

}

posted @ 2013-04-11 00:27  蓝冰悠见  阅读(164)  评论(0编辑  收藏  举报