Question

Lets say I have an xml file called pixelsTest.xml that looks like this...

<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>

What I want to do is display everything that is inside the html element in the form the html element is formed. Meaning I want to print the exact output of the html element. Here is how my code looks like...

    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
    }

But I can't get it to access the html element correctly and I check by printing my htmlList. Here is my output...

 -------------------------------------------------------------------------------
 <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>  

But I want it saved in my htmlList correctly like the bottom print out...

 -------------------------------------------------------------------------------
 <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>  

It looks like XmlSlurper skips some stuff like the CDATA and the comments too. Can anyone please help me? Thank you!

Was it helpful?

Solution

I ended up getting the exact html tag. I did not use XmlSlurper though. I went on using a different method which was parsing the file as an entire string. Then I used the substringBetween method in StringUtils and got the string containing inside the html tags. Here is a snippet of my code that does that.

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>")
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top