Oops I am so sorry.

The problem is I am trying to replace certain Nodes with some XML tags
(nodes children etc.)

I am trying to "re-stream" an XmlSlurper parsed object into a
StreamingMarkupBuilder (see below), but this does not work as the text()
method gets repeated and I get asdf several times (see below below).

Is there a better way to do this?

Thank you!
Misha

import groovy.xml.StreamingMarkupBuilder

def page=new XmlSlurper(new 
org.cyberneko.html.parsers.SAXParser()).parseText("""
<html>
<head></head>
<body>
<one attr1='val1'>asdf</one>
<two />
<replacemewithxml />
</body>
</html>
""".trim())

import groovy.xml.XmlUtil

def closure
closure={ bind,node->
  if (node.name()=="REPLACEMEWITHXML") {
    bind.mkp.yieldUnescaped "<replacementxml>sometext</replacementxml>"
  } else {
    bind."${node.name()}"(node.attributes()) {
      mkp.yield node.text()
      node.children().each { child->
 closure(bind,child)
      }
    }
  }
}
println XmlUtil.serialize(
  new StreamingMarkupBuilder().bind { bind->
    closure(bind,page)
  }
)

My output:
<?xml version="1.0" encoding="UTF-8"?>
<HTML>asdf<HEAD/>
   <BODY>asdf<ONE attr1="val1">asdf</ONE>
      <TWO/>
      <replacementxml>sometext</replacementxml>
   </BODY>
</HTML>

Misha

On Mon, 2010-06-14 at 13:04 -0400, Roger Studner wrote:
> I think you want this on the groovy forum, but to help you out.
> 
> You are getting the correct behavior.. as the only TEXT inside of the body, 
> is aaa.
> 
> (it.text())
> 
> 
> can could do XmlSlurper(html).html.body.h1[0] to literally go straight to 
> that <h1>
> 
> Roger
> 
> On Jun 14, 2010, at 1:02 PM, Misha Koshelev wrote:
> 
> > def html="""
> > <html>
> > <head>
> > </head>
> > <body>
> > <h1>aaa</h1>
> > </body>
> > """
> > 
> > I would like to get text that is _only_ related to h1. However, if I use
> > XmlSlurper on the result and do:
> > 
> > XmlSlurper.depthFirst().find { it.name()=="BODY" }.each { println
> > it.text() }
> > 
> > I still get "AAA" even though it is a _direct_ descendant of H1.
> > 
> > Thank you!
> > Misha
> > 
> > 
> > ---------------------------------------------------------------------
> > To unsubscribe from this list, please visit:
> > 
> >    http://xircles.codehaus.org/manage_email
> > 
> > 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
> 
>     http://xircles.codehaus.org/manage_email
> 
> 



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply via email to