Re: [Simple-support] How to deserialize an heterogeneous sequence of elements
Brought to you by:
niallg
|
From: Niall G. <gal...@ya...> - 2011-03-04 11:49:59
|
Hi,
Very shortly you will be able to do this, via something like this.
@VariantList({
@ElementList(entry="cleanDir", type=CleanDirCommand.class),
@ElementList(entry="copy", type=CopyCommand.class)
})
private List<Command> commands;
I have nearly completed this. It should be released in about a week or two. At the moment you would have to use the @Convert annotation and write a converter.
Niall
> I have an XML file that looks like this :
>
> /------------------- sample-script.xml
> -------------------------\
>
> <script>
> <copy srcDir="src" destDir="dest"
> filePattern="*.dep" />
> <cleanDir dir="log" />
> <!-- Other commands ....
> -->
> </script>
>
> \-------------------------- END
> -------------------------------/
>
>
> I want to parse the sequence of commands, each type of
> command transforming
> into a different class.
> I would like to avoid the use of "class='foo.Bar'"
> attributes.
>
>
> Here are 3 classes :
>
> /------------------- Script.java
> ------------------------------\
>
> @Root(name="script")
> public class Script {
> @ElementList(inline=true)
> private List<ICommand> commands;
> }
>
> \-------------------------- END
> -----------------------------/
>
>
> /--------------- CommandCopy.java
> -------------------------\
>
> @Root(name="copy")
> public class CommandCopy implements ICommand {
>
> @Attribute
> private String srcDir;
>
> @Attribute
> private String destDir;
>
> @Attribute(required=false)
> private String fileFilter = "*.*";
> }
>
> \------------------------ END
> ---------------------------------/
>
>
> /-------------- CommandCleanDir.java
> -------------------------\
>
> @Root(name="cleanDir")
> public class CommandCleanDir implements ICommand {
>
> // -- Attributes
> @Attribute
> private String dir;
> }
>
> \------------------------ END
> ---------------------------------/
>
> All those classes are in the same package.
> When I try parse this, I get the following error :
> org.simpleframework.xml.core.ElementException: Element
> 'copy' does not have
> a match in class fr.whatever.Script at line -1
>
> I think that Simple-XML could guess the class I want to
> instantiate, based
> on the @Root(name="something") annotation of the class.
> Should I register
> all the possible classes somewhere ? Or does simùple-XML
> finds out all
> annotated classes on his own ?
>
> Is there some way to achieve what I want with Simple-XML ?
>
> Thanks in advance for your help.
> Raphael
>
>
> --
> View this message in context: http://old.nabble.com/How-to-deserialize-an-heterogeneous-sequence-of-elements-tp30985952p30985952.html
> Sent from the Simple XML Serialization mailing list archive
> at Nabble.com.
>
>
> ------------------------------------------------------------------------------
> Free Software Download: Index, Search & Analyze Logs
> and other IT data in
> Real-Time with Splunk. Collect, index and harness all the
> fast moving IT data
> generated by your applications, servers and devices whether
> physical, virtual
> or in the cloud. Deliver compliance at lower cost and gain
> new business
> insights. http://p.sf.net/sfu/splunk-dev2dev
> _______________________________________________
> Simple-support mailing list
> Sim...@li...
> https://lists.sourceforge.net/lists/listinfo/simple-support
>
|