package@H_403_3@ dom;
import@H_403_3@ java.io.File;
import@H_403_3@ javax.xml.parsers.DocumentBuilder;
import@H_403_3@ javax.xml.parsers.DocumentBuilderFactory;
import@H_403_3@ javax.xml.transform.Result;
import@H_403_3@ javax.xml.transform.Source;
import@H_403_3@ javax.xml.transform.Transformer;
import@H_403_3@ javax.xml.transform.TransformerFactory;
import@H_403_3@ javax.xml.transform.dom.DOMSource;
import@H_403_3@ javax.xml.transform.stream.StreamResult;
import@H_403_3@ org.w3c.dom.Document;
import@H_403_3@ org.w3c.dom.Element;
import@H_403_3@ org.w3c.dom.Node;
import@H_403_3@ org.w3c.dom.NodeList;
public@H_403_3@ class@H_403_3@ DomParse@H_403_3@ {@H_403_3@
public@H_403_3@ DomParse@H_403_3@() {
// TODO Auto-generated constructor stub@H_403_3@
}
/** * xml的解析技术:JAXP是Java API for XML Processing的英文字头缩写,* 中文含义是:用于XML文档处理的使用Java语言编写的编程接口。JAXP支持DOM、SAX、XSLT等标准。 下面我们研究两种解析方式: * 1.dom解析 2.sax解析:Simple API for XML 下面是dom解析的实例。 JAXP-DOM解析实例: * 下面的实例实现的功能是,通过javax.xml包实现dom方式的xml的解析 具体的操作有增加节点,删除节点,修改节点内容,查询节点信息 */@H_403_3@
// 可以用junit测试工具的方法进行测试@H_403_3@
public@H_403_3@ static@H_403_3@ void@H_403_3@ main@H_403_3@(String[] args) throws@H_403_3@ Exception {
demo05();
DomParse domParse = new@H_403_3@ DomParse();
}
// 获取和解析器关联的Document对象@H_403_3@
public@H_403_3@ static@H_403_3@ void@H_403_3@ demo01@H_403_3@() throws@H_403_3@ Exception {
// ==============获得document==========================@H_403_3@
// 1.获得工厂@H_403_3@
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// 2.获得解析器@H_403_3@
DocumentBuilder builder = factory.newDocumentBuilder();
// 3.根据解析器获取Document对象@H_403_3@
@SuppressWarnings@H_403_3@("unused"@H_403_3@)
Document document = builder.parse(new@H_403_3@ File("db.xml"@H_403_3@));
}
// 查询@H_403_3@
public@H_403_3@ static@H_403_3@ void@H_403_3@ demo02@H_403_3@() throws@H_403_3@ Exception {
// 1.获得document@H_403_3@
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new@H_403_3@ File("src/db.xml"@H_403_3@).getCanonicalFile());
// 2.获得根元素--books(可以省略)@H_403_3@
@SuppressWarnings@H_403_3@("unused"@H_403_3@)
Element rootElement = document.getDocumentElement();
// 3.获取所有的book元素,属性id@H_403_3@
NodeList allBookElements = document.getElementsByTagName("book"@H_403_3@);
// 4遍历book元素----本实例db.xml中有两个book元素@H_403_3@
for@H_403_3@ (int@H_403_3@ i = 0@H_403_3@; i < allBookElements.getLength(); i++) {
Element childNode = (Element) allBookElements.item(i);
System.out.println(i);
// 4.1获取book元素的id属性@H_403_3@
String id = childNode.getAttribute("id"@H_403_3@);
System.out.println("###id:"@H_403_3@ + id);
System.out.println(childNode.getNodeName());
// 4.2获取book元素下的子节点@H_403_3@
NodeList childNodeList = childNode.getChildNodes();
// 注意文本也是节点,所以回车换行的部分会是文本,也会添加到上面定义的childNodeList中@H_403_3@
// 遍历@H_403_3@
for@H_403_3@ (int@H_403_3@ j = 0@H_403_3@; j < childNodeList.getLength(); j++) {
// 做判断-是否是非文本节点@H_403_3@
Node childNode2 = childNodeList.item(j);
// 有两种方式判断是否一定是元素@H_403_3@
// ================方法1=========================@H_403_3@
if@H_403_3@ (childNode2.getNodeType() == Node.ELEMENT_NODE) {
// 获取标签的名称@H_403_3@
System.out.print(childNode2.getNodeName());
System.out.println(":"@H_403_3@);
// 获取元素标签的内容@H_403_3@
System.out.println(childNode2.getTextContent());
}
// ===============方法2==========================@H_403_3@
if@H_403_3@ (childNode2 instanceof@H_403_3@ Element) {// ...........}@H_403_3@
// =============================================@H_403_3@
}
}
}
}
// 增加@H_403_3@
public@H_403_3@ static@H_403_3@ void@H_403_3@ demo03@H_403_3@() throws@H_403_3@ Exception {
// 获取doument对象-dom方式@H_403_3@
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new@H_403_3@ File("src/db.xml"@H_403_3@).getCanonicalFile());
// 获取根元素@H_403_3@
Element rootElement = document.getDocumentElement();
// 创建新元素,不要以为是rootElement创建的哦,Document创建,用appendchild()关联@H_403_3@
Element bookElement = document.createElement("book"@H_403_3@);
// 为元素创建id属性@H_403_3@
bookElement.setAttribute("id"@H_403_3@,"b0003"@H_403_3@);
// 为book元素创建子元素@H_403_3@
Element newTitleElement = document.createElement("title"@H_403_3@);
newTitleElement.setTextContent("Flex开发"@H_403_3@);
Element newPriceElement = document.createElement("price"@H_403_3@);
newPriceElement.setTextContent("98"@H_403_3@);
Element newAuthorElement = document.createElement("author"@H_403_3@);
newAuthorElement.setTextContent("胡玉勉"@H_403_3@);
bookElement.appendChild(newTitleElement);
bookElement.appendChild(newPriceElement);
bookElement.appendChild(newAuthorElement);
// 将book添加到根节点rootElement下*****************@H_403_3@
rootElement.appendChild(bookElement);
// =======将创建好了新元素的document更新到原来的文件中=====@H_403_3@
// 运用javax.transform技术@H_403_3@
TransformerFactory tformFactory = TransformerFactory.newInstance();
// transform(Source xmlSource,Result outputTarget)@H_403_3@
// 将 XML Source 转换为 Result。@H_403_3@
Transformer transformer = tformFactory.newTransformer();
// 这里面的参数不用rootElement,用document@H_403_3@
Source xmlSource = new@H_403_3@ DOMSource(document);
Result outputTarget = new@H_403_3@ StreamResult(new@H_403_3@ File("src/db.xml"@H_403_3@).getCanonicalFile());
transformer.transform(xmlSource,outputTarget);
}
// 删除-通过父节点删除子节点@H_403_3@
@SuppressWarnings@H_403_3@("unused"@H_403_3@)
public@H_403_3@ static@H_403_3@ void@H_403_3@ demo04@H_403_3@() throws@H_403_3@ Exception {
// 获取document@H_403_3@
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new@H_403_3@ File("db.xml"@H_403_3@));
// 获取根元素@H_403_3@
Element rootElement = document.getDocumentElement();
// 删除某一个book元素,比如id为“b003”@H_403_3@
// 1.获取id为“b003”的元素@H_403_3@
NodeList allBookElement = document.getElementsByTagName("book"@H_403_3@);
for@H_403_3@ (int@H_403_3@ m = 0@H_403_3@; m < allBookElement.getLength(); m++) {
Element bookElement = (Element) allBookElement.item(m);
String id = bookElement.getAttribute("id"@H_403_3@);
if@H_403_3@ ("b0003"@H_403_3@.equals(id)) {
// 注意删除节点的时候,应遵循由自己找到父节点,然后通过父节点删除@H_403_3@
bookElement.getParentNode().removeChild(bookElement);
}
}
// =======将删除了的document更新到原来的文件中=====@H_403_3@
// 运用javax.transform技术:Transformer此抽象类的实例能够将源树转换为结果树@H_403_3@
TransformerFactory tformFactory = TransformerFactory.newInstance();
// transform(Source xmlSource,Result outputTarget)@H_403_3@
// 将 XML Source 转换为 Result。@H_403_3@
Transformer transformer = tformFactory.newTransformer();
// 这里面的参数不用rootElement,用document@H_403_3@
Source xmlSource = new@H_403_3@ DOMSource(document);
Result outputTarget = new@H_403_3@ StreamResult(new@H_403_3@ File("db.xml"@H_403_3@));
transformer.transform(xmlSource,outputTarget);
}
// 修改@H_403_3@
@SuppressWarnings@H_403_3@("unused"@H_403_3@)
public@H_403_3@ static@H_403_3@ void@H_403_3@ demo05@H_403_3@() throws@H_403_3@ Exception {
// 获取document@H_403_3@
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new@H_403_3@ File("src/db.xml"@H_403_3@).getCanonicalFile());
Element rootElement = document.getDocumentElement();
// 获取元素进行修改@H_403_3@
NodeList bookNodeList = document.getElementsByTagName("book"@H_403_3@);
// 遍历,对id=“b002”的book元素进行修改@H_403_3@
for@H_403_3@ (int@H_403_3@ i = 0@H_403_3@; i < bookNodeList.getLength(); i++) {
// 获取低i个book元素@H_403_3@
Element bookNode = (Element) bookNodeList.item(i);
String id = bookNode.getAttribute("id"@H_403_3@);
if@H_403_3@ ("b002"@H_403_3@.equals(id)) {
// 获取book元素的子节点进行修改@H_403_3@
NodeList childNodeList = bookNode.getChildNodes();
int@H_403_3@ m = 0@H_403_3@;
for@H_403_3@ (int@H_403_3@ j = 0@H_403_3@; j < childNodeList.getLength(); j++) {
Node bookChildNode = childNodeList.item(j);
if@H_403_3@ (bookChildNode.getNodeType() != Node.TEXT_NODE) {// 判断是否有子节点防止修改text节点@H_403_3@
System.out.println(bookChildNode.getNodeName());
System.out.println(bookChildNode.getTextContent());
bookChildNode.setTextContent("Vincent"@H_403_3@);
System.out.println(bookChildNode.getTextContent());
m++;
}
}
System.out.println("执行了"@H_403_3@ + m + "次"@H_403_3@);
}
}
// 记住别忘了回写到xml文件中@H_403_3@
// =======将删除了的document更新到原来的文件中=====@H_403_3@
// 运用javax.transform技术:Transformer此抽象类的实例能够将源树转换为结果树@H_403_3@
TransformerFactory tformFactory = TransformerFactory.newInstance();
// transform(Source xmlSource,Result outputTarget)@H_403_3@
// 将 XML Source 转换为 Result。@H_403_3@
Transformer transformer = tformFactory.newTransformer();
// 这里面的参数不用rootElement,用document@H_403_3@
Source xmlSource = new@H_403_3@ DOMSource(document);
Result outputTarget = new@H_403_3@ StreamResult(new@H_403_3@ File("src/db.xml"@H_403_3@).getCanonicalFile());
transformer.transform(xmlSource,outputTarget);
}
}
所用的xml文件
<?xml version="1.0" encoding="UTF-8" standalone="no"?>@H_403_3@<books@H_403_3@>@H_403_3@
<book@H_403_3@ id@H_403_3@="b001"@H_403_3@>@H_403_3@
<title@H_403_3@>@H_403_3@android</title@H_403_3@>@H_403_3@
<price@H_403_3@>@H_403_3@28</price@H_403_3@>@H_403_3@
<author@H_403_3@>@H_403_3@vincent</author@H_403_3@>@H_403_3@
</book@H_403_3@>@H_403_3@
<book@H_403_3@ id@H_403_3@="b002"@H_403_3@>@H_403_3@
<title@H_403_3@>@H_403_3@Vincent</title@H_403_3@>@H_403_3@
<price@H_403_3@>@H_403_3@Vincent</price@H_403_3@>@H_403_3@
<author@H_403_3@>@H_403_3@Vincent</author@H_403_3@>@H_403_3@
</book@H_403_3@>@H_403_3@
<book@H_403_3@ id@H_403_3@="b0003"@H_403_3@>@H_403_3@
<title@H_403_3@>@H_403_3@Flex开发</title@H_403_3@>@H_403_3@
<price@H_403_3@>@H_403_3@98</price@H_403_3@>@H_403_3@
<author@H_403_3@>@H_403_3@胡玉勉</author@H_403_3@>@H_403_3@
</book@H_403_3@>@H_403_3@
<book@H_403_3@ id@H_403_3@="b0003"@H_403_3@>@H_403_3@
<title@H_403_3@>@H_403_3@Flex开发</title@H_403_3@>@H_403_3@
<price@H_403_3@>@H_403_3@98</price@H_403_3@>@H_403_3@
<author@H_403_3@>@H_403_3@胡玉勉</author@H_403_3@>@H_403_3@
</book@H_403_3@>@H_403_3@
<book@H_403_3@ id@H_403_3@="b0003"@H_403_3@>@H_403_3@
<title@H_403_3@>@H_403_3@Flex开发</title@H_403_3@>@H_403_3@
<price@H_403_3@>@H_403_3@98</price@H_403_3@>@H_403_3@
<author@H_403_3@>@H_403_3@胡玉勉</author@H_403_3@>@H_403_3@
</book@H_403_3@>@H_403_3@
<book@H_403_3@ id@H_403_3@="b0003"@H_403_3@>@H_403_3@
<title@H_403_3@>@H_403_3@Flex开发</title@H_403_3@>@H_403_3@
<price@H_403_3@>@H_403_3@98</price@H_403_3@>@H_403_3@
<author@H_403_3@>@H_403_3@胡玉勉</author@H_403_3@>@H_403_3@
</book@H_403_3@>@H_403_3@
</books@H_403_3@>@H_403_3@