?? testdom2.java
字號:
package DOM;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import org.apache.xml.serialize.*;
public class TestDOM2 {
@SuppressWarnings({ "deprecation", "deprecation" })
public static void main(String[] args) {
Document document = null;
DocumentBuilderFactory fabrique = null;
try {
fabrique = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = fabrique.newDocumentBuilder();
document = builder.newDocument();
Element racine = (Element) document.createElement("bibliotheque");
document.appendChild(racine);
Element livre = (Element) document.createElement("livre");
livre.setAttribute("style", "1");
Attr attribut = document.createAttribute("type");
attribut.setValue("broche");
livre.setAttributeNode(attribut);
racine.appendChild(livre);
livre.setAttribute("style2", "1");
Element titre = (Element) document.createElement("titre");
titre.appendChild(document.createTextNode("Titre 1"));
livre.appendChild(titre);
racine.appendChild(document.createComment("mon commentaire"));
XMLSerializer ser = new XMLSerializer(System.out,
new OutputFormat("xml", "UTF-8", true));
ser.serialize(document);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -