dom4j操作XML基本用法

//导入必要的包
import org.dom4j.Document;//Document文档类
import org.dom4j.Element//元素节点类
import org.dom4j.io.SAXReader;//sax读取类
import org.dom4j.io.XMLWriter;//xml写入类
import org.dom4j.io.OutputFormat;//输出格式


//读入xml文件
String fileName = "xxx.xml";
InputStream input = new FileInputStream(fileName);
SAXReader reader = new SAXReader();
Document document = reader.read(input);//或reader.read(new File(fileName));


//操作Document文档
1,最常用:返回List数据类型,可以利用iterator进行遍历,然后Element转型:
List list = document.selectNodes("//xxx:string");//XPath
for (Iterator it = list.iterator(); it.hasNext();) {
	Element elm = (Element) it.next();
	//这里是你操作元素的代码...
}

2.返回第一匹配的XPath节点:
Node node = doc.selectSingleNode("//xxx:getForexRmbRate");
或Element element = (Element)doc.selectSingleNode("//xxx:getForexRmbRate");

3.返回根节点:
Element root = document.getRootElement();

4.返回节点的所有的子节点:
List list = element.elements();
  获得指定标签名称为book的子节点:
Element ele = element.element("book");

5.获取节点元素的text文本:
String s = element.getText();
 设置节点元素的文本值:
void setText(String value);

6.返回节点的属性值:
String value = element.attributeValue(String name);
 设置属性值:
void setAttributeValue(String name,String value);

7.获得标签本身的名称:
String s = element.getName();
 设置名称:
void setName(String name);

8.获得该节点的命名空间:(一般都是将命名空间放在map中,用于解析xml)
String uri = element.getNamespaceURI();

9.复制节点:
Element createCopy();
  获得父节点:
Element getParent();

10.枚举属性:
for(Iterator it=element.attributeIterator(); it.hasNext();){
    Attribute att = (Attribute)it.next();
    ......
}

11.在一个节点后添加节点:
第一种添加方法:element.add(Element ele);
第二种添加方法:element.appendContent(element.getParent());
第三种添加方法:Element e = element.addElement("book");

12.保存xml文件:
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding(encodeOut);//含有中文时,要为GBK或GB2312
format.setIndent("     ");
XMLWriter writer = new XMLWriter(new FileWriter(new File(fileName)),format); 
writer.write(document); 
writer.close(); 

13./**
	 * 创建xml文件
	 * @param fileName  需建立的文件名
	 * @return 返回操作结果, false表失败, true表成功
	 */
	public static boolean createXMLFile(String fileName){
		boolean result = false;// 返回操作结果
		
		Document document = DocumentHelper.createDocument();//建立document对象
		
		Element booksElement = document.addElement("books"); //建立XML文档的根books 
		booksElement.addComment("This is a test for dom4j"); //加入一行注释
		Element bookElement = booksElement.addElement("book"); //加入第一个book节点
		bookElement.addAttribute("show","yes"); //加入show属性内容
		Element titleElement = bookElement.addElement("title"); // 加入title节点
		titleElement.setText("Dom4j Tutorials"); //为title设置内容
		
		bookElement = booksElement.addElement("book"); //类似的完成后两个book 
		bookElement.addAttribute("show","yes"); 
		titleElement = bookElement.addElement("title"); 
		titleElement.setText("Lucene Studing"); 
		bookElement = booksElement.addElement("book"); 
		bookElement.addAttribute("show","no"); 
		titleElement = bookElement.addElement("title"); 
		titleElement.setText("Lucene in Action"); 
		
		Element ownerElement = booksElement.addElement("owner"); //加入owner节点
		ownerElement.setText("中文啊"); 
		
		try{
			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding(encodeOut); //encodeOut为GBK或GB2312,中文时必须要设置,否则用ie打开时报错!!!
			XMLWriter writer = new XMLWriter(new FileWriter(new File(fileName)),format); 
			writer.write(document); 
			writer.close(); 
			result = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}


dom4j解析xml字符串:
http://www.cnblogs.com/macula/archive/2011/07/27/2118003.html
http://xpws2006.blog.163.com/blog/static/9543857720095183129813/
http://zhidao.baidu.com/question/136456396.html
http://zhidao.baidu.com/question/116552893.html?fr=qrl&index=0
http://starting.iteye.com/blog/758106
http://blog.csdn.net/mingjian_zheng/article/details/4170519
http://my.oschina.net/chen106106/blog/50272
http://hanzel.i.sohu.com/blog/view/200670677.htm

你可能感兴趣的:(dom4j)