【问题标题】:XmlSlurper - list text and regular nodes of xhtml documentXmlSlurper - 列出 xhtml 文档的文本和常规节点
【发布时间】:2009-05-29 18:52:24
【问题描述】:

我正在使用 Groovy 的 XmlSlurper 来解析 xhtml 文档(或 sudo xhthml 之一),我正在尝试获取文档的文本节点但不知道如何,这是代码:

import groovy.util.*

xmlText = '''
<TEXTFORMAT INDENT="10" LEADING="-5">
  <P ALIGN="LEFT">
    <FONT FACE="Garamond Premr Pro" SIZE="20" COLOR="#001200" LETTERSPACING="0" KERNING="0">
      Less is more! this 
      <FONT COLOR="#FFFF00">should be all</FONT>
      the 
      <FONT COLOR="#00FF00"> words OR should some </FONT>
      OTHER WORDS will be there?
    </FONT>
  </P>
</TEXTFORMAT>
'''
records = new XmlSlurper().parseText(xmlText)
records.P.FONT.children().eachWithIndex {it, index -> println "${index} - ${it}"} 

打印以下输出:

0 - should be all 
1 -  words OR should some

但我希望它也打印文本节点内容,因此所需的输出是:

0 - Less is more! this
1 - should be all
2 - the 
3 - words OR should some
4 - OTHER WORDS will be there?

有什么想法吗?

【问题讨论】:

    标签: xml parsing groovy


    【解决方案1】:

    看起来 XmlSlurper 没有单独的方法来检索“混合内容”

    这里有一个开放项添加支持混合内容的方法 -> Groovy JIRA

    【讨论】:

    • 是的,似乎只有 XmlParser 支持该选项
    猜你喜欢
    • 2011-03-14
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 2017-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多