【问题标题】:Groovy XmlParser / XmlSlurper: node.localText() position?Groovy XmlParser / XmlSlurper:node.localText()位置?
【发布时间】:2015-12-09 01:46:54
【问题描述】:

我对此问题有一个后续问题:Groovy XmlSlurper get value of the node without children

它解释说,为了在不递归地获取潜在内部子节点的嵌套文本的情况下获取 (HTML) 节点的本地内部文本,必须使用 #localText() 而不是 #text()

例如,原始问题的一个稍微增强的示例:

<html>
    <body>
        <div>
            Text I would like to get1.
            <a href="http://intro.com">extra stuff</a>
            Text I would like to get2.
            <a href="http://example.com">link to example</a>
            Text I would like to get3.
        </div>
        <span>
            <a href="http://intro.com">extra stuff</a>
            Text I would like to get2.
            <a href="http://example.com">link to example</a>
            Text I would like to get3.
        </span>
    </body>
</html>

应用解决方案:

def tagsoupParser = new org.ccil.cowan.tagsoup.Parser()
def slurper = new XmlSlurper(tagsoupParser)
def htmlParsed = slurper.parseText(stringToParse)

println htmlParsed.body.div[0].localText()[0]

会返回:

[Text I would like to get1., Text I would like to get2., Text I would like to get3.]

然而,在本例中解析&lt;span&gt;部分时

println htmlParsed.body.span[0].localText()

输出是

[Text I would like to get2., Text I would like to get3.]

我现在面临的问题是,显然无法确定文本的位置(“在哪些子节点之间”)。我本来预计第二次调用会产生

[, Text I would like to get2., Text I would like to get3.]

这已经很清楚了:位置 0(在孩子 0 之前)是空的,位置 1(在孩子 0 和 1 之间)是“我想要获取的文本”,位置 2(在孩子 1 和 2 之间)是“我想得到的文字3”。但鉴于 API 的工作原理,显然无法确定在索引 0 处返回的文本是否实际位于索引 0 或任何其他索引处,所有其他索引也是如此。

我已经对XmlSlurperXmlParser 进行了尝试,得到了相同的结果。

如果我在这里没记错的话,使用来自解析器的信息完全重新创建原始 HTML 文档也是不可能的,因为这个“文本索引”信息丢失了。

我的问题是:有没有办法找出那些文本位置?要求我更改解析器的答案也是可以接受的。


更新/解决方案:

为了进一步参考,这里是 Will P 的答案,应用于原始代码:

def tagsoupParser = new org.ccil.cowan.tagsoup.Parser()
def slurper = new XmlParser(tagsoupParser)
def htmlParsed = slurper.parseText(stringToParse)

println htmlParsed.body.div[0].children().collect {it in String ? it : null}

这会产生:

[Text I would like to get1., null, Text I would like to get2., null, Text I would like to get3.]

必须使用 XmlParser 而不是 XmlSlurpernode.children()

【问题讨论】:

    标签: groovy html-parsing xmlslurper


    【解决方案1】:

    我不知道 jsoup,我希望它不会干扰解决方案,但是使用纯 XmlParser 您可以获得包含原始字符串的 children() 数组:

    html = '''<html>
        <body>
            <div>
                Text I would like to get1.
                <a href="http://intro.com">extra stuff</a>
                Text I would like to get2.
                <a href="http://example.com">link to example</a>
                Text I would like to get3.
            </div>
            <span>
                <a href="http://intro.com">extra stuff</a>
                Text I would like to get2.
                <a href="http://example.com">link to example</a>
                Text I would like to get3.
            </span>
        </body>
    </html>'''
    
    def root = new XmlParser().parseText html
    
    root.body.div[0].children().with {
        assert get(0).trim() == 'Text I would like to get1.'
        assert get(0).getClass() == String
    
        assert get(1).name() == 'a'
        assert get(1).getClass() == Node
    
        assert get(2) == '''
                Text I would like to get2.
                '''
    }
    

    【讨论】:

    • 就是这样!显然,它仅适用于 XmlParser,不适用于 XmlSlurper。谢谢你。我会用解决方案更新我的问题。我只希望 Groovy 能更清楚地记录这两个类之间的差异......
    猜你喜欢
    • 2011-11-25
    • 1970-01-01
    • 1970-01-01
    • 2012-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-05
    • 1970-01-01
    相关资源
    最近更新 更多