Java使用DOMparser来解析XML的用例

来源:互联网 发布:免费收支软件 编辑:程序博客网 时间:2024/05/02 03:04

查阅了一些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 xmlpublic 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 xmlpublic 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-parentpublic 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 valuepublic 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 tagnamepublic 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);            }        }        }}


0 0
原创粉丝点击