Re: [Simple-support] Parsing an XML containing Java keyword
Brought to you by:
niallg
|
From: Mishra, D. <c_d...@qu...> - 2012-07-02 09:02:49
|
Thanks for the reply Niall ! Let me rephrase my query...
I need to parse an XML like this :
<myEnum>new</myEnum>
My Java bean is this :
public enum MyEnum {
@Element(name="new")
NEW("new"),
OLD("old");
private final String value;
MyEnum(String v)
{
value = v;
}
}
My Java code for parsing is this :
public static void main(String[] args) throws Exception {
Serializer serializer = new Persister();
File source = new File("myenum.xml");
MyEnum example=null;
try{
example = serializer.read(MyEnum.class, source);
}
catch(IllegalArgumentException e)
{
e.printStackTrace();
}
}
The exception I get is this :
java.lang.IllegalArgumentException: No enum const class MyEnum.new
at java.lang.Enum.valueOf(Unknown Source)
at org.simpleframework.xml.transform.EnumTransform.read(EnumTransform.java:58)
at org.simpleframework.xml.transform.EnumTransform.read(EnumTransform.java:29)
at org.simpleframework.xml.transform.Transformer.read(Transformer.java:104)
How should I go around this ? Can you please provide some more pointers than those mentioned in your last reply.
Thanks !
________________________________
From: Niall Gallagher [gal...@ya...]
Sent: 02 July 2012 13:58:27
To: sim...@li...; Mishra, Deepak
Subject: Re: [Simple-support] Parsing an XML containing Java keyword
I am not sure what your trying to do here, but what you should do is implement your own EnumTransform and plug it in to the Persister using a Matcher.
--- On Fri, 29/6/12, Mishra, Deepak <c_d...@qu...> wrote:
From: Mishra, Deepak <c_d...@qu...>
Subject: [Simple-support] Parsing an XML containing Java keyword
To: "sim...@li..." <sim...@li...>
Received: Friday, 29 June, 2012, 3:49 AM
Hi,
We are working on an Android project which consumes some web-services not in our control. These web-services respond with an XML, with text value as "new". Example :
<myEnum>new</myEnum>
As per the XSD, we need a Java enum to code for this.
public enum MyEnum {
@Element(name="new")
new("new"), // but we can't use 'new' here as it's a Java keyword
old("old");
}
SimpleXml seems to use valueOf() - which we can't overrride and hence we are unable to proceed. Please suggest some alternatives. I got this working after changing SimpleXml source code, but this is not acceptable to my team !
PS : The exception if it helps (when I use NEW instead of new)
java.lang.IllegalArgumentException: No enum const class pojos2.MyEnum.new
at java.lang.Enum.valueOf(Unknown Source)
at org.simpleframework.xml.transform.EnumTransform.read(EnumTransform.java:58)
Thanks,
Deepak
-----Inline Attachment Follows-----
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
-----Inline Attachment Follows-----
_______________________________________________
Simple-support mailing list
Sim...@li...<UrlBlockedError.aspx>
https://lists.sourceforge.net/lists/listinfo/simple-support
|