JAXP之DOM总结


一、JAXP、DOM介绍


JAXP:Java API for XML Programing

DOM:Document Object Method


DOM特点:

(1)将XML文档全部放入内存进行解析,因此耗内存;

(2)能够对XML进行增删改查;


<person> <name>xiazdong</name> <age>20</age> </person>
变成DOM树:




二、DOM API


DOM常用的类有:DocumentBuilderFactory、DocumentBuilder、Document、Node、NodeList、Element、Attribute、TransformerFactory、Transformer、DOMSource、StreamResult等类;


DOM开发步骤:


(1)通过DocumentBuilderFactory获得DocumentBuilder;

(2)通过DocumentBuilder解析XML,并获得Document(文档的根节点);

(3)通过Document进行操作;

(4)如果对XML进行了改动,则需要TransformerFactory创建一个Transformer;

(5)通过Transformer进行保存;


常用操作


DocumentBuilderFactory类中有:

(1)DocumentBuilderFactory factory =DocumentBuilderFactory .newInstance();

(2)DocumentBuilder builder = factory.newDocumentBuilder();


DocumentBuilder类中有:

(1)Document document = builder.parse(new File(""));//解析XML并返回document节点

(2)Document document = builder.newDocument(); //创建一个文档


Document类中有:


(1)NodeList list = getElementsByTagName("name");//获得<name>标签的队列

(2)Element e = document.createElement("name"); //创建一个<name>标签,并返回


NodeList类中有:

(1)Node node = nodeList.item(i); //获得节点队列的第i+1个节点

(2)int length = nodeList.getLength(); //获得节点队列的长度


Node中有:


(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(); //获得标签的名称


Element中有:

除了Node类的方法,还有

(1)element.setAttribute("name","value"); //设置标签的属性

(2)String value = element.getAttribute("name"); //获得标签的属性

(3)element.removeAttribute("name") //移除标签的属性


TranformerFactory 中有:

(1)TransformerFactory tfactory = TransformerFactory.newInstance();

(2)Transformer tf = tfactory.newTransformer();


Transformer中有:

(1)tf.transform(Source source,Result result); //输出


DOMSource中有:

(1)DOMSource source = new DOMSource(Document document);


StreamResult中有:

(1)StreamResult result = new StreamResult(new FileOutputStream("1.xml"));


三、DOM进行CRUD


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>

1.Create


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"))); }

2.Read


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); }
应用:遍历整个XML文档并输出

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); } }

3.Update


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"))); }


4.Delete

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"))); }





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值