标签:
一、XML解析方式分为两种:dom和sax
dom和sax解析方法的区别:
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文档,得到代表文档的document return 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测试:
@Test public 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文件中的所有节点
@Test public 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文件中:
@Test public 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")); }
为标签添加属性:
@Test public 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")); }
删除操作:
@Test public 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")); }
快乐学习,轻松掌握!
标签:
原文地址:http://www.cnblogs.com/lyjs/p/4858814.html