一、XML解析方式分为两种:dom和sax
- dom:(Document Object Model,即文档对象模型)是W3C组织推荐的解析XML的一种方式。
- sax:(Simple API for XML)不是官方标准,但它是XML社区事实上的标准,几乎所有的XML解析器都支持它。
dom和sax解析方法的区别:
- dom:对文档CRUD(增删改查)比较方便,缺点:占用内存比较大(将xml文件全部读入内存)。
- sax:占用内存较少,解析速度快(从上往下读一行解析一行)。缺点:只适合读取,不适合CRUD。
XML解析开发包:Jaxp(sun)、Jdom、dom4j
今天学习Jaxp:
二、使用JAXP对XML文档进行DOM解析:
private static String filename="src/exam.xml"; public static Document getDocument() throws Exception{//1创建工厂DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();//2得到dom解析器对象DocumentBuilder builder=factory.newDocumentBuilder();//3解析xml文档,得到代表文档的documentreturn builder.parse(filename);}
更加形象的描述为下图:
增,删,改 需要将更新后的内容在写到xml中
public static void writeXml(Document document) throws TransformerException{TransformerFactory factory=TransformerFactory.newInstance();Transformer tf=factory.newTransformer();//transform(Source xmlSource, Result outputTarget)tf.transform(new DOMSource(document), new StreamResult(filename) );}
三、实战演练:
books.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?><书架><书><书名 name="xxx">影响力</书名><作者>罗伯特.西奥迪尼</作者><售价>90</售价></书><书><书名>JavaScript</书名><作者>JS</作者><售价>45</售价></书> </书架>
读取XML文件,JUnit测试:
@Testpublic void read() throws ParserConfigurationException, SAXException, IOException {DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();DocumentBuilder builder=factory.newDocumentBuilder();Document document=builder.parse("src/books.xml");NodeList list= document.getElementsByTagName("书名");Node node= list.item(0);String content=node.getTextContent();System.out.println(content);}
递归遍历XML文件中的所有节点
@Testpublic void read1() throws ParserConfigurationException, SAXException, IOException {DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();DocumentBuilder builder=factory.newDocumentBuilder();Document document=builder.parse("src/books.xml");Node root=document.getElementsByTagName("书架").item(0);list(root); }/** 递归遍历XML文件中的所有节点* */public void list(Node node){if(node instanceof Element){System.out.println(node.getNodeName());}NodeList childs= node.getChildNodes();for(int i=0;i<childs.getLength();i++){Node child=childs.item(i);list(child);}}
添加节点到XML文件中:
@Testpublic void add() throws ParserConfigurationException, SAXException, IOException, TransformerException{DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();DocumentBuilder builder=factory.newDocumentBuilder();Document document=builder.parse("src/books.xml");Element price=document.createElement("售价");//创建节点//把创建的节点添加到第一本书price.setTextContent("90");Element book=(Element) document.getElementsByTagName("书").item(0);book.appendChild(price);//把更新后的文档TransformerFactory tfactory=TransformerFactory.newInstance();Transformer tf=tfactory.newTransformer();tf.transform(new DOMSource(document), new StreamResult("src/books.xml"));}
为标签添加属性:
@Testpublic void addAttr() throws ParserConfigurationException, SAXException, IOException, TransformerException{DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();DocumentBuilder builder=factory.newDocumentBuilder();Document document=builder.parse("src/books.xml");Element bookname=(Element) document.getElementsByTagName("书名").item(0);bookname.setAttribute("name", "xxx");//把更新后的文档TransformerFactory tfactory=TransformerFactory.newInstance();Transformer tf=tfactory.newTransformer();tf.transform(new DOMSource(document), new StreamResult("src/books.xml"));}
删除操作:
@Testpublic void delete() throws ParserConfigurationException, SAXException, IOException, TransformerException{DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();DocumentBuilder builder=factory.newDocumentBuilder();Document document=builder.parse("src/books.xml");Element bookname=(Element) document.getElementsByTagName("售价").item(0);//把更新后的文档Element e=(Element) document.getElementsByTagName("书").item(0);e.removeChild(bookname);TransformerFactory tfactory=TransformerFactory.newInstance();Transformer tf=tfactory.newTransformer();tf.transform(new DOMSource(document), new StreamResult("src/books.xml"));}
快乐学习,轻松掌握!