0

我有编写以下代码来读取“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 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)

我错过了什么?

4

1 回答 1

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"
      ....
    }
    
于 2013-02-01T10:36:04.557 回答