import java.io.*;import org.jdom.*;import org.jdom.input.*;import org.jdom.input.*;import org.jdom.output.*;public class test1 { public void BuildXMLDoc() throws IOException, JDOMException { Element eeeRoot, eee1, eee2; Document Doc; eeeRoot = new Element("employees_information"); Doc = new Document(eeeRoot); eeeRoot = Doc.getRootElement(); eee1 = new Element("name"); eee2 = eee1.setText("C.Y. Shen"); //eee2 = eee1.addAttribute("emp_id", "001"); eee1 = eeeRoot.addContent(eee2); eee1 = new Element("age"); eee2 = eee1.setText("43"); eee1 = eeeRoot.addContent(eee2); eee1 = new Element("sex"); eee2 = eee1.setText("Male"); eee1 = eeeRoot.addContent(eee2); XMLOut.setEncoding("gb2312") ; XMLOutputter XMLOut = new XMLOutputter(); XMLOut.output(Doc, new FileOutputStream("test1.xml")); } public static void main(String[] args) { try { test1 s1 = new test1(); System.out.println("Now we build an XML document ....."); s1.BuildXMLDoc(); } catch (Exception e) { System.out.println(e.getMessage()); } }}
输出如下:
<?xml version="1.0" encoding="gb2312" ?>
- <employees_information>
<record>
<name>C.Y. Shen</name>
<age>43</age>
<sex>Male</sex>
</employees_information>
输出如下:
<?xml version="1.0" encoding="gb2312" ?>
- <employees_information>
<record>
<name>C.Y. Shen</name>
<age>43</age>
<sex>Male</sex>
</employees_information>
解决方案 »
- 为什么java只支持单继承?多继承的缺点在接口也同样存在
- hibernate中的lazy与fetch有什么区别
- mysql5.6.11免安装版为何插入不了汉字,如何设置编码格式呢?
- Tomcat下 URL 如果带有汉字,如:http://localhost:8080/app/temp/upload/非技术.jpg 就显示不出来,大虾帮帮忙
- 跪求在My Eclipse4.0上配置hibernate方法!
- 请问在那里可以下载到完整的j2ee规范,谢谢
- tomcat大问题,很难的说
- 我在sun网站下载的j2se1.5,一共43.9M,安装时复制到最后报错:1335,DATA1.cab不能用.是我下的文件有问题吗?
- 这是怎么提交的啊?
- java字符串对象的intern方法问题
- TOMCAT5.0的连接池配置在WIN2000下没问题,怎么换到solaris下就有问题拉??在线等!!!
- 50分!请问如何改变一个jtable中cell的颜色(背景)?
import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.apache.crimson.tree.XmlDocument;public class XmlManager {
public void writerXml(String file) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
String[] name = {
"搨憁", "屽嬻", "挅敧夲", "嵐憁", };
int[] age = {
35, 23, 34, 32};
String[] phone = {
"1385326897", "13675516137", "1367551613", "13956002134"};
String[] address = {
"廈崅姳巕掜妛峑", "壴壥嶳嗨搷壥昳婎抧", "崅榁彲愻梺埪杸拞怱",
"棳嵐壨帯\u6C61\u603B巜\u6325\u5904"};
Element root = doc.createElement("惣揤庢恖柤");
doc.appendChild(root);
for (int i = 0; i < name.length; i++) {
Element member = doc.createElement("恖");
root.appendChild(member);
// member.setAttribute("惈", "抝");
Element Name = doc.createElement("惄柤");
Node tName = doc.createTextNode(name[i]);
Name.appendChild(tName);
member.appendChild(Name); Element Sex=doc.createElement("sex");
// Node tSex=doc.createTextNode("man");
Element tSex=doc.createElement("man");
Sex.appendChild(tSex);
member.appendChild(Sex); Element Age = doc.createElement("擭");
Node tAge = doc.createTextNode(String.valueOf(age[i]));
Age.appendChild(tAge);
member.appendChild(Age); Element Phone = doc.createElement("宯");
Text tPhone = doc.createTextNode(phone[i]);
Phone.appendChild(tPhone);
member.appendChild(Phone); Element Address = doc.createElement("宯抧毈");
Node tAddress = doc.createTextNode(address[i]);
Address.appendChild(tAddress);
member.appendChild(Address);
}
DataOutputStream out = new DataOutputStream(new FileOutputStream(file));
( (XmlDocument) doc).write(out);
out.close();
} public void readXML(String file) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file); Element root = doc.getDocumentElement();
System.out.println(root.getNodeName()); NodeList nodes = root.getChildNodes();
reuse(nodes); } public void reuse(NodeList nodeList) {
for (int i = 0; i < nodeList.getLength(); i++) {
Node s = nodeList.item(i);
if (s.hasChildNodes()) {
System.out.println(s.getNodeName());
NodeList lst = s.getChildNodes();
reuse(lst);
}
else {
System.out.println(s.getNodeValue());
}
}
}
}
import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.apache.crimson.tree.XmlDocument;public class XmlManager {
public void writerXml(String file) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
String[] name = {
"唐僧", "悟空", "猪八戒", "沙僧", };
int[] age = {
35, 23, 34, 32};
String[] phone = {
"1385326897", "13675516137", "1367551613", "13956002134"};
String[] address = {
"州高干子弟学校", "花果山猴桃果品基地", "高老庄洗浴按摩中心",
"流沙河治\u6C61\u603B指\u6325\u5904"};
Element root = doc.createElement("西天取人名");
doc.appendChild(root);
for (int i = 0; i < name.length; i++) {
Element member = doc.createElement("人");
root.appendChild(member);
// member.setAttribute("性", "男");
Element Name = doc.createElement("姓名");
Node tName = doc.createTextNode(name[i]);
Name.appendChild(tName);
member.appendChild(Name); Element Sex=doc.createElement("sex");
// Node tSex=doc.createTextNode("man");
Element tSex=doc.createElement("man");
Sex.appendChild(tSex);
member.appendChild(Sex); Element Age = doc.createElement("年");
Node tAge = doc.createTextNode(String.valueOf(age[i]));
Age.appendChild(tAge);
member.appendChild(Age); Element Phone = doc.createElement("系");
Text tPhone = doc.createTextNode(phone[i]);
Phone.appendChild(tPhone);
member.appendChild(Phone); Element Address = doc.createElement("系地址");
Node tAddress = doc.createTextNode(address[i]);
Address.appendChild(tAddress);
member.appendChild(Address);
}
DataOutputStream out = new DataOutputStream(new FileOutputStream(file));
( (XmlDocument) doc).write(out);
out.close();
} public void readXML(String file) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file); Element root = doc.getDocumentElement();
System.out.println(root.getNodeName()); NodeList nodes = root.getChildNodes();
reuse(nodes); } public void reuse(NodeList nodeList) {
for (int i = 0; i < nodeList.getLength(); i++) {
Node s = nodeList.item(i);
if (s.hasChildNodes()) {
System.out.println(s.getNodeName());
NodeList lst = s.getChildNodes();
reuse(lst);
}
else {
System.out.println(s.getNodeValue());
}
}
}
}
http://www-900.ibm.com/developerWorks/cn/java/j-jdom/index.shtml#1