如何使用groovy从XML文件中提取HTML代码

时间:2012-04-30 16:49:25

标签: html xml grails groovy

我有这个XML文件,我需要从“mono”元素中提取HTML代码,但我需要html标记。我需要使用groovy编程语言。

“mono”元素中的所有div都是HTML标签,包括div

提前谢谢。

<dataset>
    <chapters>
        <chapter id="700" name="Immunology">
            <title>Immunology</title>   
            <monos>
                <mono id="382727">

                    <div>
                        <h1>blah blah</h1>
                    </div>
                    <div>
                        <p>blah blah</p>
                    </div>

            </mono>
            </monos>
        </chapter>  
        <chapter id="701" name="hematology">
            <title>Inmuno Hematology</title>    
            <monos>
                <mono id="blah blah">
                    <div>
                        <h1>blah blah</h1>
                    </div>
                    <div>
                        <div class="class1">blah blah</div>
                    </div>
                </mono>
            </monos>
        </chapter>
    </chapters>
</dataset>

我试过了:

import javax.xml.parsers.*;

xml = new XmlParser().parse("languages.xml")

println("There are " +xml.chapters.chapter.size() +" Chapters")

for (int i = 0; i < xml.chapters.chapter.size(); i++) {

            def chapter = xml.chapters.chapter[i]
            def chapterName = chapter.'@name'
            println chapterName

            println("----  Monos List ----\n\n")


            for (int j = 0; j < chapter.monos.mono.size(); j++) {

                        def mono = chapter.monos.mono[j]
                        println("Mono Content: " + mono.toString());
            }

           println("---- End Monos List ----\n\n")

}

但我得到以下输出:

共有2章 免疫学 ----单声道列表----

Mono Content:mono [attributes = {id = 382727};值= [DIV [属性= {};值= [H1 [属性= {}; value = [blah blah]]]],div [attributes = {};值= [P [属性= {};价值= [blah blah]]]]]] ----结束Monos List ----

血液学 ----单声道列表----

单声道内容:mono [attributes = {id = blah blah};值= [DIV [属性= {};值= [H1 [属性= {}; value = [blah blah]]]],div [attributes = {};值= [DIV [属性= {类= Class1的};价值= [blah blah]]]]]] ----结束Monos List ----

2 个答案:

答案 0 :(得分:3)

import groovy.xml.*

def src="""
<dataset>
    <chapters>
        <chapter id="700" name="Immunology">
            <title>Immunology</title>   
            <monos>
                <mono id="382727">

                    <div>
                        <h1>blah blah</h1>
                    </div>
                    <div>
                        <p>blah blah</p>
                    </div>

            </mono>
            </monos>
        </chapter>  
        <chapter id="701" name="hematology">
            <title>Inmuno Hematology</title>    
            <monos>
                <mono id="blah blah">
                    <div>
                        <h1>blah blah</h1>
                    </div>
                    <div>
                        <div class="class1">blah blah</div>
                    </div>
                </mono>
            </monos>
        </chapter>
    </chapters>
</dataset>
"""

def parsed=new XmlSlurper().parseText(src)

parsed.'**'.findAll{it.name()=='mono'}.each{mono->
    mono.children().each {htmlElement->
        println new StreamingMarkupBuilder().bind{out << htmlElement}.toString()
    }
}

答案 1 :(得分:2)

您可以使用XmlSlurper或XmlParser来解析xml内容。

http://groovy.codehaus.org/Reading+XML+using+Groovy的+的XmlSlurper http://groovy.codehaus.org/Reading+XML+using+Groovy的+ XmlParser的

import groovy.xml.*
def RECORDS = '''
        <dataset>
        <chapters>
            <chapter id="700" name="Immunology">
                <title>Immunology</title>   
                <monos>
                    <mono id="382727">

                            <div>
                                <h1>blah blah</h1>
                            </div>
                            <div>
                                <p>blah blah</p>
                            </div>

                    </mono>
                </monos>
                </chapter>    
                <chapter id="701" name="hematology">
                    <title>Inmuno Hematology</title>    
                    <monos>
                        <mono id="blah blah">
                            <div>
                                <h1>blah blah</h1>
                            </div>
                            <div>
                                <div class="class1">blah blah</div>
                            </div>
                        </mono>
                    </monos>
                </chapter>
            </chapters>
        </dataset>    
  '''
def records = new XmlSlurper().parseText(RECORDS)
def monos = records.depthFirst().findAll{ it.name().equals('mono') }
assert monos[0].toString() == "blah blahblah blah";
XmlUtil.serialize( monos[0] );

输出:

<?xml version="1.0" encoding="UTF-8"?><mono id="382727">
  <div>
    <h1>blah blah</h1>
  </div>
  <div>
    <p>blah blah</p>
  </div>
</mono>