java dom4j 解析 xml 代码
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class ReadyXML {
public List<Column> readlyXML(String xmlURL) throws Exception{
List<Column> list =new LinkedList<Column>();
SAXReader reader =new SAXReader();
Document document =reader.read(xmlURL);
Element root = document.getRootElement();
return disk_readlyXML(root,list);
}
public List<Column> disk_readlyXML(Element root,List<Column> list) throws Exception{
for (Iterator iter = root.elementIterator(); iter.hasNext();) {
Element element = (Element) iter.next();
Column column =new Column();
Attribute attributeID = element.attribute("id");
// 判断是有写个 id 属性
if (attributeID!=null) {
if (attributeID.getValue()!=null&&!"".equals(attributeID.getValue())) {
column.setId(attributeID.getValue());
}else {
column.setId(null);
}
}
Attribute attributeTITLE = element.attribute("title");
// 判断是有写个 title 属性
if (attributeTITLE!=null) {
if (attributeTITLE.getValue()!=null&&!"".equals(attributeTITLE.getValue())) {
column.setTitle(attributeTITLE.getValue());
}else {
column.setTitle(null);
}
}
Attribute attributeVALUE = element.attribute("value");
// 判断是有写个 value 属性
if (attributeVALUE!=null) {
if (attributeVALUE.getValue()!=null&&!"".equals(attributeVALUE.getValue())) {
column.setValue(attributeVALUE.getValue());
}else {
column.setValue(null);
}
}
Attribute attributeNAME = element.attribute("name");
// 判断是有写个 name 属性
if (attributeNAME!=null) {
if (attributeNAME.getValue()!=null&&!"".equals(attributeNAME.getValue())) {
column.setValue(attributeNAME.getValue());
}else {
column.setValue(null);
}
}
Attribute attributePID = element.attribute("pid");
// 判断是有写个 pid 属性
if (attributePID!=null) {
if (attributePID.getValue()!=null&&!"".equals(attributePID.getValue())) {
column.setPid(attributePID.getValue());
}else {
column.setPid(null);
}
}
list.add(column);
disk_readlyXML(element,list);
}
return list;
}
public static void main(String[] args) {
ReadyXML readyXML =new ReadyXML();
try {
List<Column> list = readyXML.readlyXML("d:/menu.xml");
for (Column column : list) {
System.out.println("id: "+column.getId());
System.out.println("title: "+column.getTitle());
System.out.println("name: "+column.getName());
System.out.println("value: "+column.getValue());
System.out.println("pid: "+column.getPid());
System.out.println("--------------------------");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
class Column{
private String id;
private String name;
private String title;
private String value;
private String pid;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
}
}
xml 的数据 结构
<?xml version="1.0" encoding="UTF-8"?>
<root>
<column pid="0" name="aaaa" title="a" value="" id="1">
<column pid="1" name="0001" title="a" value="" id="11" >
<column pid="11" name="00010001" title="11" value="" id="111" >
</column>
<column pid="11" name="00010002" title="12" value="" id="112" >
</column>
</column>
<column pid="1" name="0001" title="a" value="" id="12" >
</column>
</column>
<column pid="0" name="bbbb" title="b" value="" id="2">
<column pid="2" name="0001" title="a" value="" id="21" >
<column pid="21" name="00020001" title="21" value="" id="211" >
</column>
<column pid="21" name="00020002" title="22" value="" id="212" >
</column>
</column>
<column pid="2" name="0002" title="a" value="" id="21" >
</column>
</column>
</root>