App下載
首頁javadomJava HTML/XML - 如何dom設置元素錯誤

Java HTML/XML - 如何dom設置元素錯誤

我們想知道如何dom設置元素錯誤。
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
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;

public class Main {
  public static void main(String[] args) throws Throwable {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);

    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.newDocument();

    Element root = doc.createElement("root");
    root.setAttribute("xmlns:m", "http://hgci.cn/blog");
    root.setAttribute("xmlns:rt", "http://hgci.cn/forum");
    doc.appendChild(root);

    Element elt = doc.createElement("simple");
    elt.setAttribute("m:Path", "false");
    elt.setAttribute("m:Content", "false");
    elt.setAttribute("rt:file", "false");

    root.appendChild(doc.createTextNode("\n\t"));
    root.appendChild(elt);
    root.appendChild(doc.createTextNode("\n"));
    TransformerFactory.newInstance().newTransformer()
        .transform(new DOMSource(doc), new StreamResult(System.out));
  }
}