如何在GRAILS中使用XmlSlurper在html元素中显示所有内容

时间:2013-07-16 21:44:45

标签: groovy xmlslurper

假设我有一个名为pixelsTest.xml的xml文件,看起来像这样......

<twa>
  <trackingPixels>
    <pixelNew pagekey="somepagekey">
        <html>
            <!--
            Google Code for Lead Tracking
            -->
            <script type="text/javascript">
                /*
                <![CDATA[
                */ var google_conversion_id = 10; var google_conversion_language = "en_US"; /*
                ]]>
                */
            </script>
            <script type="text/javascript" src="//www.SomeWebsite.com"></script>
            <noscript>
                <div style="display:inline;">
                    <img height="1" width="1" style="border-style:none;" alt="" src="//www.SomeOtherWebsite.com"/>
                </div>
            </noscript>
        </html>
        <publisher>Google adwords</publisher>
        <dateAddedToRegistry>2013-05-08</dateAddedToRegistry>
    </pixelNew>

    <pixelNew pagekey="someotherpagekey">
        <html>
            <script type="text/javascript">
                var axel = Math.random() + "";
                var a = axel * 10000000000000;
                document.write('<iframe src="www.somewebsite.com/ad" width="1" height="1" frameborder="0" style="display:none"></iframe>');
            </script>
            <noscript>
                <iframe src="https:www.somewebsite.com/ads" width="1" height="1" frameborder="0" style="display:none"></iframe>
            </noscript>
        </html>
        <publisher>Agency Doubleclick Tag</publisher>
        <dateAddedToRegistry>2013-04-17</dateAddedToRegistry>
    </pixelNew>

  </trackingPixels>
</twaDoc>

我想要做的是以html元素形成的形式显示html元素内的所有内容。意思是我想打印html元素的确切输出。这是我的代码的样子......

    def f = new File('c:\\pixelsTest.xml')
    def x = new XmlSlurper().parse(f)
    def htmlList = []

    x.trackingPixels.children().each { px ->
        def html = new StreamingMarkupBuilder().bind { out << px.html } as String
        htmlList << html
    }

    htmlList.each { h ->
        println '-' * 79
        println h
    }

但我无法正确访问html元素并通过打印我的htmlList进行检查。这是我的输出......

 -------------------------------------------------------------------------------
 <html><script type='text/javascript'>
 /*

 */ var google_conversion_id = 10; var google_conversion_language = "en_US"; /*

 */
 </script><script src='//www.SomeWebsite.com' type='text/javascript'></script><noscript><div style='display:inline;'><img height='1' style='border-style:none;' alt='' width='1' src='//www.SomeOtherWebsite.com'></img></div></noscript></html>
 -------------------------------------------------------------------------------
 <html><script type='text/javascript'>
                var axel = Math.random() + "";
                var a = axel * 10000000000000;
                document.write('<iframe frameborder='0' height='1' style='display:none' width='1' src='www.somewebsite.com/ad'></iframe>');
      </script><noscript><iframe frameborder='0' height='1' style='display:none' width='1' src='https:www.somewebsite.com/ads'></iframe></noscript></html>  

但是我想把它保存在我的htmlList中,就像底部打印出来一样......

 -------------------------------------------------------------------------------
 <html>
     <!--
     Google Code for Lead Tracking
     -->
     <script type="text/javascript">
         /*
         <![CDATA[
         */ var google_conversion_id = 10; var google_conversion_language = "en_US"; /*
         ]]>
         */
     </script>
     <script type="text/javascript" src="//www.SomeWebsite.com"></script>
     <noscript>
         <div style="display:inline;">
             <img height="1" width="1" style="border-style:none;" alt="" src="//www.SomeOtherWebsite.com"/>
         </div>
     </noscript>
 </html>
 -------------------------------------------------------------------------------
 <html>
     <script type="text/javascript">
         var axel = Math.random() + "";
         var a = axel * 10000000000000;
         document.write('<iframe src="www.somewebsite.com/ad" width="1" height="1" frameborder="0" style="display:none"></iframe>');
     </script>
     <noscript>
         <iframe src="https:www.somewebsite.com/ads" width="1" height="1" frameborder="0" style="display:none"></iframe>
     </noscript>
 </html>  

看起来XmlSlurper会跳过一些像CDATA和评论这样的东西。谁能帮帮我吗?谢谢!

1 个答案:

答案 0 :(得分:0)

我最终得到了确切的html标签。我没有使用XmlSlurper。我继续使用另一种方法将文件解析为整个字符串。然后我在StringUtils中使用了substringBetween方法,并获得了包含在html标记内的字符串。以下是我的代码片段。

import org.apache.commons.lang.StringUtils

String file = new File('c:\\pixelsTest.xml').text
def newPixelList = []
def htmlList = []
newPixelList = StringUtils.substringsBetween(file, "<pixelNew", "</pixelNew>")
for(int i =0; i < newPixelList.size(); i++){
    //Here I can access the html tag and other tags as well like publisher...

    htmlList[i] = StringUtils.substringBetween(newPixelList[i], "<html>", "</html>")
}