首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

dom4j读写xml事例

2012-12-18 
dom4j读写xml例子package com.hw.DOM4Jimport java.io.Fileimport java.io.FileWriterimport java.io.I

dom4j读写xml例子

package com.hw.DOM4J;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import com.hw.MyException;

/**
?* @author Administrator
?*?
?*?? 获取节点的文字
?*?? String text=memberElm.getText();
?*?? 或者可以用? String text=root.elementText("name");
?*
?*
?*
?*/
public class TestDemo extends MyException{
?
?
? public TestDemo(String message) {
??super(message);
??// TODO Auto-generated constructor stub
?}

?/**
? *
? */
?

?public static void main(String[] args) {
?? String pathname="src/com/hw/xiongmao2.xml";//(另一种格式d:\\xiongmao.xml)
?? File file=new File(pathname);
?? Element root=null;
?? boolean flag=false;
?? XMLWriter writer=null;
?? Document doc=null;
?? OutputFormat format=null;
?? try {
??? //声明写xml的对象
??? //
???format=OutputFormat.createPrettyPrint();
??? format.setEncoding("GBK");//设置xml文件的编码格式?
???//判断文件是否存在
??? if(file.exists()){
??? SAXReader reader=new SAXReader();
???
???
???
??? doc=reader.read(file);//读取xml文件
???
???
???
???root=doc.getRootElement();//得到根结点
???
???
???Element xiongmao=null;
???Element name=null;
???
???
???for(Iterator iterator=root.elementIterator("xiongmao");iterator.hasNext();){
????xiongmao=(Element) iterator.next();
????
????String belong=xiongmao.attributeValue("belong");//获得belong属性
????System.out.println(belong);
????
????String username=xiongmao.elementText("name");//获得name元素值
????
????System.out.println(username);
????//如果有多个name元素,则如下:
????for(Iterator iterator2=xiongmao.elementIterator("name");iterator2.hasNext();){
?????name=(Element) iterator2.next();
?????String id=name.attributeValue("id");
?????
?????System.out.println(id);
?????//通过迭代得到name元素值
?????System.out.println(name.getText());
?????System.out.println(name.getData());
????}
????//得到birthdate元素
????String birthdate=xiongmao.elementText("birthdate");
????System.out.println(birthdate);
????
????//修改
????if(xiongmao.attributeValue("belong").equals("zhangbin")){
?????//修改belong="zhangbin"的信息
?????
?????//只能修改一个name元素的值
??????? xiongmao.selectSingleNode("name").setText("baby");
?????xiongmao.selectSingleNode("height").setText("167");
?????System.out.println("---修改成功---");
?????writer=new XMLWriter(new FileWriter(file),format);
?????writer.write(doc);
?????writer.close();
?????flag=true;
?????break;
????}
???}
???
???
???//添加一个xiongmao
???if(flag){
????
???Element? mao=root.addElement("xiongmao");
???mao.addAttribute("belong", "zhangbin1");
???
???Element name_name1=mao.addElement("name");
???name_name1.addAttribute("id", "03");
???name_name1.setText("sweet cat");
???
???Element name_name2=mao.addElement("name");
???name_name2.addAttribute("id", "04");
???name_name2.setText("darling");
???
???
???Element _height=mao.addElement("height");
???_height.setText("162");
???????
???try {
????writer=new XMLWriter(new FileWriter(file),format);
????writer.write(doc);
????writer.close();?
????System.out.println("---添加成功---");
???} catch (IOException e) {
????// TODO Auto-generated catch block
????e.printStackTrace();
???}
??
???
???}
???
??? }
???
???
???
???
??? else {
????//新建xiongmao2.xml
????
???? //创建 document
???? Document _dDocument=DocumentFactory.getInstance().createDocument();
????
????
????
???? // 创建下列元素
???? Element _root=_dDocument.addElement("xiongmaos");//创建根元素
???? //注意目录元素,不能随便创建? 需要与根元素对应
???? _root.addAttribute("xiongmao", "http://www.baidu.com");
????
???? Element? mao=_root.addElement("xiongmao");
?????mao.addAttribute("belong", "zhangbin1");
?????
?????Element name_name1=mao.addElement("name");
?????name_name1.addAttribute("id", "03");
?????name_name1.setText("sweet cat");
?????
?????Element name_name2=mao.addElement("name");
?????name_name2.addAttribute("id", "04");
?????name_name2.setText("darling");
?????
?????
?????Element _height=mao.addElement("height");
?????_height.setText("162");
????
????
?????writer=new XMLWriter(new FileWriter(file),format);
?????writer.write(_dDocument);
?????writer.close();?
?????System.out.println("---创建成功---");
?????
????throw new MyException("file不存在");
???}
??} catch (DocumentException e) {
???// TODO Auto-generated catch block
???e.printStackTrace();
??} catch (MyException e) {
???// TODO Auto-generated catch block
???e.printStackTrace();
??} catch (IOException e) {
???// TODO Auto-generated catch block
???e.printStackTrace();
??}
?

?}
?
?

}

热点排行