Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-1955

Adding complete body of XML tag with GPathResult.getBody() including CDATA sections

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • None
    • 1.1-beta-2
    • XML Processing
    • None

    Description

      Closes a gap in GPathResult methods by providing a new "getBody()" method.
      The method extracts the complete body of an XML tag into a closure wich can
      then be inserted at another location in the XML builder.

      The example below demonstrates how to use the getBody() method.
      In this example the <sec> tag is replaced by a <t> tag.
      Then the body of the <sec> tag is inserted into the <t> tag.

      The expected result is: <doc><t>Hello<p>World</p></t></doc>

      Existing methods of GPathResult ignore CDATA. In the example below
      the "Hello" would be ignored.

      def xmlOut = new StreamingMarkupBuilder()
      def xmlInStr ="""
      <doc><sec>Hello<p>World</p></sec></doc>
      """

      GPathResult xmlIn = new XmlSlurper().parseText(xmlInStr)

      xmlIn.sec.replaceNode{ node ->
      t()

      { delegate.mkp.yield node.getBody() }

      }

      println xmlOut.bind

      { mkp.yield xmlIn }

      Attachments

        Activity

          People

            blackdrag Jochen Theodorou
            kaf Frank Kaiser
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: