我编写了以下代码来读取“struts.xml”(struts2配置文件)。
static main(args) {
String path = /struts.xml/
String timestamp = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date())
String csvName = "struts2-" + timestamp + ".csv"
Node struts = new XmlParser().parse(new File(path))
FileWriter writer = new FileWriter(new File(csvName))
writer.write "#,package,extends,action,method,class\n"
int counter = 1;
struts.package.each { pck ->
pck.action.each { action ->
writer.write("${counter++},${pck.@name},${pck.@extends},${action.@name},${Utils.preventNull(action.@method)},${action.@class}\n")
}
}
writer.close()
println("File Processed")
}
struts.xml在其中有以下doctype定义。
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
当我从struts.xml中删除此行时,代码运行完美。否则会产生以下异常。
Exception in thread "main" java.net.UnknownHostException: struts.apache.org
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:234)
at java.net.Socket.connect(Socket.java:551)
at java.net.Socket.connect(Socket.java:501)
at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:403)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:521)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:246)
at sun.net.www.http.HttpClient.New(HttpClient.java:320)
at sun.net.www.http.HttpClient.New(HttpClient.java:337)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:838)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:790)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:715)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:959)
at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.startEntity(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.startDTDEntity(Unknown Source)
at org.apache.xerces.impl.XMLDTDScannerImpl.setInputSource(Unknown Source)
at org.apache.xerces.impl.XMLDocumentScannerImpl$DTDDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at groovy.util.XmlParser.parse(XmlParser.java:118)
at groovy.util.XmlParser$parse.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at demo.main(Struts2ConfigurationReader.groovy:14)
我错过了什么?
答案 0 :(得分:3)
我假设您在进行解析时没有互联网连接?
您可以通过解析器的功能关闭它:
替换:
Node struts = new XmlParser().parse(new File(path))
与
XmlParser parser = new XmlParser()
parser.setFeature( "http://apache.org/xml/features/nonvalidating/load-external-dtd", false )
Node struts = parser.parse( new File( path ) )
PS:您还可以使用groovy执行以下操作:
static main( args )
- 假设这是一个脚本,则不需要变化:
String path = /struts.xml/
String timestamp = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date())
String csvName = "struts2-" + timestamp + ".csv"
到
String path = 'struts.xml'
String timestamp = new Date().format( 'yyyy-MM-dd_HH-mm-ss' )
String csvName = "struts2-${timestamp}.csv"
使用File.withWriter
可能更容易阅读/编码。这样:
FileWriter writer = new FileWriter(new File(csvName))
writer.write "#,package,extends,action,method,class\n"
....
writer.close()
可以写成:
new File(csvName).withWriter { writer ->
writer.writeLine "#,package,extends,action,method,class"
....
}