将xml解析:
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* 利用dom4j进行XML编程
*
* @author cuiweibing
* @since 2007.8.10
*/
public class Dom4jReadExmple {
/**
* 遍历整个XML文件,获取所有节点的值与其属性的值,并放入HashMap中
*
* @param filename
* String 待遍历的XML文件(相对路径或者绝对路径)
* @param hm
* HashMap 存放遍历结果,格式:<nodename,nodevalue>或者<nodename+attrname,attrvalue>
*/
public void iterateWholeXML(String filename, HashMap<String, String> hm) {
SAXReader saxReader = new SAXReader();
try {
Document document = saxReader.read(new File(filename));
Element root = document.getRootElement();
// 用于记录学生编号的变量
int num = -1;
// 遍历根结点(students)的所有孩子节点(肯定是student节点)
for (Iterator iter = root.elementIterator(); iter.hasNext();) {
Element element = (Element) iter.next();
num++;
// 获取person节点的age属性的值
Attribute ageAttr = element.attribute("age");
if (ageAttr != null) {
String age = ageAttr.getValue();
if (age != null && !age.equals("")) {
hm.put(element.getName() + "-" + ageAttr.getName()+ num, age);
} else {
hm.put(element.getName() + "-" + ageAttr.getName()+ num, "20");
}
} else {
hm.put(element.getName() + "-age" + num, "20");
}
// 遍历student结点的所有孩子节点(即name,college,telphone,notes),并进行处理
for (Iterator iterInner = element.elementIterator(); iterInner
.hasNext();) {
Element elementInner = (Element) iterInner.next();
if (elementInner.getName().equals("college")) {
hm.put(elementInner.getName() + num, elementInner.getText());
// 获取college节点的leader属性的值
Attribute leaderAttr = elementInner.attribute("leader");
if (leaderAttr != null) {
String leader = leaderAttr.getValue();
if (leader != null && !leader.equals("")) {
hm.put(elementInner.getName() + "-"+ leaderAttr.getName() + num, leader);
} else {
hm.put(elementInner.getName() + "-"+ leaderAttr.getName() + num, "leader");
}
} else {
hm.put(elementInner.getName() + "-leader" + num,"leader");
}
} else {
hm.put(elementInner.getName() + num, elementInner.getText());
}
}
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
包装:
package com.xml;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class Dom4JXML {
public void createXML() {
//用工厂类创建一个document实例
Document doc = DocumentHelper.createDocument();
//创建根元素emps
Element rootEle = doc.addElement("emps");
//添加注释
rootEle.addComment("这是一个dom4j生成的xml文件");
//emps根节点下创建一个emp节点
Element empEle = rootEle.addElement("emp");
//emp添加属性id="1"
empEle.addAttribute("id", "1");
//emp节点下创建一个name节点
Element nameEle = empEle.addElement("name");
//name节点下创建一个文本节点zhangsan
nameEle.setText("zhangsan");
//再为name节点创建一个兄弟节点
Element sexEle = empEle.addElement("sex");
sexEle.setText("man");
//将document中的内容写入文件中
try {
Writer out = new FileWriter("F:\\emps.xml");
//格式化输出,类型IE浏览一样
OutputFormat format = OutputFormat.createPrettyPrint();
//OutputFormat format = OutputFormat.createCompactFormat();
format.setEncoding("UTF-8");
//创建写出对象
XMLWriter writer = new XMLWriter(out,format);
writer.write(doc);
writer.close();
System.out.println("生成emps.xml成功。");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("失败了。");
}
}
public static void main(String[] args) {
new Dom4JXML().createXML();
}
}
文档内容:
<?xml version="1.0" encoding="UTF-8"?>
<emps>
<!--这是一个dom4j生成的xml文件-->
<emp id="1">
<name>zhangsan </name>
<sex>man </sex>
</emp>
</emps>
3.
解析code属性:
public static String getCode(String filename){
InputStream readerXml = FilePath.class.getResourceAsStream(filename);
String btsNum = null;
SAXReader saxReader = new SAXReader();
try {
Document document = saxReader.read(readerXml);
Element root = document.getRootElement();
int num = -1;
for (Iterator iter = root.elementIterator(); iter.hasNext();) {
Element element = (Element) iter.next();
num++;
Attribute CodeAttr = element.attribute("code");
if (CodeAttr != null) {
btsNum = CodeAttr.getValue();
}
}
} catch (DocumentException e) {
e.printStackTrace();
}
return btsNum;
}