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

Unable to modify value of element using XmlParser

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 1.5.6
    • Fix Version/s: 1.5.7
    • Component/s: XML Processing
    • Labels:
      None
    • Environment:
      Windows XP SP2, Sun JDK 1.5.0_15-b04

      Description

      Running the example described here in the Groovy User Guide results in a ClassCastException.

      Here is the testcase:

      class XmlExampleTest extends GroovyTestCase {
      void testThatChangingValueOfXmlWorks() {
      def input = '''
      <shopping>
      <category type="groceries">
      <item>Chocolate</item>
      <item>Coffee</item>
      </category>
      <category type="supplies">
      <item>Paper</item>
      <item quantity="4">Pens</item>
      </category>
      <category type="present">
      <item when="Aug 10">Kathryn's Birthday</item>
      </category>
      </shopping>
      '''

      def root = new XmlParser().parseText(input)

      def groceries = root.category.findAll

      { it.@type == 'groceries' }

      .item
      groceries.each

      { g -> g.value = 'Luxury ' + g.text() }

      }
      }

      This results in a ClassCastExcpetion:

      1) testThatChangingValueOfXmlWorks(com.xebia.proxy.maven.XmlExampleTest)java.lang.ClassCastException:
      java.lang.String
      at org.codehaus.groovy.reflection.CachedClass$IntegerCachedClass.coerceArgument(CachedClass.java:459)
      at org.codehaus.groovy.reflection.ParameterTypes.coerceArgumentsToClasses(ParameterTypes.java:138)
      at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:228)
      at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2007)
      at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2591)
      at groovy.lang.DelegatingMetaClass.setProperty(DelegatingMetaClass.java:161)
      at org.codehaus.groovy.runtime.InvokerHelper.setProperty(InvokerHelper.java:199)
      at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.setProperty(ScriptBytecodeAdapter.java:506)
      at com.xebia.proxy.maven.XmlExampleTest$_testThatChangingValueOfXmlWorks_closure2.doCall(XmlExampleTest.groovy:25)
      ..... (Rest of stacktrace omitted for readability)

      Changing the values of attributes in XML does work however.

        Attachments

          Activity

            People

            • Assignee:
              paulk Paul King
              Reporter:
              lvonk Lars Vonk
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: