用dom4j saxreader 修改xml 节点内容
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE books SYSTEM "file://c:/Dtds/catalog.dtd">
<books><!--这是个一个根节点-->
<book>
<name>c语言</name>
<price>12</price>
</book>
<book>
<name>c语言1</name>
<price>13</price>
</book>
</books>
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(Class3.class.getClassLoader().getResourceAsStream("note.xml"));
ArrayList list = (ArrayList) document.selectNodes("//name");
Iterator iterator = list.iterator();
while(iterator.hasNext()){
Element element= (Element)iterator.next();
element.setText("xx");
}
try {
XMLWriter xmlWriter=new XMLWriter(new FileWriter(new File("d://note.xml")));
xmlWriter.write(document);
xmlWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
报以下错误
Exception in thread "main" org.dom4j.DocumentException: Connection timed out: connect Nested exception: Connection timed out: connect
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.dom4j.io.SAXReader.read(SAXReader.java:343)
at com.xtgj.dom4jxml.Class3.main(Class3.java:21)
Nested exception:
java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.NetworkClient.openServer(Unknown Source)
at sun.net.ftp.FtpClient.openServer(Unknown Source)
at sun.net.ftp.FtpClient.openServer(Unknown Source)
at sun.net.www.protocol.ftp.FtpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.ftp.FtpURLConnection.getInputStream(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startEntity(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startDTDEntity(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl.setInputSource(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.dispatch(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.dom4j.io.SAXReader.read(SAXReader.java:343)
at com.xtgj.dom4jxml.Class3.main(Class3.java:21)
原因在于:xml 文件中的 <!DOCTYPE books SYSTEM "file://c:/Dtds/catalog.dtd"> 干扰。因为这个路径不存在。
可以把这个dtd 的引入删掉
问题最后解决了。