Java使用DOMparser来解析XML的用例
2016-10-13 21:58
162 查看
查阅了一些Java中的xml解析,感觉Java真是一门写字好多的语言啊!
我想我肯定记不住的,就写了一个文件总结了一下,在其他程序里面拿起来直接用也是很方便的
其中的功能包括以下几点:
生成一个XML然后返回他的doc
给一个指定XML,读取文件然后返回这个XML的doc
根据doc和要存储的文件路径,保存这个doc到XML里面
在一个节点下面插入一个新的子节点(给定节点名称和值)
在一个doc中寻找所有以tagname为名字的节点,返回这些节点的内容(放在链表里)
在一个doc中寻找所有以tagname为名字的节点,删掉这些节点
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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 javax.xml.parsers.DocumentBuilder;
public class XmlManipulate {
public static Document CreateXML() throws ParserConfigurationException {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
return doc;
}
//read from xml
public static Document ReadXML(String path) throws IOException, ParserConfigurationException, SAXException {
File inputFile = new File(path);
DocumentBuilderFactory dbFactory
= DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
return doc;
}
//store the xml
public static void StoreXML(Document doc, String path) throws ParserConfigurationException, TransformerException{
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(path));
transformer.transform(source, result);
System.out.println("File saved!");
}
//add a node <tagname>value</tagname> below the node-parent
public static void AddNode(String tagName, String value, Node parent) {
Document dom = parent.getOwnerDocument();
Node node = dom.createElement(tagName);
Text nodeVal = dom.createTextNode(value);
Node c = node.appendChild(nodeVal);
parent.appendChild(node);
}
//find all the tag named tagname, and return their value
public static ArrayList<String> FindTag(String tagname, Document doc) {
ArrayList<String> res = new ArrayList<String>();
NodeList nList = doc.getElementsByTagName(tagname);
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
String content = nNode.getTextContent();
//System.out.println(content);
res.add(content);
}
}
if(res.isEmpty()) return null;
return res;
}
//delete all the tag named tagname
public static void DeleteTag(String tagname, Document doc) {
NodeList nList = doc.getElementsByTagName(tagname);
int len = nList.getLength();
for (int temp = 0; temp < len; temp++) {
Node nNode = nList.item(0);
if(nNode != null) {
Node parent = nNode.getParentNode();
parent.removeChild(nNode);
}
}
}
}
我想我肯定记不住的,就写了一个文件总结了一下,在其他程序里面拿起来直接用也是很方便的
其中的功能包括以下几点:
生成一个XML然后返回他的doc
给一个指定XML,读取文件然后返回这个XML的doc
根据doc和要存储的文件路径,保存这个doc到XML里面
在一个节点下面插入一个新的子节点(给定节点名称和值)
在一个doc中寻找所有以tagname为名字的节点,返回这些节点的内容(放在链表里)
在一个doc中寻找所有以tagname为名字的节点,删掉这些节点
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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 javax.xml.parsers.DocumentBuilder;
public class XmlManipulate {
public static Document CreateXML() throws ParserConfigurationException {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
return doc;
}
//read from xml
public static Document ReadXML(String path) throws IOException, ParserConfigurationException, SAXException {
File inputFile = new File(path);
DocumentBuilderFactory dbFactory
= DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
return doc;
}
//store the xml
public static void StoreXML(Document doc, String path) throws ParserConfigurationException, TransformerException{
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(path));
transformer.transform(source, result);
System.out.println("File saved!");
}
//add a node <tagname>value</tagname> below the node-parent
public static void AddNode(String tagName, String value, Node parent) {
Document dom = parent.getOwnerDocument();
Node node = dom.createElement(tagName);
Text nodeVal = dom.createTextNode(value);
Node c = node.appendChild(nodeVal);
parent.appendChild(node);
}
//find all the tag named tagname, and return their value
public static ArrayList<String> FindTag(String tagname, Document doc) {
ArrayList<String> res = new ArrayList<String>();
NodeList nList = doc.getElementsByTagName(tagname);
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
String content = nNode.getTextContent();
//System.out.println(content);
res.add(content);
}
}
if(res.isEmpty()) return null;
return res;
}
//delete all the tag named tagname
public static void DeleteTag(String tagname, Document doc) {
NodeList nList = doc.getElementsByTagName(tagname);
int len = nList.getLength();
for (int temp = 0; temp < len; temp++) {
Node nNode = nList.item(0);
if(nNode != null) {
Node parent = nNode.getParentNode();
parent.removeChild(nNode);
}
}
}
}
相关文章推荐
- Java中使用DOM来解析xml文档
- 使用Java自带SAX工具解析XML
- 使用Java自带SAX工具解析XML
- Java使用JDOM解析XML(转载,简单又详细)
- JAVA使用DOM解析XML
- java解析xml之一:使用SAX与DOM
- java 使用 dom4j 解析 XML
- 使用Java自带SAX工具解析XML
- Java学习之路——使用DOM解析XML文档
- java学习笔记——使用DOM解析XML和使用SAX解析XML
- java解析xml之一:使用SAX与DOM
- 使用DOM解析XML文档示例(Java)
- [备份]java中XML解析利器-dom4j的使用简介
- 【JAVA】java:使用XMLReaderFactory类和XMLReader类解析XML文档
- 使用Digester解析XML文件和JAVA对象的映射
- java解析xml之三:使用domj4
- [备份]java中XML解析利器-dom4j的使用简介
- 【原创】使用DOM解析XML文档示例(Java)
- [JAVA100例]070、使用JDOM解析XML
- Java使用DOM解析XML文件