Quantcast

XML without pretty printing

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

XML without pretty printing

Andrew Goodspeed
Is there a Groovy way to get XML as a String that is not pretty printed? I could swear I have done so before. I ended up doing this, falling back on Java. It works, but...

    def dom = DOMBuilder.parse(...)
    ...
    TransformerFactory transformerFactory = TransformerFactory.newInstance()
    Transformer transformer = transformerFactory.newTransformer()
    StringWriter sw = new StringWriter()
    StreamResult sr = new StreamResult(sw)
    DOMSource source = new DOMSource(dom)
    transformer.transform(source, sr)
    String xmlString = sw.toString()

-Andy
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: XML without pretty printing

tim yates-2
Not sure what you mean...  If you want to unprettify xml, you can do:

def prettyxml = '''<a>
                  |  <b>c</b>
                  |</a>'''.stripMargin()

String uglyxml = new groovy.xml.StreamingMarkupBuilder().bind {
  mkp.yield new XmlSlurper().parseText( prettyxml )
}

Hope this helps!

Tim

On 30 March 2012 13:44, <[hidden email]> wrote:
Is there a Groovy way to get XML as a String that is not pretty printed? I could swear I have done so before. I ended up doing this, falling back on Java. It works, but...

    def dom = DOMBuilder.parse(...)
    ...
    TransformerFactory transformerFactory = TransformerFactory.newInstance()
    Transformer transformer = transformerFactory.newTransformer()
    StringWriter sw = new StringWriter()
    StreamResult sr = new StreamResult(sw)
    DOMSource source = new DOMSource(dom)
    transformer.transform(source, sr)
    String xmlString = sw.toString()

-Andy

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: XML without pretty printing

Andrew Goodspeed
The ellipsis I included in place of lines of code is where I modify the DOM. Then I need to get it back out as standard (unpretty) XML.

All of the doc wants it pretty and that seems to be straightforward - the default even. It seems like I should be able to get it out of the DOM without shenanigans.

I think I am missing something obvious.

> Not sure what you mean...  If you want to unprettify xml, you can do:

>
> def prettyxml = '''<a>

>                   |  <b>c</b>
>                   |</a>'''.stripMargin()
>
> String uglyxml = new groovy.xml.StreamingMarkupBuilder().bind {

>   mkp.yield new XmlSlurper().parseText( prettyxml )
> }
>
> Hope this helps!

>
> Tim

>
> On 30 March 2012 13:44, <[hidden email]> wrote:

> Is there a Groovy way to get XML as a String that is not pretty
> printed? I could swear I have done so before. I ended up doing this,
> falling back on Java. It works, but...
>
>     def dom = DOMBuilder.parse(...)
>     ...
>     TransformerFactory transformerFactory = TransformerFactory.newInstance()
>     Transformer transformer = transformerFactory.newTransformer()
>     StringWriter sw = new StringWriter()
>     StreamResult sr = new StreamResult(sw)
>     DOMSource source = new DOMSource(dom)
>     transformer.transform(source, sr)
>     String xmlString = sw.toString()
>
> -Andy
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: XML without pretty printing

tim yates-2
Not knowing what you're doing to modify the dom, here's an example that does some modification:

def prettyxml = '''<a>
                  |  <b>c</b>
                  |</a>'''.stripMargin()

dom = new XmlSlurper().parseText( prettyxml )
dom.appendNode {
  groovy( 'cool' )
}
String uglyxml = new groovy.xml.StreamingMarkupBuilder().bind {
  mkp.yield dom
}

assert uglyxml == '<a><b>c</b><groovy>cool</groovy></a>'

Tim

On 30 March 2012 14:10, <[hidden email]> wrote:
The ellipsis I included in place of lines of code is where I modify the DOM. Then I need to get it back out as standard (unpretty) XML.

All of the doc wants it pretty and that seems to be straightforward - the default even. It seems like I should be able to get it out of the DOM without shenanigans.

I think I am missing something obvious.

> Not sure what you mean...  If you want to unprettify xml, you can do:

>
> def prettyxml = '''<a>

>                   |  <b>c</b>
>                   |</a>'''.stripMargin()
>
> String uglyxml = new groovy.xml.StreamingMarkupBuilder().bind {

>   mkp.yield new XmlSlurper().parseText( prettyxml )
> }
>
> Hope this helps!

>
> Tim

>
> On 30 March 2012 13:44, <[hidden email]> wrote:

> Is there a Groovy way to get XML as a String that is not pretty
> printed? I could swear I have done so before. I ended up doing this,
> falling back on Java. It works, but...
>
>     def dom = DOMBuilder.parse(...)
>     ...
>     TransformerFactory transformerFactory = TransformerFactory.newInstance()
>     Transformer transformer = transformerFactory.newTransformer()
>     StringWriter sw = new StringWriter()
>     StreamResult sr = new StreamResult(sw)
>     DOMSource source = new DOMSource(dom)
>     transformer.transform(source, sr)
>     String xmlString = sw.toString()
>
> -Andy

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: XML without pretty printing

Andrew Goodspeed
Okay, here is the code with your suggestion in place of what I had previously.

import groovy.sql.Sql
import groovy.xml.DOMBuilder
import groovy.xml.dom.DOMCategory

def sql = Sql.newInstance(...)

sql.eachRow("""
select xml_data from MyTable where xml_data like '%<PaymentArrangement>%'
""".toString()) { row ->
    def dom = DOMBuilder.parse(row.xml_data.getCharacterStream())
    def root = dom.documentElement
    use(DOMCategory) {
        root.PaymentArrangement.each { pa ->
            println "${row.agent_id}-${row.account_id} before:"
            println pa
            pa.each { paElement ->
                if (['FirstPaymentSameAccount', 'RequestedFirstDraftDate', 'AmountPaidWithApp'].contains(paElement.nodeName)) {
                    pa.removeChild(paElement)
                }
            }
            pa.OneTimeDraft.each { otd ->
                otd.each { otdElement ->
                    if (['Amount'].contains(otdElement.nodeName)) {
                        def otdAmount = otd.removeChild(otdElement)
                        pa.appendNode('OneTimeDraftAmount', otdAmount.getFirstChild().data)
                    }
                }
            }
            pa.RecurringDraft.each { rd ->
                rd.each { rdElement ->
                    if (['Amount'].contains(rdElement.nodeName)) {
                        rd.removeChild(rdElement)
                    }
                }
            }
            println "${row.agent_id}-${row.account_id} ${row.id} after:"
            println pa
        }

        String xmlString = new groovy.xml.StreamingMarkupBuilder().bind {
            mkp.yield dom
        }
        assert xmlString.indexOf('FirstPaymentSameAccount') == -1

        println xmlString
    }
}

When I run it I get a pretty version of my XML with &lt; and &gt; in place of the "<" and ">". Of course my desire is to write the XML back to the database which I can do using the not-very-Groovy code I cited.

-Andy

> Not knowing what you're doing to modify the dom, here's an example
> that does some modification:

>
> def prettyxml = '''<a>

>                   |  <b>c</b>
>                   |</a>'''.stripMargin()
>
> dom = new XmlSlurper().parseText( prettyxml )

> dom.appendNode {
>   groovy( 'cool' )
> }
> String uglyxml = new groovy.xml.StreamingMarkupBuilder().bind {
>   mkp.yield dom
> }
>
> assert uglyxml == '<a><b>c</b><groovy>cool</groovy></a>'
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: XML without pretty printing

paulk_asert

If you were using XmlParser you could use XmlNodePrinter
and if you were using XmlSlurper you could do as Tim suggests directly
but because you are using the DOM classes you would need something like:

     mkp.yield new XmlSlurper().parseText(XmlUtil.serialize(root))

Which will do the round-tripping similar to what your Java version
does but with only one line of code.

Cheers, Paul.

On 31/03/2012 9:36 AM, [hidden email] wrote:

> Okay, here is the code with your suggestion in place of what I had previously.
>
> import groovy.sql.Sql
> import groovy.xml.DOMBuilder
> import groovy.xml.dom.DOMCategory
>
> def sql = Sql.newInstance(...)
>
> sql.eachRow("""
> select xml_data from MyTable where xml_data like '%<PaymentArrangement>%'
> """.toString()) { row ->
> def dom = DOMBuilder.parse(row.xml_data.getCharacterStream())
> def root = dom.documentElement
> use(DOMCategory) {
> root.PaymentArrangement.each { pa ->
> println "${row.agent_id}-${row.account_id} before:"
> println pa
> pa.each { paElement ->
> if (['FirstPaymentSameAccount', 'RequestedFirstDraftDate', 'AmountPaidWithApp'].contains(paElement.nodeName)) {
> pa.removeChild(paElement)
> }
> }
> pa.OneTimeDraft.each { otd ->
> otd.each { otdElement ->
> if (['Amount'].contains(otdElement.nodeName)) {
> def otdAmount = otd.removeChild(otdElement)
> pa.appendNode('OneTimeDraftAmount', otdAmount.getFirstChild().data)
> }
> }
> }
> pa.RecurringDraft.each { rd ->
> rd.each { rdElement ->
> if (['Amount'].contains(rdElement.nodeName)) {
> rd.removeChild(rdElement)
> }
> }
> }
> println "${row.agent_id}-${row.account_id} ${row.id} after:"
> println pa
> }
>
> String xmlString = new groovy.xml.StreamingMarkupBuilder().bind {
> mkp.yield dom
> }
> assert xmlString.indexOf('FirstPaymentSameAccount') == -1
>
> println xmlString
> }
> }
>
> When I run it I get a pretty version of my XML with &lt; and &gt; in place of the "<" and ">". Of course my desire is to write the XML back to the database which I can do using the not-very-Groovy code I cited.
>
> -Andy
>
>  > Not knowing what you're doing to modify the dom, here's an example
>  > that does some modification:
>  >
>  > def prettyxml = '''<a>
>  > | <b>c</b>
>  > |</a>'''.stripMargin()
>  >
>  > dom = new XmlSlurper().parseText( prettyxml )
>  > dom.appendNode {
>  > groovy( 'cool' )
>  > }
>  > String uglyxml = new groovy.xml.StreamingMarkupBuilder().bind {
>  > mkp.yield dom
>  > }
>  >
>  > assert uglyxml == '<a><b>c</b><groovy>cool</groovy></a>'


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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: XML without pretty printing

Andrew Goodspeed
Thanks Paul.

I came to much the same conclusion - that there is not much there for the DOM classes in this regard.

It seems very strange and unGroovy that there is no simple way to get the XML out in its natural form. Apparently it is pretty or nothing!

It might be worth adding to the DOM doc (http://groovy.codehaus.org/Creating+XML+with+Groovy+and+DOM).(?)

-Andy

> If you were using XmlParser you could use XmlNodePrinter
> and if you were using XmlSlurper you could do as Tim suggests directly
> but because you are using the DOM classes you would need something like:
>
>      mkp.yield new XmlSlurper().parseText(XmlUtil.serialize(root))
>
> Which will do the round-tripping similar to what your Java version
> does but with only one line of code.
Loading...