[Simple-support] help with deserializing XML to ElementMap
Brought to you by:
niallg
|
From: Micah J. <mi...@af...> - 2008-02-23 00:27:41
|
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
|