📄 code 17.2.4-2.txt
字号:
import com.ibm.xml.parser.TXDocument;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import java.io.PrintWriter;
public class MakeStateDoc {
public static void main(String[] argv) {
try {
// Create Document object
Document doc = new TXDocument();
// Make tag as root, and add it
Element root = doc.createElement("state");
root.setAttribute("stateid", "MN");
// Make element and add it
Element elem = doc.createElement("city");
elem.setAttribute("cityid", "mn12");
root.appendChild(elem);
// Make element and add it
elem = doc.createElement("name");
elem.appendChild(doc.createTextNode("Johnson"));
root.appendChild(elem);
// Make element and add it
elem = doc.createElement("population");
elem.appendChild(doc.createTextNode("5000"));
root.appendChild(elem);
// State has city, name, and population
doc.appendChild(root);
// Show the XML document
((TXDocument )doc).setVersion("1.0");
((TXDocument )doc).printWithFormat(new PrintWriter(System.out));
} catch (Exception e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -