这个是xml文件
- XML code
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE root (View Source for full doctype...)> - <!-- this is the first xml created by dom4j! --> - <root name="root">- <!-- the root --> this is the root's content! - <child name="first">- <!-- first child --> the first child element! </child>- <child name="second">- <!-- second child --> the second child element! </child> </root>
我用下面的程序 读取:
- Java code
package iterate;import java.io.FileNotFoundException;import java.io.FileReader;import java.util.Iterator;import java.util.List;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.io.SAXReader;public class printXml { @SuppressWarnings("unchecked") public static void main(String[] argv) { final String filename = "D:/firstXml.xml"; try { SAXReader saxReader = new SAXReader(); //这里抛出异常,异常在下面列出 FileReader file = new FileReader(filename); Document doc = saxReader.read(file); System.out.println(filename); List list = doc.selectNodes("/root/child"); Iterator iter = list.iterator(); while (iter.hasNext()){ //System.out.println(filename); Element childElem = (Element)iter.next(); System.out.println(childElem.getText()); } } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
抛出异常:
org.dom4j.DocumentException: D Nested exception: D
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.dom4j.io.SAXReader.read(SAXReader.java:365)
at iterate.printXml.main(printXml.java:24)
Nested exception:
java.net.UnknownHostException: D
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:177)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at sun.net.NetworkClient.doConnect(NetworkClient.java:157)
at sun.net.NetworkClient.openServer(NetworkClient.java:118)
at sun.net.ftp.FtpClient.openServer(FtpClient.java:488)
at sun.net.ftp.FtpClient.openServer(FtpClient.java:475)
at sun.net.www.protocol.ftp.FtpURLConnection.connect(FtpURLConnection.java:270)
at sun.net.www.protocol.ftp.FtpURLConnection.getInputStream(FtpURLConnection.java:352)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:653)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startEntity(XMLEntityManager.java:1316)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startDTDEntity(XMLEntityManager.java:1283)
at com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl.setInputSource(XMLDTDScannerImpl.java:283)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.dispatch(XMLDocumentScannerImpl.java:1168)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.next(XMLDocumentScannerImpl.java:1068)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:988)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:645)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)