Groovys XmlParser忽略CDATA CR / CL

时间:2012-02-24 13:45:48

标签: java xml groovy log4j

我想解析一个log4j生成的xml日志。在xml中是一个带有throwable(如果有)的节点。此(多行,选项卡式)文本封装在CDATA标记中。

这是整个文件的摘录:

<log4j:event logger="org.codehaus.groovy.grails.web.errors.GrailsExceptionResolver" timestamp="1330083921521" level="ERROR" thread="http-8080-1">
<log4j:message><![CDATA[Exception occurred when processing request: [GET] /test/log/show
Stacktrace follows:]]></log4j:message>
<log4j:throwable><![CDATA[org.xml.sax.SAXParseException: XML document structures must start and end within the same entity.
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1231)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
    at test.LogController$_closure2.doCall(LogController.groovy:21)
    at test.LogController$_closure2.doCall(LogController.groovy)
    at java.lang.Thread.run(Thread.java:662)
]]></log4j:throwable>
</log4j:event>

我用groovys XmlParser解析它:

def parser = new XmlParser(false, false).parse(new File("stack.log"))

return parser.'log4j:event'.collect { l ->
    LogEntry entry = new LogEntry()
    entry.with {
        level = l.'@level'
        message = l.'log4j:message'.text()
        thread = l.'@thread'
        logger = l.'@logger'
        timestamp = new Date(l.'@timestamp' as long)
        throwable = l.'log4j:throwable'?.text() ?: ''
    }
    entry
}

'throwable'字段包含所有文本但没有CR / LF。

有人知道如何应对吗?

先谢谢...

2 个答案:

答案 0 :(得分:1)

讨厌向您抛出代码,但它似乎按预期工作并返回CRLF

def xml = '''<log>
            |  <log4j:event logger="org.codehaus.groovy.grails.web.errors.GrailsExceptionResolver" timestamp="1330083921521" level="ERROR" thread="http-8080-1">
            |    <log4j:message><![CDATA[Exception occurred when processing request: [GET] /test/log/show
            |Stacktrace follows:]]></log4j:message>
            |    <log4j:throwable><![CDATA[org.xml.sax.SAXParseException: XML document structures must start and end within the same entity.
            |    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1231)
            |    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
            |    at test.LogController$_closure2.doCall(LogController.groovy:21)
            |    at test.LogController$_closure2.doCall(LogController.groovy)
            |    at java.lang.Thread.run(Thread.java:662)
            |]]></log4j:throwable>
            |  </log4j:event>
            |</log>'''.stripMargin()


class LogEntry {
  def level
  def message
  def thread
  def logger
  def timestamp
  def throwable

  String toString() {
    """EVENT:
      |  level   : $level
      |  message : $message
      |  thread  : $thread
      |  logger  : $logger
      |  ts      : $timestamp
      |  thrown  : $throwable""".stripMargin()
  }
}

def parser = new XmlParser(false, false).parseText( xml )
def entries = parser.'log4j:event'.collect { event ->
  new LogEntry().with {
    level     = event.@level
    message   = event.'log4j:message'.text()
    thread    = event.@thread
    logger    = event.@logger
    timestamp = new Date( event.@timestamp as long )
    throwable = event.'log4j:throwable'?.text() ?: ''
    it
  }
}

entries.each {
  println it
}

打印:

EVENT:
  level   : ERROR
  message : Exception occurred when processing request: [GET] /test/log/show
Stacktrace follows:
  thread  : http-8080-1
  logger  : org.codehaus.groovy.grails.web.errors.GrailsExceptionResolver
  ts      : Fri Feb 24 11:45:21 GMT 2012
  thrown  : org.xml.sax.SAXParseException: XML document structures must start and end within the same entity.
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1231)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
    at test.LogController$_closure2.doCall(LogController.groovy:21)
    at test.LogController$_closure2.doCall(LogController.groovy)
    at java.lang.Thread.run(Thread.java:662)

其中有CRLF字符,它应该是......

这是使用Groovy 1.8.6 btw ...你使用的是什么版本?你可以升级并重试吗?

答案 1 :(得分:0)

xml标准要求在解析期间对空格进行规范化。

我不确定,但解析器可能有一个设置来覆盖此行为。否则,您可以预处理文件,将c数据部分中的行结尾替换为其xml实体等效项,然后解析它。