How to comment/uncomment XML nodes?

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

How to comment/uncomment XML nodes?

pkc
This post has NOT been accepted by the mailing list yet.

if ( xyzEnabled ) {
  // uncomment node in xml config file if commented out since it is now "enabled"
  // or if it doesn't exist in a comment add a default node
} else {
  // comment out node in xml config file since it is "disabled"
}

The nodes are fairly complex and I cannot make assumptions about the format since people may manually edit the document BUT most likely it will be the default XML document where nodes are commented or uncommented based on initial installation configuration.

Reply | Threaded
Open this post in threaded view
|

Re: How to comment/uncomment XML nodes?

Opal
It seems to be better idea to prepare different xml files for various purposes (if the nodes are too complex to handle them) and redesign the service to make use of an appropriate document.

According to (un)commenting - no idea how to do it.
Reply | Threaded
Open this post in threaded view
|

Re: How to comment/uncomment XML nodes?

Bob Brown
Hmmm.

The best I have been able to come up with is actually pretty crappy...

///
import groovy.xml.DOMBuilder
import groovy.xml.dom.DOMCategory
import groovy.xml.XmlUtil

final CAR_RECORDS = '''
    <records>
      <car name='HSV Maloo' make='Holden' year='2006'>
        <country>Australia</country>
        <record type='speed'>Production Pickup Truck with speed of
271kph</record>
      </car>
    </records>
  '''

def reader  = new StringReader(CAR_RECORDS)
def doc     = DOMBuilder.parse(reader)
def records = doc.documentElement
 
use (DOMCategory) {

  def maloo = records.'car'[0]
  assert maloo.'@name' == 'HSV Maloo'
 
  maloo.replaceNode {
    'comment' XmlUtil.serialize(maloo)['<?xml version="1.0"
encoding="UTF-8"?>'.size()..-1]
  }
 
  println XmlUtil.serialize(records).replaceAll('<comment>',
'<!--').replaceAll('</comment>', '-->')
}
///

There HAS to be a better way! Any Groovy Geniuses out there?

> -----Original Message-----
> From: Opal [mailto:[hidden email]]
> Sent: Saturday, 31 August 2013 7:58 PM
> To: [hidden email]
> Subject: [groovy-user] Re: How to comment/uncomment XML nodes?
>
> It seems to be better idea to prepare different xml files for various
purposes

> (if the nodes are too complex to handle them) and redesign the service to
> make use of an appropriate document.
>
> According to (un)commenting - no idea how to do it.
>
>
>
> --
> View this message in context: http://groovy.329449.n5.nabble.com/How-to-
> comment-uncomment-XML-nodes-tp5716682p5716694.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