您的位置:首页 > 编程语言 > Java开发

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 java