最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
java的xml api接口dom4j一简单例子
时间:2022-06-29 02:52:22 编辑:袖梨 来源:一聚教程网
dom4j感觉有点像js了我们来看一个关于dom4j的一些基本的用法吧。
基本用法
1.获取文档的根节点.
Element rootElm = document.getRootElement();
2.取得某节点的单个子节点.
Element memberElm=root.element("member");// "member"是节点名
3.取得节点的文字
String text=memberElm.getText();
也可以用:
String text=root.elementText("name");这个是取得根节点下的name字节点的文字.
4.取得某节点下名为"member"的所有字节点并进行遍历.
List nodes = rootElm.elements("member");
for (Iterator it = nodes.iterator(); it.hasNext();) {
Element elm = (Element) it.next();
// do something
}
5.对某节点下的所有子节点进行遍历.
for(Iterator it=root.elementIterator();it.hasNext();){
Element element = (Element) it.next();
// do something
}
6.在某节点下添加子节点.
Element ageElm = newMemberElm.addElement("age");
7.设置节点文字.
ageElm.setText("29");
8.删除某节点.
parentElm.remove(childElm);// childElm是待删除的节点,parentElm是其父节点
在IBM developerWorks上面还可以找到一篇文章,对主流的Java XML API进行的性能、功能和易用性的评测,所以可以知道dom4j无论在哪个方面都是非常出色的。如今可以看到越来越多的Java软件都在使用dom4j来读写XML,特别值得一提的是连Sun的JAXM也在用dom4j。这已经是必须使用的jar包, Hibernate也用它来读写配置文件。
例子,读写xml
package com.test.transport.service;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class XMLService {
public static Logger log = Logger.getLogger(XMLService.class);
public Document getDocument(){
Document doc = DocumentHelper.createDocument();
doc.addComment("a simple demo ");
Element root = doc.addElement("Persons");
Element p1 = root.addElement("Person");
p1.addAttribute("name", "xiaohei");
p1.addAttribute("age", "20");
p1.addAttribute("sex", "male");
Element p2 = root.addElement("Person");
p2.addAttribute("name", "xiaobai");
p2.addAttribute("age", "21");
p2.addAttribute("sex", "female");
return doc;
}
public void saveDocument(Document doc,String filepath){
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = null;
try {
writer = new XMLWriter(new FileWriter(new File(filepath)), format);
writer.write(doc);
writer.flush();
writer.close();
} catch (IOException e) {
log.error("文件错误");
e.printStackTrace();
}
}
public static void main(String args[]){
XMLService service = new XMLService();
Document doc = service.getDocument();
service.saveDocument(doc, "demo.xml");
}
}
2、增加修改方法
package com.test.transport.service;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class XMLService {
public static Logger log = Logger.getLogger(XMLService.class);
public Document getDocument(){
Document doc = DocumentHelper.createDocument();
doc.addComment("a simple demo ");
Element root = doc.addElement("Persons");
Element p1 = root.addElement("Person");
p1.addAttribute("name", "xiaohei");
p1.addAttribute("age", "20");
p1.addAttribute("sex", "male");
Element p2 = root.addElement("Person");
p2.addAttribute("name", "xiaobai");
p2.addAttribute("age", "21");
p2.addAttribute("sex", "female");
return doc;
}
public void saveDocument(Document doc,String filepath){
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = null;
try {
writer = new XMLWriter(new FileWriter(new File(filepath)), format);
writer.write(doc);
writer.flush();
writer.close();
} catch (IOException e) {
log.error("文件错误");
e.printStackTrace();
}
}
public Document editDocument(File inputFile){
SAXReader saxReader = new SAXReader();
Document doc = null;
try {
doc = saxReader.read(inputFile);
} catch (DocumentException e) {
log.error("读取文件错误");
e.printStackTrace();
}
List
Iterator
while(iterator.hasNext()){
Attribute attribute = iterator.next();
if(attribute.getValue().endsWith("xiaohei")){
attribute.setValue("zhaodaoxiaohei");
}
}
List
Iterator
while (iterator2.hasNext()) {
Element element = iterator2.next();
element.addText("5");
if(element.attributeValue("name").equals("xiaobai")){
element.addElement("grade").addText("3");
}
}
return doc;
}
public static void main(String args[]){
XMLService service = new XMLService();
Document doc = service.getDocument();
service.saveDocument(doc, "demo.xml");
Document document = service.editDocument(new File("demo.xml"));
service.saveDocument(document, "demo.xml");
}
}
相关文章
- 云星穹铁道权益升级怎么玩 云崩铁权益升级活动介绍 12-03
- 以闪亮之名山中绘梦怎么玩 以闪亮之名滢然花梦套装活动介绍 12-03
- 以闪亮之名绿野梦歌礼包怎么样 以闪亮之名绿野梦歌拍照礼包介绍 12-03
- 光遇12.6有什么更新 光遇12月6日更新内容介绍 12-03
- 伍六七暗影交锋测试资格怎么获得 伍六七暗影交锋内测资格获取方法 12-03
- 无限暖暖晶莹诗集怎么样 12-03