`

JDom解析XML

    博客分类:
  • XML
 
阅读更多
XML文件:

<?xml version = '1.0'?>
<DataSource>
<param database="mysql" version="5.0">
<driver>com.mysql.jdbc.Driver</driver>
<url>jdbc:mysql://localhost:3306/j1101</url>
<user>root</user>
<password>root</password>
</param>

<param database="Oracle" version="10G">
<driver>oracle.jdbc.driver.OracleDriver</driver>
<url>jdbc:oracle:thin:@127.0.0.1:1521:orcl</url>
<user>test</user>
<password>test</password>
</param>

</DataSource>




JDom解析scr目录下data-sources.xml文件:public class JdomDemo {
// 解析
public void parse() {
// 1.构建SAXBuilder对象
SAXBuilder builder = new SAXBuilder();
try {
// 2.获取文档对象
Document document = builder.build("data-sources.xml");
// 3.获取根节点,获取根节点
org.jdom.Element root = document.getRootElement();
System.out.println("根节点:" + root.getName());// 根节点的名字
// 3.获取根节点下的所有的子节点
List<Element> childList = root.getChildren();
for (Element e : childList) {
List<Attribute> attritues = e.getAttributes(); // 当前元素的属性的集合
for (Attribute a : attritues) {// 遍历Attribute(属性)的集合
System.out.println(a.getName() + "\t " + a.getValue());
}
// 查找节点值
List<Element> c = e.getChildren();// 将param元素下的子元素保存进集合中
for (Element valueChild : c) { // 遍历子元素集合、

System.out.println(valueChild.getName() + "===="
+ valueChild.getValue());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

// 根据元素名字来解析
public void parse2() {
// 1.构建SAXBuilder对象
SAXBuilder builder = new SAXBuilder();
try {
// 2.获取文档对象
Document document = builder.build("data-sources.xml");
// 3.获取根节点,获取根节点
org.jdom.Element root = document.getRootElement();
System.out.println("根节点:" + root.getName());// 根节点的名字
// 假设我们已经知道XML文档的内容 的情况下:
List<Element> childList = root.getChildren("param");
for (Element child : childList) {
String database = child.getAttribute("database").getValue();
String version = child.getAttribute("version").getValue();
String driver = child.getChildTextTrim("driver");
String url = child.getChildTextTrim("url");
String user = child.getChildTextTrim("user");
String password = child.getChildTextTrim("password");
System.out.println("database-->" + database);
System.out.println("version-->" + version);
System.out.println("driver-->" + driver);
System.out.println("url-->" + url);
System.out.println("user-->" + user);
System.out.println("password-->" + password);
}
} catch (Exception e) {
e.printStackTrace();
}
}

public void parseMenu() {

// 1.构建SAXBuilder对象
SAXBuilder builder = new SAXBuilder();
try {
// 2.获取文档对象
Document document = builder.build("menu.xml");
// 3.获取根节点,获取根节点
org.jdom.Element root = document.getRootElement();
System.out.println("根节点:" + root.getName());// 根节点的名字
// 3.获取根节点下的所有的子节点
List<Element> childList = root.getChildren();
for (Element e : childList) {
List<Attribute> attritues = e.getAttributes(); // 当前元素的属性的集合
for (Attribute a : attritues) {// 遍历Attribute(属性)的集合
System.out.println(a.getName() + "\t " + a.getValue());
}
// 查找节点值
List<Element> c = e.getChildren();// 将param元素下的子元素保存进集合中
for (Element valueChild : c) { // 遍历子元素集合、
List<Attribute> childAttritues = valueChild.getAttributes(); // 当前元素的属性的集合
for (Attribute a : childAttritues) {// 遍历Attribute(属性)的集合
System.out.println(a.getName() + "\t " + a.getValue());
}
System.out.println(valueChild.getName() + "===="
+ valueChild.getValue());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

/*
* jdom书写XML文档
*/
public void addXML() {
// 1.创建根节点 root element
Element root = new Element("DataSource");
// 传递根元素创建基于根元素的文档对象
Document document = new Document(root); // <DataSource>
// 2.创建子节点,根据传递进去的名字创建一个这样的元素
Element paramNode = new Element("param"); // <param>
// 设置属性 接要下来就要为这个元素添加属性了 database="mysql" version="5.0">
paramNode.setAttribute("database", "mysql");
paramNode.setAttribute("version", "5.0");
// 3.添加该子节点下的子节点。例如: <driver><url><user><password>这些节点
Element driver = new Element("driver");
driver.setText("com.mysql.jdbc.Driver");
Element url = new Element("url");
url.setText("jdbc:mysql://localhost:3306/j1101");
Element user = new Element("user");
user.setText("root");
Element password = new Element("password");
password.setText("root");
// 4.将这些 <driver><url><user><password>这些节点添加到 param节点中
paramNode.addContent(driver);
paramNode.addContent(url);
paramNode.addContent(user);
paramNode.addContent(password);
// 5.将 param 节点 加入到 root中
root.addContent(paramNode);
// 设置字符集编码 encoding
Format format = Format.getPrettyFormat().setEncoding("gb2312");
XMLOutputter out = new XMLOutputter(format);

try {
out.output(document, new FileOutputStream("test.xml"));
System.out.println("使用 jdom  成功创建了 xml 文件");
} catch (Exception e) {
System.out.println("你杯具了");
e.printStackTrace();
}
}

/*
* 采用jdom来实现xml的修改
*/
public void testUpdate() {
SAXBuilder builder = new SAXBuilder();
try {
Document document = builder.build("test.xml");
// 获取根节点
Element root = document.getRootElement();
// 通过根节点获取子节点,将子节点保存在集合中
List<Element> childList = root.getChildren();
// 遍历集合拿取数据
for (Element child : childList) {
/*
* 我们想去修改 database="mysql" 的 url
* 修改为:<url>jdbc:mysql://127.0.0.1:3306/mysql</url>
*/
// if("mysql".equals(child.getAttribute("database").getValue())){}
if ("mysql".equals(child.getAttributeValue("database"))) {
// 修改当前的元素的url的值
child.getChild("url").setText(
"jdbc:mysql://127.0.0.1:3306/mysql");
}
}

XMLOutputter out = new XMLOutputter();
// 设置 缩进格式 以及编码方式
out.setFormat(Format.getPrettyFormat().setEncoding("gb2312"));
out.output(document, new FileOutputStream("testUpdate.xml"));
System.out.println("修改成功");

} catch (Exception e) {
System.out.println("修改失败");
e.printStackTrace();
}
}

/*
* 如何删除
*/

public void testDelete() {
/* 想要实现删除 test.xml 文件中的 user,password这两个元素 */
SAXBuilder builder = new SAXBuilder();
try {
Document document = builder.build("test.xml");
Element root = document.getRootElement();
// List<Element> childList=root.getChildren();
List<Element> paramList = root.getChildren("param");
for(Element child : paramList){
if("mysql".equals(child.getAttributeValue("database"))){
//删除掉指定元素(节点)
child.removeChild("user");
child.removeChild("password");

/*//删除根目录下的所有子节点
root.removeContent(child);
break;
*/
}
}
XMLOutputter out =new XMLOutputter();
out.setFormat(Format.getPrettyFormat().setEncoding("gb2312"));
out.output(document, new FileOutputStream("test2.xml"));
System.out.println("删除成功");
} catch (Exception e) {
System.out.println("删除失败");
e.printStackTrace();
}
}

public static void main(String[] args) {
JdomDemo j = new JdomDemo();
// j.parseMenu();
// j.addXML();
j.testDelete();
}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics