Quantcast

Modify XML

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

Modify XML

citron
Hi,

I have been trying to figure out how to modify an XML file containing namespaces without success.

Could someone please provide a code example how to do this?

Thanks!!


Express yourself instantly with MSN Messenger! MSN Messenger
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Modify XML

tugwilson

citron wrote
Hi,

I have been trying to figure out how to modify an XML file containing namespaces without success.

Could someone please provide a code example how to do this?

what sort of modification are you trying to do?

John Wilson

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

RE: Modify XML

citron
Hi John,
 
I have now managed to modify the xml but there is one problem left.
 
Every element are prefixed in the modified xml file, but the original file uses a default namespace.
 
How do I get rid of all the prefixes in the output?
 
I have tried the mkp.declareNamespace("":  "http://java.sun.com/xml/ns/j2ee") without success, as suggested here
http://www.nabble.com/XmlSlurper---StreamingMarkupBuilder---Namespaces-td7669152.html
 


> Date: Tue, 15 Jan 2008 02:25:09 -0800
> From: [hidden email]
> To: [hidden email]
> Subject: Re: [groovy-user] Modify XML
>
>
>
>
> citron wrote:
> >
> >
> > Hi,
> >
> > I have been trying to figure out how to modify an XML file containing
> > namespaces without success.
> >
> > Could someone please provide a code example how to do this?
> >
> >
>
>
> what sort of modification are you trying to do?
>
> John Wilson
>
>
> --
> View this message in context: http://www.nabble.com/Modify-XML-tp14813147p14836537.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
>



Express yourself instantly with MSN Messenger! MSN Messenger
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Modify XML

tugwilson

citron wrote
Hi John,
 
I have now managed to modify the xml but there is one problem left.
 
Every element are prefixed in the modified xml file, but the original file uses a default namespace.
 
How do I get rid of all the prefixes in the output?
 
I have tried the mkp.declareNamespace("":  "http://java.sun.com/xml/ns/j2ee") without success, as suggested here
http://www.nabble.com/XmlSlurper---StreamingMarkupBuilder---Namespaces-td7669152.html

Could you post a very small example?

it may be a bug:)

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

RE: Modify XML

citron
I can also see that " is replaced with ' and the first line eg. <?xml version="1.0" encoding="UTF-8"?> is missing.
 
Anyway, here is an example:
 
<groovy>
 import groovy.xml.*;
 def application = new XmlSlurper().parse(new File('D:/INPUT.xml')).declareNamespace( "":
'http://www.tibco.com/xmlns/ApplicationManagement')
      
 for (binding in application.services.bw.bindings.binding)
 {
  [hidden email] = <A href='mailto:binding.@name.text()+"_DEV'>binding.@...()+"_DEV"
  binding.machine = binding.machine.text()+"_DEV"
 }      

 def builder = new StreamingMarkupBuilder()
 def copier = builder.bind{ mkp.yield(application) }
 def result = "$copier"
 def destination = new FileWriter('D:/OUTPUT.xml')
 copier.writeTo(destination)         
</groovy>
 
********** INPUT ***********************
<?xml version="1.0" encoding="UTF-8"?>
<application name="EAIAdapter" xmlns="http://www.tibco.com/xmlns/ApplicationManagement"> 
   <services>    
      <bw name="EAIAdapter.par">                    
         <bindings>              
            <binding name="CITRON">                  
               <machine>citron</machine>        
            </binding>          
         </bindings>      
      </bw>
   </services>
</application>
********** OUTPUT ***********************
<tag0:application name='EAIAdapter' xmlns:ns1='http://www.tibco.com/xmlns/ApplicationManagement'>
 <tag0:services>
  <tag0:bw name='EAIAdapter.par'>
   <tag0:bindings>
    <tag0:binding name='CITRON_DEV'>
     <tag0:machine>citron_DEV</tag0:machine>
    </tag0:binding>
   </tag0:bindings>
  </tag0:bw>
 </tag0:services>
</tag0:application>


> Date: Tue, 15 Jan 2008 06:52:10 -0800
> From: [hidden email]
> To: [hidden email]
> Subject: RE: [groovy-user] Modify XML
>
>
>
>
> citron wrote:
> >
> >
> > Hi John,
> >
> > I have now managed to modify the xml but there is one problem left.
> >
> > Every element are prefixed in the modified xml file, but the original file
> > uses a default namespace.
> >
> > How do I get rid of all the prefixes in the output?
> >
> > I have tried the mkp.declareNamespace("":
> > "http://java.sun.com/xml/ns/j2ee") without success, as suggested here
> > http://www.nabble.com/XmlSlurper---StreamingMarkupBuilder---Namespaces-td7669152.html
> >
>
>
> Could you post a very small example?
>
> it may be a bug:)
>
> John Wilson
> --
> View this message in context: http://www.nabble.com/Modify-XML-tp14813147p14841365.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
>



Express yourself instantly with MSN Messenger! MSN Messenger
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Modify XML

tugwilson
Thanks,

That's *so* much clearer.

I'll play around with your example and post something tomorrow morning (my timezone is GMT).

John Wilson

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

RE: Modify XML

tugwilson
In reply to this post by citron

citron wrote
I can also see that " is replaced with ' and the first line eg. <?xml version="1.0" encoding="UTF-8"?> is missing.

Took less time than I expected:
import groovy.xml.StreamingMarkupBuilder
 
 doc = """<?xml version="1.0" encoding="UTF-8"?>
 <application name="EAIAdapter" xmlns="http://www.tibco.com/xmlns/ApplicationManagement"> 
    <services>   
       <bw name="EAIAdapter.par">                   
          <bindings>             
             <binding name="CITRON">                 
                <machine>citron</machine>       
             </binding>         
          </bindings>     
       </bw>
    </services>
 </application>"""

 def application = new XmlSlurper().parseText(doc)
     
application.services.bw.bindings.binding = {element->
                                      def attrs = element.attributes()
                                      attrs.name = "${attrs.name}_DEV"
                                      binding(attrs) {
                                        machine("${element.machine.text()}_DEV")
                                      }
 }

def builder = new StreamingMarkupBuilder()
System.out << builder.bind{
                                      mkp.xmlDeclaration()
                                       namespaces << ["": "http://www.tibco.com/xmlns/ApplicationManagement"]
                                        out << application
                                   }


Note the way we edit the document is to provide a closure which is called just before each binding element is to be emitted. The closure is given a GPathResult object which represents the binding element and its children. The closure is executed in builder mode and the result of executing the closure replaces the binding element in the document.

when we emit the document we explicitly emit the XML declaration and declare a default namespace.

No doubt you will have some more questions when you try it out yourself :)

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

RE: Modify XML

tugwilson
This is a more general solution. It allows multiple elements in the <binding> element.

import groovy.xml.StreamingMarkupBuilder
 
 doc = """<?xml version="1.0" encoding="UTF-8"?>
 <application name="EAIAdapter" xmlns="http://www.tibco.com/xmlns/ApplicationManagement"> 
    <services>   
       <bw name="EAIAdapter.par">                   
          <bindings>             
             <binding name="CITRON">                 
                <machine>citron</machine>     
             </binding>         
          </bindings>     
       </bw>
    </services>
 </application>"""

 def application = new XmlSlurper().parseText(doc)
     
application.services.bw.bindings.binding = {element->
                                      def attrs = element.attributes()
                                      attrs.name = "${attrs.name}_DEV"
                                      binding(attrs) {
                                        element.children().each {
                                          if (it.name() == "machine" ) {
                                            machine("${element.machine.text()}_DEV")
                                          } else {
                                            out << it
                                          }
                                        }
                                      }
 }

def builder = new StreamingMarkupBuilder()
 System.out << builder.bind{
                                mkp.xmlDeclaration()
                                namespaces << ["": "http://www.tibco.com/xmlns/ApplicationManagement"]
                                out << application
                              }
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Modify XML

citron
Thanks for your help John!
 
I have put the groovy code inside ant and its complainting about '<<'
Is it possible to modify the code to not use this syntax?
 
Thanks!


> Date: Wed, 16 Jan 2008 00:27:02 -0800
> From: [hidden email]
> To: [hidden email]
> Subject: RE: [groovy-user] Modify XML
>
>
> This is a more general solution. It allows multiple elements in the <binding>
> element.
>
> import groovy.xml.StreamingMarkupBuilder
>
> doc = """<?xml version="1.0" encoding="UTF-8"?>
> <application name="EAIAdapter"
> xmlns="http://www.tibco.com/xmlns/ApplicationManagement">
> <services>
> <bw name="EAIAdapter.par">
> <bindings>
> <binding name="CITRON">
> <machine>citron</machine>
> </binding>
> </bindings>
> </bw>
> </services>
> </application>"""
>
> def application = new XmlSlurper().parseText(doc)
>
> application.services.bw.bindings.binding = {element->
> def attrs = element.attributes()
> attrs.name = "${attrs.name}_DEV"
> binding(attrs) {
> element.children().each {
> if (it.name() == "machine" ) {
>
> machine("${element.machine.text()}_DEV")
> } else {
> out << it
> }
> }
> }
> }
>
> def builder = new StreamingMarkupBuilder()
> System.out << builder.bind{
> mkp.xmlDeclaration()
> namespaces << ["":
> "http://www.tibco.com/xmlns/ApplicationManagement"]
> out << application
> }
>
> --
> View this message in context: http://www.nabble.com/Modify-XML-tp14813147p14873660.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
>



Express yourself instantly with MSN Messenger! MSN Messenger
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Modify XML

Nick Rothwell
> I have put the groovy code inside ant and its complainting about '<<'
> Is it possible to modify the code to not use this syntax?

Can you not just escape the Groovy code in a CDATA section?

... Ant XML ...

<![CDATA[
... Groovy code
]]>

... more Ant XML ...





Nick Rothwell / Cassiel.com Limited
www.cassiel.com


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

    http://xircles.codehaus.org/manage_email

123
Loading...