changing value of element in xml using xmlparser result in classcastexception

View: New views
1 Messages — Rating Filter:   Alert me  

changing value of element in xml using xmlparser result in classcastexception

by lars vonk :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I just ran the example as shown here:
http://groovy.codehaus.org/Updating+XML+with+XmlParser, but I get a
class castexception when trying to modify the value of an element.
Changing the value of an attribute does work however. Is this a bug? I
am using groovy 1.5.6 and JDK 1.5.0_15-b04.

The Code:

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()
    }
        }
}

The Exception I receive:

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)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
        at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:230)
        at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:248)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:756)
        at groovy.lang.Closure.call(Closure.java:292)
        at groovy.lang.Closure.call(Closure.java:305)
        at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:1079)
        at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:1056)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at org.codehaus.groovy.runtime.metaclass.ReflectionMetaMethod.invoke(ReflectionMetaMethod.java:51)
        at org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod.invoke(NewInstanceMetaMethod.java:54)
        at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:230)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:912)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:756)
        at org.codehaus.groovy.runtime.InvokerHelper.invokePojoMethod(InvokerHelper.java:766)
        at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:754)
        at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:170)
        at com.xebia.proxy.maven.XmlExampleTest.testThatChangingValueOfXmlWorks(XmlExampleTest.groovy:24)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)


Thanks in advance,

Lars

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

    http://xircles.codehaus.org/manage_email


LightInTheBox - Buy quality products at wholesale price