需要的jar包 jdom.jar
下载地址:
1.读取xml文件中的元素
a. abc.xml
-
xml version="1.0" encoding="gb2312"?>
-
<messages>
-
<message id="1">
-
<title>11title>
-
<content>
-
<name>lvpingyuname>
-
<age>23age>
-
content>
-
<email>33email>
-
message>
-
<message id="2">
-
<title>44title>
-
<content>
-
<name>lvpingyuname>
-
<age>23age>
-
content>
-
<email>66email>
-
message>
-
messages>
11
lvpingyu
23
33
44
lvpingyu
23
66
b.如何读取 代码如下:
-
package test;
-
-
import java.io.FileInputStream;
-
import java.util.List;
-
-
import org.jdom.Document;
-
import org.jdom.Element;
-
import org.jdom.input.SAXBuilder;
-
-
public class MyJdom {
-
public static void main(String[] args) throws Exception {
-
SAXBuilder sb = new SAXBuilder();
-
String path="abc.xml";
-
Document doc = sb.build(new FileInputStream(path));
-
Element root = doc.getRootElement();
-
List list = root.getChildren();
-
for (int i = 0; i < list.size(); i++) {
-
System.out.println("---------------------------");
-
Element item = (Element) list.get(i);
-
String id = item.getAttribute("id").getValue();
-
System.out.println("id-->" + id);
-
Element sub = item.getChild("title");
-
String text = sub.getText();
-
System.out.println("Title-->" + text);
-
if (item.getChild("content").getChildren().size() > 0) {
-
Element sub2 = item.getChild("content").getChild("name");
-
String text2 = sub2.getText();
-
System.out.println("name-->" + text2);
-
}
-
Element sub3 = item.getChild("email");
-
String text3 = sub3.getText();
-
System.out.println("Email-->" + text3);
-
-
}
-
System.out.println("---------------------------");
-
}
-
-
}
package test;
import java.io.FileInputStream;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
public class MyJdom {
public static void main(String[] args) throws Exception {
SAXBuilder sb = new SAXBuilder();//建立构造器
String path="abc.xml";
Document doc = sb.build(new FileInputStream(path));//读入指定文件
Element root = doc.getRootElement();//获得根节点
List list = root.getChildren();//将根节点下的所有子节点放入List中
for (int i = 0; i < list.size(); i++) {
System.out.println("---------------------------");
Element item = (Element) list.get(i);//取得节点实例
String id = item.getAttribute("id").getValue();//取得属性值
System.out.println("id-->" + id);
Element sub = item.getChild("title");//取得当前节点的子节点
String text = sub.getText();//取得当前节点的值
System.out.println("Title-->" + text);
if (item.getChild("content").getChildren().size() > 0) {
Element sub2 = item.getChild("content").getChild("name");
String text2 = sub2.getText();
System.out.println("name-->" + text2);
}
Element sub3 = item.getChild("email");
String text3 = sub3.getText();
System.out.println("Email-->" + text3);
}
System.out.println("---------------------------");
}
}
2.生成xml文件
-
package test;
-
-
import java.io.FileNotFoundException;
-
import java.io.FileOutputStream;
-
import java.io.IOException;
-
-
import org.jdom.Document;
-
import org.jdom.Element;
-
import org.jdom.output.XMLOutputter;
-
-
public class JavaXML {
-
public void buildXML() throws Exception {
-
String path = "company_list.xml";
-
-
Element root = new Element("list");
-
-
Document Doc = new Document(root);
-
-
for (int i = 0; i < 5; i++) {
-
-
Element elements = new Element("company");
-
-
elements.setAttribute("id", "" + i);
-
-
-
-
-
elements.addContent(new Element("company_name").setText("name" + i));
-
elements.addContent(new Element("company_email").setText("@" + i
-
+ ".com"));
-
-
-
root.addContent(elements);
-
}
-
XMLOutputter XMLOut = new XMLOutputter();
-
-
XMLOut.output(Doc, new FileOutputStream(path));
-
}
-
-
public static void main(String[] args) {
-
JavaXML javaXML = new JavaXML();
-
try {
-
javaXML.buildXML();
-
} catch (Exception e) {
-
-
e.printStackTrace();
-
}
-
-
}
-
-
}
package test;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
public class JavaXML {
public void buildXML() throws Exception {
String path = "company_list.xml";
// 创建根节点 list;
Element root = new Element("list");
//根节点添加到文档中;
Document Doc = new Document(root);
// 此处 for 循环可替换成 遍历 数据库表的结果集操作;
for (int i = 0; i < 5; i++) {
// 创建子节点 company;
Element elements = new Element("company");
// 给 company 节点添加属性 id;
elements.setAttribute("id", "" + i);
// 给 company 节点添加子节点并赋值
// new Element("company_name")中的 "company_name"
//替换成表中相应字段,setText("name")中 "name 替换成表中记录值;
elements.addContent(new Element("company_name").setText("name" + i));
elements.addContent(new Element("company_email").setText("@" + i
+ ".com"));
// 给父节点list添加company子节点;
root.addContent(elements);
}
XMLOutputter XMLOut = new XMLOutputter();
// 输出company_list.xml文件;
XMLOut.output(Doc, new FileOutputStream(path));
}
public static void main(String[] args) {
JavaXML javaXML = new JavaXML();
try {
javaXML.buildXML();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
可参考的资料:参考资料:
JDOM V1.0 API在线参考手册()
JDOM V1.0下载()
JDOM处理XML快速上手
(http://kingwong.blogdriver.com/kingwong/291878.html)
*注:本文转自http://blog.csdn.net/java_cxrs/article/details/5474815
阅读(468) | 评论(0) | 转发(0) |