代码:
package com.xml;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
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 DOM4J {
private static ArrayList<People> peopleList = new ArrayList<People>();
/**
* @param args
*/
public static void main(String[] args) {
SAXReader reader = new SAXReader();
try {
Document document = reader.read(new File("src/res/peopleList.xml"));
Element peopleStore = document.getRootElement();
Iterator it = peopleStore.elementIterator();
while (it.hasNext()) {
System.out.println("");
Element people = (Element) it.next();
List<Attribute> peopleAttrs = people.attributes();
for (Attribute attr : peopleAttrs) {
System.out.println("属性名:" + attr.getName() + "--属性值:"
+ attr.getValue());
}
Iterator itt = people.elementIterator();
while (itt.hasNext()) {
Element peopleChild = (Element) itt.next();
System.out.println("节点名:" + peopleChild.getName() + "--节点值:" + peopleChild.getStringValue());
}
System.out.println("");
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
xml:
<viewentry position="2" unid="A" noteid="3" siblings="14">
<entrydata columnnumber="1" name="name">
<text>王五</text></entrydata>
<entrydata columnnumber="2" name="FullName">
<text>王五/华夏/ZJCZ</text></entrydata>
<entrydata columnnumber="3" name="dept">
<text>办公室(科研处)</text></entrydata>
<entrydata columnnumber="4" name="role">
<text>办公室秘书岗</text></entrydata>
<entrydata columnnumber="5" name="duty">
<text>其他</text></entrydata>
<entrydata columnnumber="6" name="wkgp">
<text></text></entrydata>
</viewentry>
想要输出成:name=xxx,fullname=...,dept=...,
相关分类