jom写文件实现,jdom读取XML文件,解析某一element并改值存入文

1.建立一个xml文件
TestJDOM.java
package com.jdom.write;

import java.io.FileWriter;
import java.io.IOException;

import org.jdom.Content;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class TestJDOM {

public static void main(String[] args) {
Element rootElement = new Element("MyInfo");
Document myDocument = new Document(rootElement);

// Attribute rootAttri = new Attribute("comment","introduce myself");
// rootElement.setAttribute(rootAttri);

// comment属性名称,"introduce myself"属性值,作用等同上面两句。
rootElement.setAttribute("comment", "introduce myself");
// rootElement.setAttribute(new Attribute("comment","introduce
// myself"));
// Element sexElement = new Element("sex");
// rootElement.addContent(sexElement);

// Element nameElement = new Element("name");
// nameElement.addContent("kingwong");
// rootElement.addContent(nameElement);

for (int i = 0; i < 5; i++) {
// 三种加入内容的不同形式addContent方法可以嵌套。
rootElement.addContent((Content) (new Element("name")
.addContent("kingwong")));
Element guo = new Element("sex");
guo.setAttribute("value", "male");
guo.setAttribute("v", String.valueOf(i));
rootElement.addContent(guo);
rootElement.addContent((Content) (new Element("contract")
.addContent((Content) (new Element("telephone")
.setAttribute("telephone", "87654321")))));
}
// 删除
// rootElement.removeChild("sex");

XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());
try {
xmlOut.output(myDocument, System.out);
// // xmlOut.output(rootElement.getChild("name"),System.out);
// String outString = xmlOut.outputString(myDocument);
FileWriter writer = new FileWriter("src/myFile.xml");
xmlOut.output(myDocument, writer);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
执行结果如下:
<?xml version="1.0" encoding="UTF-8"?>
<MyInfo comment="introduce myself">
<name>kingwong</name>
<sex value="male" v="0" />
<contract>
<telephone telephone="87654321" />
</contract>
<name>kingwong</name>
<sex value="male" v="1" />
<contract>
<telephone telephone="87654321" />
</contract>
<name>kingwong</name>
<sex value="male" v="2" />
<contract>
<telephone telephone="87654321" />
</contract>
<name>kingwong</name>
<sex value="male" v="3" />
<contract>
<telephone telephone="87654321" />
</contract>
<name>kingwong</name>
<sex value="male" v="4" />
<contract>
<telephone telephone="87654321" />
</contract>
</MyInfo>

读取有条件并修改xml文件
TestJDOM2.java
package com.jdom.read;

import java.io.FileWriter;
import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

/**
* @author kingwong
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class TestJDOM2 {
public static void main(String[] args) {
SAXBuilder sb = new SAXBuilder();
try {
Document doc = sb.build("src/myFile.xml");

Element root = doc.getRootElement();

String str1 = root.getAttributeValue("comment");
System.out.println("Root Element's comment attribute is : " + str1);

String str2 = root.getChild("sex").getAttributeValue("value");
System.out.println("sex Element's value attribute is : " + str2);
List list = root.getChildren("sex");
Iterator it = list.iterator();
while (it.hasNext()) {
Element guo = (Element) it.next();
if ("2".equals(guo.getAttribute("v").getValue()))
guo.getAttribute("value").setValue("郭秀志实验品");
System.out.println(guo.getAttribute("v").getValue());
}

// root.removeChildren("sex");
// root.getChild("sex").getChildren().setAttribute("value","guo");
// String str3 = root.getChildText("name");
String str3 = root.getChild("name").getText();
System.out.println(list.size() + "name Element's content is :"
+ str3);

// System.out.println("contact Element's telephone subelement
// content is : " + str4 + "/n");
// Element inputElement = root.getChild("contact");
// inputElement.addContent(new
// Element("email").setAttribute("value","wanghua@cyberobject.com"));

// 设置字符集后就可以读写汉字
XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat()
.setEncoding("gb2312"));
String outStr = xmlOut.outputString(root);
System.out.println(outStr);
// 写入文件
FileWriter writer = new FileWriter("src/myFile.xml");
xmlOut.output(doc, writer);
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
执行效果如下:
Root Element's comment attribute is : introduce myself
sex Element's value attribute is : male
0
1
2
3
4
5name Element's content is :kingwong
<MyInfo comment="introduce myself">
<name>kingwong</name>
<sex value="male" v="0" />
<contract>
<telephone telephone="87654321" />
</contract>
<name>kingwong</name>
<sex value="male" v="1" />
<contract>
<telephone telephone="87654321" />
</contract>
<name>kingwong</name>
<sex value="郭秀志实验品" v="2" />
<contract>
<telephone telephone="87654321" />
</contract>
<name>kingwong</name>
<sex value="male" v="3" />
<contract>
<telephone telephone="87654321" />
</contract>
<name>kingwong</name>
<sex value="male" v="4" />
<contract>
<telephone telephone="87654321" />
</contract>
</MyInfo>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值