IT程序员开发必备-各类资源下载清单,史上最全IT资源,个人收藏总结!
package edu.jaxp;import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
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;
/**
* 测试对document对象进行CRUD操作
**/
public class JaxpDomCRUD {
//解析xml文档,得到代表文档的document
public Document getDocument(){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
Document document = null;
try {
builder = factory.newDocumentBuilder();
document = builder.parse("WebRoot/product1.xml");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return document;
}
//将内存中修改的document对象更新至xml文档中
public void updateXML(Document documentSrc) {
//得到TransformerFactory实例对象
TransformerFactory transformerFactory = TransformerFactory.newInstance();
//由transformerFactory对象创建Transformer对象
Transformer transformer = null;
try {
transformer = transformerFactory.newTransformer();
} catch (TransformerConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
/*
* 调用transformer对象的transform(Source xmlSource, Result outputTarget)方法
* public class DOMSource extends Object implements Source
* 构造方法:DOMSource(Node n)
* public class StreamResult extends Object implements Result
* 构造方法:StreamResult(OutputStream outputStream)
*/
try {
transformer.transform(new DOMSource(documentSrc),
new StreamResult(new FileOutputStream("WebRoot/product1.xml")));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//向<product category="Table">节点中添加节点<notes>这是桌椅</notes>
@Test
public void appendNode(){
//得到xml文档的document对象
Document document = getDocument();
//创建<notes>新节点
Element notes = document.createElement("notes");
notes.setAttribute("name","肖华");
notes.setTextContent("这是桌椅");
//把创建的新节点追加在第二个<product>节点下
Element product = (Element) document.getElementsByTagName("product").item(1);
product.appendChild(notes);
//更新XML文档
updateXML(document);
}
//向xml文档中指定位置上添加节点:<notes>这是桌椅</notes>
@Test
public void insertNode(){
//得到xml文档的document对象
Document document = getDocument();
//创建<notes>新节点
Element notes = document.createElement("notes");
notes.setAttribute("name","肖华");
notes.setTextContent("这是桌椅");
//得到参考节点
Element refNode = (Element) document.getElementsByTagName("price").item(1);
//把创建的新节点插入在第二个<product>节点下的子节点<price>前面
Element product = (Element) document.getElementsByTagName("product").item(1);
product.insertBefore(notes, refNode);
//更新XML文档
updateXML(document);
}
//向xml文档指定节点添加或修改或删除属性
@Test
public void setOrUpdateAttr(){
//得到xml文档的document对象
Document document = getDocument();
//为 <notes>这是扳手</notes>节点添加属性
Element notesNode = (Element) document.getElementsByTagName("notes").item(0);
notesNode.setAttribute("name", "曹欢");
//为<price street="澳门街" wholesale="部分">100.0</price>修改属性值
Element priceNode = (Element) document.getElementsByTagName("price").item(1);
priceNode.setAttribute("wholesale", "小部分");
// 移除<specifications weight="2.0kg">扳手</specifications>的weight属性
Element specificationsNode = (Element) document.getElementsByTagName("specifications").item(0);
specificationsNode.removeAttribute("weight");
//更新XML文档
updateXML(document);
}
//删除xml文档中指定的节点<notes name="曹欢">这是扳手</notes>
@Test
public void deleteNode(){
//得到xml文档的document对象
Document document = getDocument();
//取得要删除的节点
Element notesNode = (Element) document.getElementsByTagName("notes").item(0);
//取得要删除的节点的父节点,如果noteNode节点为null的话,则会抛出NullPointerException
//Element parentNode = (Element) notesNode.getParentNode();
Element parentNode = (Element) document.getElementsByTagName("product").item(1);
//由父节点移除子节点
parentNode.removeChild(notesNode);
//更新XML文档
updateXML(document);
}
//修改Element元素的内容
@Test
public void updateContextText(){
//得到xml文档的document对象
Document document = getDocument();
//priceNode只能是productNode节点的子节点
Element productNode = (Element) document.getElementsByTagName("product").item(0);
Element priceNode = (Element) productNode.getElementsByTagName("price").item(0);
priceNode.setTextContent("90.0");
//更新XML文档
updateXML(document);
}
}
product1.xml文件内容:
<?xml version="1.0" encoding="utf-8" standalone="no"?> <catalog id="cata1"> <product category="HandTool"> <specifications>扳手</specifications> <price street="香港街">90.0</price> </product> <product category="Table"> <specifications>桌椅</specifications> <notes name="肖华">这是桌椅</notes> <price street="澳门街" wholesale="小部分">100.0</price> </product> </catalog> |