java web 集成dom4j_JavaWeb_使用dom4j解析、生成XML文件

dom4j  官网

xml解析DOM文档对象模型(树形结构)

DOM方式解析:把xml文档加载到内存形成树形结构,可以进行增删改的操作

Learn

使用dom4j解析文件"NewFile.xml"

使用dom4j生成XML文件

添加jar包进Project中  百度云  传送门  密码:7c8x

211aca48cb2132ec588011e6e1953e05cc3.jpg

839b4f74b0af190ebb58796383e371f160d.jpg

1,解析根元素

2,解析有哪些子元素

3,解析一个元素又哪些属性

4,得到元素的文本内容

5,修改、添加、删除某个元素节点

6,修改、添加、删除某个属性

解析的时候可以做的事情

Gary->New->Folder 新建一个lib文件夹

Ctrl C+Ctrl V 将dom4j.jar包复制到lib文件夹中

dom4j.jar->Build Path->Add to Build Path

dom4j解析文件"NewFile.xml"

c7c674fe86b9c75316ed37c8449caeec.png

99348914515f34bb0c6327b89767bf8debc.jpg

f25b96379d1aa30d284845072eaf79c961a.jpg

12

香蕉

广州

39

苹果

北京

33

芒果

深圳

NewFile.xml

ParseXML解析NewFile.xml中的Dom元素

5f4f88cbcb44da738857de18a0add940.png

1c8e2b317251d5c7e2f57890e598d0b25a4.jpg

0c2c9a48ffef4dc1d5196776ca5a3c591ae.jpg

packageDuoxiancheng;importjava.util.Iterator;importorg.dom4j.Attribute;importorg.dom4j.Document;importorg.dom4j.Element;importorg.dom4j.io.SAXReader;public classParseXML {public static void main(String[] args) throwsException{

SAXReader reader= newSAXReader();//获得xml文件路径

Document document = reader.read("src/NewFile.xml");//获得根节点名

Element root =document.getRootElement();

System.out.println(root.getName());//获取子元素

Iterator it =root.elementIterator();while(it.hasNext()) {

Element ele=it.next();//获取子元素为name中的文本值//存在good中没文本会抛出java.lang.NullPointerException空指针异常

if(ele.getName().equals("good")) {

Element name= ele.element("name");//if(name!=null)

System.out.println(name.getText());

}//获得子元素名

System.out.println(ele.getName());

Iterator attributes =ele.attributeIterator();while(attributes.hasNext()) {

Attribute ab=attributes.next();

System.out.println(ab.getName()+":"+ab.getValue());

}

}//xml :Element Attribute//函数方法输入.后查看

Element ele = null;//ele.elementIterator(); 遍历方法//ele.

Attribute ab= null;//ab.

}

}

ParseXML.class

输出:

goodlist

香蕉

good

苹果

good

芒果

good

dom4j生成XML文件

ded101157ac62595dcfe358c69940a37.png

d5a6b713ed3a6659aee7f395b4fd91cc33c.jpg

ff18e8e6646354c3a592b370ec463f1862f.jpg

packageDuoxiancheng;importjava.io.FileWriter;importjava.io.IOException;importorg.dom4j.Document;importorg.dom4j.DocumentHelper;importorg.dom4j.Element;public classCreateXML {public static void main(String[] args) throwsIOException {

Document document=DocumentHelper.createDocument();

Element root= document.addElement("root");

Element author1= root.addElement("author")

.addAttribute("name","Gary")

.addAttribute("localtion", "China")

.addText("Hello Gary");//author1.addElement("添加子标签name")

Element author2= root.addElement("author")

.addAttribute("name","Bob")

.addAttribute("localtion", "US")

.addText("Hello Bob");//保存文件,运行后刷新一下工程

FileWriter out = new FileWriter("Gary.xml");

document.write(out);

out.close();

}

}

CreateXML.class

674dd4b92ec7286c819b5c415e876d9b.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值