开发者

How to prevent groovy from creating lowercase (meta) property names?

开发者 https://www.devze.com 2023-01-22 18:08 出处:网络
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).

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.


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

精彩评论

暂无评论...
验证码 换一张
取 消