使用dom4j解析xml出错
这是我的xml文件
<?xml version="1.0" encoding = "gbk"?>
<rdf:RDF xml:base="nari" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cim="http://iec.ch/TC57/2003/CIM-schema-cim10#" xmlns:cimNARI="http://www.naritech.cn/CIM/ext-schema#">
<cim:SubControlArea rdf:ID="209000001">
<cim:Naming.name>乐平市供</cim:Naming.name>
<cimNARI:SubControlArea.areaNo>1</cimNARI:SubControlArea.areaNo>
<cimNARI:SubControlArea.recordApp>2031647</cimNARI:SubControlArea.recordApp>
<cimNARI:SubControlArea.MemberOf_ControlArea rdf:resource="#209000007"/>
</cim:SubControlArea>
<cim:SubControlArea rdf:ID="209000002">
<cim:Naming.name>德兴市</cim:Naming.name>
<cimNARI:SubControlArea.areaNo>2</cimNARI:SubControlArea.areaNo>
<cimNARI:SubControlArea.recordApp>2031647</cimNARI:SubControlArea.recordApp>
<cimNARI:SubControlArea.MemberOf_ControlArea rdf:resource="#209000007"/>
</cim:SubControlArea>
<cim:SubControlArea rdf:ID="209000003">
<cim:Naming.name>鄱阳县</cim:Naming.name>
<cimNARI:SubControlArea.areaNo>3</cimNARI:SubControlArea.areaNo>
<cimNARI:SubControlArea.recordApp>2031647</cimNARI:SubControlArea.recordApp>
<cimNARI:SubControlArea.MemberOf_ControlArea rdf:resource="#209000007"/>
</cim:SubControlArea>
</rdf:RDF>
import java.io.File;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
public class ReadMyXML2{
public static void main(String[] args) throws Exception {
SAXReader saxReader = new SAXReader();
Document doc = saxReader.read(new File("work.xml"));
Element root = doc.getRootElement();
List<Element> lstSCA = doc.selectNodes("//*[local-name()='SubControlArea']");
for (Element elem : lstSCA) {
Attribute attrID = elem.attribute("ID");
System.out.println(attrID.getName() + "=" + attrID.getText());
Node elemName = elem.selectSingleNode("*[local-name()='Naming.name']");
System.out.println(elemName.getName() + "\t\t" + elemName.getText());
}
}
}
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class XmlReader {
private List<SubControlArea> list = new ArrayList<SubControlArea>();
public void read(){
SAXParserFactory sf = SAXParserFactory.newInstance();
SAXParser sax = null;
try {
sax = sf.newSAXParser();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
//sax.parse("src/work.xml", new DefaultHandler());
sax.parse("src/work.xml", new SAXHander());
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i=0;i<list.size();i++){
System.out.println(list.get(i).getRecordApp());
}
}
public class SAXHander extends DefaultHandler{
String currentName = "";
SubControlArea area = null;
@Override
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
if(name.equals("cim:SubControlArea")){
area = new SubControlArea();
}
if(name.equals("cimNARI:SubControlArea.areaNo")){
currentName = name;
}else{
currentName = "";
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
String textContent = new String(ch, start, length);
if((textContent.trim().equals("")==false) && textContent!=null && currentName.equals("cimNARI:SubControlArea.areaNo")){
area.setRecordApp(textContent);
}else{
}
}
@Override
public void endElement(String uri, String localName, String name)
throws SAXException {
// TODO Auto-generated method stub
if(name.equals("cim:SubControlArea")){
list.add(area);
}
}
}
private class SubControlArea{
private String name;
private String areaNo;
private String recordApp;
private String MemberOf_ControlArea;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAreaNo() {
return areaNo;
}
public void setAreaNo(String areaNo) {
this.areaNo = areaNo;
}
public String getRecordApp() {
return recordApp;
}
public void setRecordApp(String recordApp) {
this.recordApp = recordApp;
}
public String getMemberOf_ControlArea() {
return MemberOf_ControlArea;
}
public void setMemberOf_ControlArea(String memberOf_ControlArea) {
MemberOf_ControlArea = memberOf_ControlArea;
}
}
public static void main(String[] args) {
XmlReader reader = new XmlReader();
reader.read();
}
}