java对xml 增删改_Java对XML文件增删改查操作示例

本文实例讲述了java对xml文件增删改查操作。分享给大家供大家参考,具体如下:

xml文件:

哈里波特

10

这是一本很好看的书。

三国演义

10

四大名著之一。

水浒

6

四大名著之一。

红楼

5

四大名著之一。

增删改查 test.java

import java.io.file;

import java.io.fileoutputstream;

import org.w3c.dom.*;

import javax.xml.parsers.*;

import javax.xml.transform.*;

import javax.xml.transform.dom.domsource;

import javax.xml.transform.stream.*;

import javax.xml.xpath.*;

public class test {

public static void main(string[] args) {

documentbuilderfactory factory = documentbuilderfactory.newinstance();

element thebook = null, theelem = null, root = null;

try {

factory.setignoringelementcontentwhitespace(true);

documentbuilder db = factory.newdocumentbuilder();

document xmldoc = (document) db.parse(new file("test.xml"));

root = xmldoc.getdocumentelement();

// --- 新建一本书开始 ----

thebook = xmldoc.createelement("book");

theelem = xmldoc.createelement("name");

theelem.settextcontent("新书");

thebook.appendchild(theelem);

theelem = xmldoc.createelement("price");

theelem.settextcontent("20");

thebook.appendchild(theelem);

theelem = xmldoc.createelement("memo");

theelem.settextcontent("新书的更好看。");

thebook.appendchild(theelem);

root.appendchild(thebook);

system.out.println("--- 新建一本书开始 ----");

output(xmldoc);

// --- 新建一本书完成 ----

// --- 下面对《哈里波特》做一些修改。 ----

// --- 查询找《哈里波特》----

thebook = (element) selectsinglenode("/books/book[name='哈里波特']",

root);

system.out.println("--- 查询找《哈里波特》 ----");

output(thebook);

// --- 此时修改这本书的价格 -----

thebook.getelementsbytagname("price").item(0).settextcontent("15");// getelementsbytagname返回的是nodelist,所以要跟上item(0)。另外,getelementsbytagname("price")相当于xpath的".//price"。

system.out.println("--- 此时修改这本书的价格 ----");

output(thebook);

// --- 另外还想加一个属性id,值为b01 ----

thebook.setattribute("id", "b01");

system.out.println("--- 另外还想加一个属性id,值为b01 ----");

output(thebook);

// --- 对《哈里波特》修改完成。 ----

// --- 要用id属性删除《三国演义》这本书 ----

thebook = (element) selectsinglenode("/books/book[@id='b02']", root);

system.out.println("--- 要用id属性删除《三国演义》这本书 ----");

output(thebook);

thebook.getparentnode().removechild(thebook);

system.out.println("--- 删除后的xml ----");

output(xmldoc);

// --- 再将所有价格低于10的书删除 ----

nodelist somebooks = selectnodes("/books/book[price<10]", root);

system.out.println("--- 再将所有价格低于10的书删除 ---");

system.out.println("--- 符合条件的书有 " + somebooks.getlength()

+ "本。 ---");

for (int i = 0; i < somebooks.getlength(); i++) {

somebooks.item(i).getparentnode().removechild(somebooks.item(i));

}

output(xmldoc);

savexml("test1_edited.xml", xmldoc);

} catch (exception e) {

e.printstacktrace();

}

}

/**

* 将node的xml字符串输出到控制台

*

* @param node

*/

public static void output(node node) {

transformerfactory transfactory = transformerfactory.newinstance();

try {

transformer transformer = transfactory.newtransformer();

transformer.setoutputproperty("encoding", "gb2312");

transformer.setoutputproperty("indent", "yes");

domsource source = new domsource();

source.setnode(node);

streamresult result = new streamresult();

result.setoutputstream(system.out);

transformer.transform(source, result);

} catch (exception e) {

e.printstacktrace();

}

}

/**

* 查找节点,并返回第一个符合条件节点

*

* @param express

* @param source

* @return

*/

public static node selectsinglenode(string express, object source) {

node result = null;

xpathfactory xpathfactory = xpathfactory.newinstance();

xpath xpath = xpathfactory.newxpath();

try {

result = (node) xpath.evaluate(express, source, xpathconstants.node);

} catch (xpathexpressionexception e) {

e.printstacktrace();

}

return result;

}

/**

* 查找节点,返回符合条件的节点集。

* @param express

* @param source

* @return

*/

public static nodelist selectnodes(string express, object source) {

nodelist result = null;

xpathfactory xpathfactory = xpathfactory.newinstance();

xpath xpath = xpathfactory.newxpath();

try {

result = (nodelist) xpath.evaluate(express, source,

xpathconstants.nodeset);

} catch (xpathexpressionexception e) {

e.printstacktrace();

}

return result;

}

/**

* 将document输出到文件

* @param filename

* @param doc

*/

public static void savexml(string filename, document doc) {

transformerfactory transfactory = transformerfactory.newinstance();

try {

transformer transformer = transfactory.newtransformer();

transformer.setoutputproperty("indent", "yes");

domsource source = new domsource();

source.setnode(doc);

streamresult result = new streamresult();

result.setoutputstream(new fileoutputstream(filename));

transformer.transform(source, result);

} catch (exception e) {

e.printstacktrace();

}

}

}

ps:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:

在线xml/json互相转换工具:

在线格式化xml/在线压缩xml:

xml在线压缩/格式化工具:

xml代码在线格式化美化工具:

希望本文所述对大家java程序设计有所帮助。

希望与广大网友互动??

点此进行留言吧!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值