JAXP:Java API for XML Programing
DOM:Document Object Method
DOM特点:
(1)将XML文档全部放入内存进行解析,因此耗内存;
(2)能够对XML进行增删改查;
<person> <name>xiazdong</name> <age>20</age> </person>
DOM常用的类有:DocumentBuilderFactory、DocumentBuilder、Document、Node、NodeList、Element、Attribute、TransformerFactory、Transformer、DOMSource、StreamResult等类;
(1)通过DocumentBuilderFactory获得DocumentBuilder;
(2)通过DocumentBuilder解析XML,并获得Document(文档的根节点);
(3)通过Document进行操作;
(4)如果对XML进行了改动,则需要TransformerFactory创建一个Transformer;
(5)通过Transformer进行保存;
(1)DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance();
(2)DocumentBuilder builder = factory.newDocumentBuilder();
(1)Document document = builder.parse(new File(""));//解析XML并返回document节点
(2)Document document = builder.newDocument(); //创建一个文档
(1)NodeList list = getElementsByTagName("name");//获得<name>标签的队列
(2)Element e = document.createElement("name"); //创建一个<name>标签,并返回
(1)Node node = nodeList.item(i); //获得节点队列的第i+1个节点
(2)int length = nodeList.getLength(); //获得节点队列的长度
(1)String text = node.getTextContent(); //获得节点的值
(2)node.appendChild(Node child); //向node尾部添加一个child子节点
(3)node.insertBefore(Node child,Node beforeNode);//向node的beforeNode子节点之前添加一个child子节点
(4)NodeList list = node.getChildNodes(); //获得node全部的子节点
(5)Node parent = node.getParentNode(); //获得node的父节点
(6)node.removeChild(Node child); //移除node的child子节点
(7)node.setTextContent(String text); //设置node 的值
(8)String name = node.getNodeName(); //获得标签的名称
(1)element.setAttribute("name","value"); //设置标签的属性
(2)String value = element.getAttribute("name"); //获得标签的属性
(3)element.removeAttribute("name") //移除标签的属性
(1)TransformerFactory tfactory = TransformerFactory.newInstance();
(2)Transformer tf = tfactory.newTransformer();
(1)tf.transform(Source source,Result result); //输出
(1)DOMSource source = new DOMSource(Document document);
(1)StreamResult result = new StreamResult(new FileOutputStream("1.xml"));
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <personlist> <person> <name aaa="xxxx">xiazdong-1</name> <age>20</age> <salary>1000</salary> </person> <person> <name>xiazdong-2</name> <age>21</age> <salary>2000</salary> </person> </personlist>
private static void create(DocumentBuilder builder) throws Exception { Document document = builder.newDocument(); Element root = document.createElement("person"); Element name = document.createElement("name"); name.setTextContent("xiazdong"); Element age = document.createElement("age"); age.setTextContent("20"); root.appendChild(name); root.appendChild(age); document.appendChild(root); TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer tf = tfactory.newTransformer(); tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("output.xml"))); }
private static void insert(Document document)throws Exception { Node person = document.getElementsByTagName("person").item(0); Node name = document.getElementsByTagName("name").item(0); Element tmp = document.createElement("tmpChild"); tmp.setTextContent("xxx"); person.insertBefore(tmp, name); TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer tf = tfactory.newTransformer(); tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("1.xml"))); }
private static void read(Document document) { Node node = document.getElementsByTagName("name").item(0); String name = node.getNodeName(); String value = node.getTextContent(); String attr = ((Element)node).getAttribute("aaa"); System.out.println(name); System.out.println(value); System.out.println(attr); }
private static void readAll(Document document) { Node root = document.getElementsByTagName("personlist").item(0); read(root); } private static void read(Node node) { if(node instanceof Element) System.out.print("<"+node.getNodeName()+"></"+node.getNodeName()+">"); NodeList list = node.getChildNodes(); for(int i=0;i<list.getLength();i++){ Node n = list.item(i); read(n); } }
private static void update(Document document) throws Exception { Node node = document.getElementsByTagName("name").item(0); node.setTextContent("xiazdong"); Element e = (Element)node; e.setAttribute("aaa", "111"); TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer tf = tfactory.newTransformer(); tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("1.xml"))); }
private static void delete(Document document) throws Exception{ Node name = document.getElementsByTagName("name").item(0); name.getParentNode().removeChild(name); TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer tf = tfactory.newTransformer(); tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("1.xml"))); }