前端之家收集整理的这篇文章主要介绍了
xml解析-dom范例,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package dom;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class DomParse {
public DomParse() {
}
/** * 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的解析 具体的操作有增加节点,删除节点,修改节点内容,查询节点信息 */
public static void main(String[] args) throws Exception {
demo05();
DomParse domParse = new DomParse();
}
public static void demo01() throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
@SuppressWarnings("unused")
Document document = builder.parse(new File("db.xml"));
}
public static void demo02() throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File("src/db.xml").getCanonicalFile());
@SuppressWarnings("unused")
Element rootElement = document.getDocumentElement();
NodeList allBookElements = document.getElementsByTagName("book");
for (int i = 0; i < allBookElements.getLength(); i++) {
Element childNode = (Element) allBookElements.item(i);
System.out.println(i);
String id = childNode.getAttribute("id");
System.out.println("###id:" + id);
System.out.println(childNode.getNodeName());
NodeList childNodeList = childNode.getChildNodes();
for (int j = 0; j < childNodeList.getLength(); j++) {
Node childNode2 = childNodeList.item(j);
if (childNode2.getNodeType() == Node.ELEMENT_NODE) {
System.out.print(childNode2.getNodeName());
System.out.println(":");
System.out.println(childNode2.getTextContent());
}
if (childNode2 instanceof Element) {
}
}
}
}
public static void demo03() throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File("src/db.xml").getCanonicalFile());
Element rootElement = document.getDocumentElement();
Element bookElement = document.createElement("book");
bookElement.setAttribute("id","b0003");
Element newTitleElement = document.createElement("title");
newTitleElement.setTextContent("Flex开发");
Element newPriceElement = document.createElement("price");
newPriceElement.setTextContent("98");
Element newAuthorElement = document.createElement("author");
newAuthorElement.setTextContent("胡玉勉");
bookElement.appendChild(newTitleElement);
bookElement.appendChild(newPriceElement);
bookElement.appendChild(newAuthorElement);
rootElement.appendChild(bookElement);
TransformerFactory tformFactory = TransformerFactory.newInstance();
Transformer transformer = tformFactory.newTransformer();
Source xmlSource = new DOMSource(document);
Result outputTarget = new StreamResult(new File("src/db.xml").getCanonicalFile());
transformer.transform(xmlSource,outputTarget);
}
@SuppressWarnings("unused")
public static void demo04() throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File("db.xml"));
Element rootElement = document.getDocumentElement();
NodeList allBookElement = document.getElementsByTagName("book");
for (int m = 0; m < allBookElement.getLength(); m++) {
Element bookElement = (Element) allBookElement.item(m);
String id = bookElement.getAttribute("id");
if ("b0003".equals(id)) {
bookElement.getParentNode().removeChild(bookElement);
}
}
TransformerFactory tformFactory = TransformerFactory.newInstance();
Transformer transformer = tformFactory.newTransformer();
Source xmlSource = new DOMSource(document);
Result outputTarget = new StreamResult(new File("db.xml"));
transformer.transform(xmlSource,outputTarget);
}
@SuppressWarnings("unused")
public static void demo05() throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File("src/db.xml").getCanonicalFile());
Element rootElement = document.getDocumentElement();
NodeList bookNodeList = document.getElementsByTagName("book");
for (int i = 0; i < bookNodeList.getLength(); i++) {
Element bookNode = (Element) bookNodeList.item(i);
String id = bookNode.getAttribute("id");
if ("b002".equals(id)) {
NodeList childNodeList = bookNode.getChildNodes();
int m = 0;
for (int j = 0; j < childNodeList.getLength(); j++) {
Node bookChildNode = childNodeList.item(j);
if (bookChildNode.getNodeType() != Node.TEXT_NODE) {
System.out.println(bookChildNode.getNodeName());
System.out.println(bookChildNode.getTextContent());
bookChildNode.setTextContent("Vincent");
System.out.println(bookChildNode.getTextContent());
m++;
}
}
System.out.println("执行了" + m + "次");
}
}
TransformerFactory tformFactory = TransformerFactory.newInstance();
Transformer transformer = tformFactory.newTransformer();
Source xmlSource = new DOMSource(document);
Result outputTarget = new StreamResult(new File("src/db.xml").getCanonicalFile());
transformer.transform(xmlSource,outputTarget);
}
}
所用的xml文件
<?xml version="1.0" encoding="UTF-8" standalone="no"?><books>
<book id="b001">
<title>android</title>
<price>28</price>
<author>vincent</author>
</book>
<book id="b002">
<title>Vincent</title>
<price>Vincent</price>
<author>Vincent</author>
</book>
<book id="b0003">
<title>Flex开发</title>
<price>98</price>
<author>胡玉勉</author>
</book>
<book id="b0003">
<title>Flex开发</title>
<price>98</price>
<author>胡玉勉</author>
</book>
<book id="b0003">
<title>Flex开发</title>
<price>98</price>
<author>胡玉勉</author>
</book>
<book id="b0003">
<title>Flex开发</title>
<price>98</price>
<author>胡玉勉</author>
</book>
</books>
原文链接:https://www.f2er.com/xml/295062.html