Wednesday, March 24, 2010

Parse XML string into groovy.util.Node and serialize it back to XML string using XmlNodePrinter.*

import groovy.xml.MarkupBuilder

def SITEMAP= """

  
      http://www.example.com/
      2005-01-01
      monthly
      0.8
  
  
      http://www.example.com/catalog?item=12&desc=vacation_hawaii
      weekly
  
  
      http://www.example.com/catalog?item=73&desc=vacation_new_zealand
      2004-12-23
      weekly
  
  
      http://www.example.com/catalog?item=74&desc=vacation_newfoundland
      2004-12-23T18:00:15+00:00
      0.3
  
  
      http://www.example.com/catalog?item=83&desc=vacation_usa
      2004-11-23
  

"""
def sitemapProrocolSchema="""
  
      http://host3.volti.com/
      
      
      
  
"""

def xml = """

  Java
  Groovy
  JavaScript

"""

Node urlset = new XmlParser().parseText(SITEMAP)

StringWriter xmlStringWriter= new StringWriter()
XmlNodePrinter printer= new XmlNodePrinter(new PrintWriter(xmlStringWriter, true))

XmlNodePrinter.NamespaceContext namespace= new XmlNodePrinter.NamespaceContext(printer)
namespace.registerNamespacePrefix('xmlns','http://www.sitemaps.org/schemas/sitemap/0.9')

printer.print(urlset, namespace)
println xmlStringWriter.toString()

1 comment:

  1. More code samples to try out in Groovy console:

    import groovy.xml.MarkupBuilder

    def SITEMAP= """


    http://www.example.com/
    2005-01-01
    monthly
    0.8


    http://www.example.com/catalog?item=12&desc=vacation_hawaii
    weekly


    http://www.example.com/catalog?item=73&desc=vacation_new_zealand
    2004-12-23
    weekly


    http://www.example.com/catalog?item=74&desc=vacation_newfoundland
    2004-12-23T18:00:15+00:00
    0.3


    http://www.example.com/catalog?item=83&desc=vacation_usa
    2004-11-23


    """
    def sitemapProrocolSchema="""

    http://host3.volti.com/




    """

    def xml = """

    Java
    Groovy
    JavaScript

    """

    Node urlset = new XmlParser().parseText(SITEMAP)

    StringWriter xmlStringWriter= new StringWriter()
    XmlNodePrinter printer= new XmlNodePrinter(new PrintWriter(xmlStringWriter, true))

    XmlNodePrinter.NamespaceContext namespace= new XmlNodePrinter.NamespaceContext(printer)
    namespace.registerNamespacePrefix('xmlns','http://www.sitemaps.org/schemas/sitemap/0.9')

    printer.print(urlset, namespace)
    println xmlStringWriter.toString()

    //def usa= urlList.find{it.@id == 'usa'}
    //Node usa= urlset.url.find{it.@id == 'usa'}
    def urlList= urlset.children()
    //def n= urlList.size()
    for(Node urlNode: urlList){
    def testUrl=('http://www.example.com/catalog?item=74&desc=vacation_newfoundland')
    println testUrl
    candidate=(urlNode.loc.text())
    println candidate
    if(candidate==testUrl){
    println 'GOTCHA'
    urlset.remove(urlNode)
    }
    }
    //urlset.remove(usa)
    println 'AFTER:'
    printer.print(urlset, namespace)
    println xmlStringWriter.toString()


    /*def writer = new StringWriter()
    def newNodeXml = new MarkupBuilder(writer)
    newNodeXml
    newNodeXml.url('xmlns':'http://www.sitemaps.org/schemas/sitemap/0.9', id:'1342'){
    //loc(new QName('http://www.sitemaps.org/schemas/sitemap/0.9', 'http://host3.volti.com'))
    loc('http://host3.volti.com')
    lastmod('')
    changefreq('')
    priority('')
    }

    def newNodeString= writer.toString()
    println newNodeString
    */
    /*
    def newNode=new XmlParser().parseText(newNodeString)
    urlset.append(newNode)
    println urlset.url.size()
    def sitemapUrls= urlset.url*.loc*.text()
    urlset.print(new PrintWriter(System.out))
    println "AGAIN"
    urlset.toString()*/
    //def versions = langs.language*.attribute("version")
    //println langs.getClass()
    // class groovy.util.Node

    //println langs
    /*
    langs[attributes={type=current, count=3, mainstream=true};
    value=[language[attributes={flavor=static, version=1.5};
    value=[Java]],
    language[attributes={flavor=dynamic, version=1.6.0};
    value=[Groovy]],
    language[attributes={flavor=dynamic, version=1.9};
    value=[JavaScript]]
    ]
    ]
    */

    /*langs.url.each{
    println it.loc.text()
    }*/

    // separating the query
    // and the each closure
    // into distinct parts
    /*def list = langs.url
    list.each{
    println it.lastmod.text()
    }*/

    //println list.getClass()
    // groovy.util.NodeList

    ReplyDelete