views:

22

answers:

1

Hi folks,

I've a problem. I want to have nice dynamic groovy classes to represent an ugly XML structure (unfortunately JAXB, XmlBeans etc. is not possible). For this goal I need case-sensitive properties to map the element values from XML to my classes.

But Groovy generates automatically lowercase property names due to the JavaBeans specification.

Can I override this feature and preserve my properties?

thanks

Here is an example:

The source XML looks like this:

<data>
<document>
    <linked name="MyDataObject">
        <datarow>
            <element name="Name"></element>
            <element name="CurrentTime"></element>
            <element name="abc"></element>
            <element name="UID"></element>
        </datarow>
    </linked>

    <!--
        Here are a lot of more of <element> and <linked> 
    -->

</document>
</data>

I parse it with XmlParser and let some classes work on the seperate nodes.

class XMLMyDataObject extends MyXMLNodeBaseClassWithDynamicFunctionality {
String Name
String CurrentTime
String abc
String UID
}

The result in this example due to the lowercase names: I can only find 2 elements.

XMLMyDataObject.metaClass.properties ...

name                cannot find XML-Element
currentTime         cannot find XML-Element
abc                 find XML-Element
UID                 find XML-Element

My current situation:

I'm using the pure Java part of my classes to get the property-names calling the getDeclaredFields method.

But the question is still interesting.

+1  A: 

Not sure what you are doing. Normally you would use XmlParser or XmlSlurper to parse xml in Groovy and none of those will change the names of your properties to lowercase.

Example:

def xml = """<person>
    <ADRESS>
        <PostalCode>43</PostalCode>
    </ADRESS>
</person>"""

new XmlParser().parseText(xml).ADRESS.PostalCode.text()
xlson
Yeah, you're right. I've to work with this XML structure (s.above). I can't work only with the XmlParser-result. I won't understand my code any more in two weeks. ;)
steyze