0

我有这个 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 章免疫学 ---- Monos List ----

单声道内容:mono[attributes={id=382727}; 值=[div[属性={}; 值=[h1[属性={}; value=[blah blah]]]], div[属性={}; 值=[p[属性={}; value=[blah blah]]]]]] ---- End Monos List ----

血液学 ---- Monos List ----

单声道内容:mono[attributes={id=blah blah}; 值=[div[属性={}; 值=[h1[属性={}; value=[blah blah]]]], div[属性={}; 值=[div[属性={class=class1}; value=[blah blah]]]]]] ---- End Monos List ----

4

2 回答 2

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()
    }
}
于 2012-04-30T17:57:39.170 回答
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>
于 2012-04-30T17:44:42.530 回答