Re: [Simple-support] help with deserializing XML to ElementMap
Brought to you by:
niallg
|
From: Niall G. <gal...@ya...> - 2008-03-05 18:40:54
|
Hi Micah,
I've tested this, everything works as expected.
However you have no need for the Field class. If you
specify a map key as an attribute=true then its
inlined in to the created wrapper element entry=true.
So if you use the following:
@ElementMap(name="fields", entry="field",
key="id", attribute=true)
private HashMap<String, Lucene> fields = new
HashMap<String, Lucene>();
Then everything works fine. BTW, if you use generics
you don't need to declare keyType or valueType in the
annotation.
Niall
--- Micah Jaffe <mi...@af...> wrote:
> I'm trying to convert some existing XML parsing code
> to use Simple
> XML and am almost there, but I'm running into an
> issue trying to use
> ElementMap.
>
> Here's the XML (abbreviated):
>
> <?xml version="1.0" encoding="ISO-8859-1"?>
> <index id="users">
> <database>xyz</database>
> <query>
> <columns>foo,bar</columns>
> <tables>a,b,c</tables>
> </query>
> <fields>
> <field id="foo">
> <lucene>
> <index>TOKENIZED</index>
> <store>false</store>
> <default>true</default>
> </lucene>
> </field>
> <field id="bar">
> <lucene>
> <index>TOKENIZED</index>
> <store>false</store>
> <default>true</default>
> </lucene>
> </field>
> </fields>
> </index>
>
> Here are my classes:
>
> @Root(name="index")
> public class IndexConfig {
>
> @Attribute
> private String id;
>
> @Element
> private String database;
>
> @Element
> private Query query;
>
> @ElementMap(name="fields", entry="field",
> key="id",
> attribute=true, keyType=String.class,
> valueType=Field.class)
> private HashMap<String, Field> fields = new
> HashMap<String,
> Field>();
> }
>
> @Root
> public class Field {
>
> @Attribute
> private String id;
>
> @Element
> private Lucene lucene;
> }
>
> [Not including Query or Lucene classes here as
> they're not at issue]
>
> The parsing chokes here:
>
> org.simpleframework.xml.load.ValueRequiredException:
> Unable to
> satisfy @org.simpleframework.xml.Attribute(name=,
> empty=,
> required=true) on field 'id' for class
> com.affinitycircles.core.xml.Field at line 16
> at
> org.simpleframework.xml.load.Composite.validate
> (Composite.java:389)
> at
>
org.simpleframework.xml.load.Composite.readAttributes
>
> (Composite.java:221)
> at
> org.simpleframework.xml.load.Composite.read
> (Composite.java:193)
> [... the rest deleted ...]
>
> For some reason it can't seem to find the id
> attribute for Field. If
> I make the id attribute optional, then it seems the
> parser gets
> confused and is looking for elements in the Lucene
> block. If I
> change ElementMap to instead be an ElementList, the
> file will parse.
> I've tried not using @Root with Field and that
> doesn't affect the
> outcome (I'm really not sure what @Root is for...).
>
> Any help?
>
> -Micah
>
>
-------------------------------------------------------------------------
> This SF.net email is sponsored by: Microsoft
> Defy all challenges. Microsoft(R) Visual Studio
> 2008.
>
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
> _______________________________________________
> Simple-support mailing list
> Sim...@li...
>
https://lists.sourceforge.net/lists/listinfo/simple-support
>
____________________________________________________________________________________
Looking for last minute shopping deals?
Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsearch/category.php?category=shopping
|