Modifying xml, how to prepend node?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Modifying xml, how to prepend node?

citron
Hi,

I am trying to figure out how add an element to an existing xml file.

In the example below, I would like to add an <EnvironmentSummary> element with <environment> as parent and before the <hosts> element. But, the <EnvironmentSummary> element ends upp after the <hosts> element.

Many thanks for any suggestions how to achieve this!!

Desired output:

<enterprise..>
...
  <environment..>
    <EnvironmentSummary .../> <---- like this
    <hosts>
..
    </hosts>   
  </environment>
...
</enterprise>


Actual output:

<?xml version="1.0" encoding="UTF-8"?>
<enterprise name="myEnterprise">
  <environment name="dev">
    <hosts>
      <host name="host1" availDisk="10"/>
    </hosts>
    <EnvironmentSummary numberOfHosts="1" AvailDisk="10"/>
  </environment>
  <environment name="tst">
    <hosts>
      <host name="host1" availDisk="200"/>
      <host name="host2" availDisk="200"/>
    </hosts>
    <EnvironmentSummary numberOfHosts="2" AvailDisk="400"/>
  </environment>
  <environment name="prd">
    <hosts>
      <host name="host1" availDisk="300"/>
      <host name="host2" availDisk="300"/>
      <host name="host3" availDisk="300"/>
    </hosts>
    <EnvironmentSummary numberOfHosts="3" AvailDisk="900"/>
  </environment>
</enterprise>





def xml = """<enterprise name="myEnterprise">
                <environment name ="dev">
                    <hosts>
                        <host name="host1" availDisk="10"/>
                    </hosts>
                </environment>
                <environment name ="tst">
                    <hosts>
                        <host name="host1" availDisk="200"/>
                        <host name="host2" availDisk="200"/>
                    </hosts>
                </environment>
                <environment name ="prd">
                    <hosts>
                        <host name="host1" availDisk="300"/>
                        <host name="host2" availDisk="300"/>
                        <host name="host3" availDisk="300"/>
                     </hosts>               
                </environment>                                 
             </enterprise>"""
             
             
def enterprise = new XmlSlurper().parseText(xml)

enterprise.environment.each{environment->
    int numberOfHosts =0
    int totalAvailDisk = 0
    environment.hosts.host.each{host->
        numberOfHosts++
        totalAvailDisk+=host.@availDisk.toInteger()
    }
    environment.appendNode{
        EnvironmentSummary(numberOfHosts:numberOfHosts,AvailDisk:totalAvailDisk)
    }
}


println groovy.xml.XmlUtil.serialize(enterprise)        
Reply | Threaded
Open this post in threaded view
|

RE: Modifying xml, how to prepend node?

Bob Brown
I couldn't work out a way of doing this with XmlSlurper.

It can be done with XmlParser, though...not too much changes:

///
def xml = """
  [...elided, as before...]
"""
             
def enterprise = new XmlParser().parseText(xml)

enterprise.environment.each{environment->
    int numberOfHosts =0
    int totalAvailDisk = 0
    environment.hosts.host.each{host->
        numberOfHosts++
        totalAvailDisk+=[hidden email]()
    }
    def es = new Node(null, 'EnvironmentSummary',
[numberOfHosts:numberOfHosts,AvailDisk:totalAvailDisk])
    environment.children().add(0, es)
}

new XmlNodePrinter().print(enterprise)
///

Cheers,

BOB

> -----Original Message-----
> From: citron [mailto:[hidden email]]
> Sent: Friday, 8 March 2013 8:50 AM
> To: [hidden email]
> Subject: [groovy-user] Modifying xml, how to prepend node?
>
> Hi,
>
> I am trying to figure out how add an element to an existing xml file.
>
> In the example below, I would like to add an <EnvironmentSummary>
> element with <environment> as parent and before the <hosts> element.
> But, the <EnvironmentSummary> element ends upp after the <hosts>
> element.
>
> Many thanks for any suggestions how to achieve this!!
>
> *Desired output:*
>
> <enterprise..>
> ...
>   <environment..>
>    * <EnvironmentSummary .../>* <---- like this
>     <hosts>
> ..
>     </hosts>
>   </environment>
> ...
> </enterprise>
>
>
> *Actual output:*
>
> <?xml version="1.0" encoding="UTF-8"?>
> <enterprise name="myEnterprise">
>   <environment name="dev">
>     <hosts>
>       <host name="host1" availDisk="10"/>
>     </hosts>
>     *<EnvironmentSummary numberOfHosts="1" AvailDisk="10"/>*
>   </environment>
>   <environment name="tst">
>     <hosts>
>       <host name="host1" availDisk="200"/>
>       <host name="host2" availDisk="200"/>
>     </hosts>
>     *<EnvironmentSummary numberOfHosts="2" AvailDisk="400"/>*
>   </environment>
>   <environment name="prd">
>     <hosts>
>       <host name="host1" availDisk="300"/>
>       <host name="host2" availDisk="300"/>
>       <host name="host3" availDisk="300"/>
>     </hosts>
>     *<EnvironmentSummary numberOfHosts="3" AvailDisk="900"/>*
>   </environment>
> </enterprise>
>
>
>
>
>
> def xml = """<enterprise name="myEnterprise">
>                 <environment name ="dev">
>                     <hosts>
>                         <host name="host1" availDisk="10"/>
>                     </hosts>
>                 </environment>
>                 <environment name ="tst">
>                     <hosts>
>                         <host name="host1" availDisk="200"/>
>                         <host name="host2" availDisk="200"/>
>                     </hosts>
>                 </environment>
>                 <environment name ="prd">
>                     <hosts>
>                         <host name="host1" availDisk="300"/>
>                         <host name="host2" availDisk="300"/>
>                         <host name="host3" availDisk="300"/>
>                      </hosts>
>                 </environment>
>              </enterprise>"""
>
>
> def enterprise = new XmlSlurper().parseText(xml)
>
> enterprise.environment.each{environment->
>     int numberOfHosts =0
>     int totalAvailDisk = 0
>     environment.hosts.host.each{host->
>         numberOfHosts++
>         totalAvailDisk+=[hidden email]()
>     }
>     environment.appendNode{
>
> EnvironmentSummary(numberOfHosts:numberOfHosts,AvailDisk:totalAvailD
> isk)
>     }
> }
>
>
> println groovy.xml.XmlUtil.serialize(enterprise)
>
>
>
>
> -----
> Project pages
> AndersTool
> earBuddy
>
> --
> View this message in context:
> http://groovy.329449.n5.nabble.com/Modifying-xml-how-to-prepend-node-
> tp5713584.html
> Sent from the groovy - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> 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 | Threaded
Open this post in threaded view
|

RE: Modifying xml, how to prepend node?

citron
Excellent solution, many thanks!