App下載
首頁(yè)javadomJava HTML/XML - 如何創(chuàng)建用于修改的XML實(shí)用程序 - 添加,刪除/刪除,更改/修改

Java HTML/XML - 如何創(chuàng)建用于修改的XML實(shí)用程序 - 添加,刪除/刪除,更改/修改

我們想知道如何創(chuàng)建用于修改的XML實(shí)用程序 - 添加,刪除/刪除,更改/修改。
import java.io.ByteArrayInputStream;
import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

class XMLUtil {
  static XPath xpath = XPathFactory.newInstance().newXPath();
  Document doc;
  Element root;

  public XMLUtil(String xml) throws Exception {
    doc = parseXml(xml);
    root = doc.getDocumentElement();
  }

  private static Document parseXml(String xml) throws Exception {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    ByteArrayInputStream bis = new ByteArrayInputStream(xml.getBytes());
    return docBuilder.parse(bis);
  }

  private String asXPath(String path) {
    return path.startsWith("/") ? path : "http://" + path;
  }

  private static Node findNode(Document doc, String xPath) throws Exception {
    XPathExpression expr = xpath.compile(xPath);
    return (Node) expr.evaluate(doc, XPathConstants.NODE);
  }

  public static XMLUtil createXml(String xml) throws Exception {
    return new XMLUtil(xml);
  }

  public XMLUtil addNode(String path, String xml) throws Exception {
    Document subDoc = parseXml(xml);
    Node destNode = findNode(doc, asXPath(path));
    Node srcNode = subDoc.getFirstChild();
    destNode.appendChild(doc.adoptNode(srcNode.cloneNode(true)));
    return this;
  }

  public XMLUtil removeNode(String path) throws Exception {
    Node destNode = findNode(doc, asXPath(path));
    destNode.getParentNode().removeChild(destNode);
    return this;
  }

  public XMLUtil addAttribute(String path, String attr, String value)
      throws Exception {
    Element destNode = (Element) findNode(doc, asXPath(path));
    destNode.setAttribute(attr, value);
    return this;
  }

  public XMLUtil removeAttribute(String path, String attr) throws Exception {
    Element destNode = (Element) findNode(doc, asXPath(path));
    destNode.removeAttribute(attr);
    return this;
  }

  public String docToString(Document doc) {
    try {
      Transformer transformer = TransformerFactory.newInstance()
          .newTransformer();
      transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
      transformer.setOutputProperty(OutputKeys.INDENT, "yes");
      transformer.setOutputProperty(
          "{http://xml.apache.org/xslt}indent-amount", "2");
      StringWriter sw = new StringWriter();
      transformer.transform(new DOMSource(doc), new StreamResult(sw));
      return sw.toString();
    } catch (Exception e) {
      return "";
    }
  }

  public String toString() {
    return docToString(doc);
  }
}