Re: [Simple-support] ElementMap question
Brought to you by:
niallg
|
From: Micah J. <mi...@af...> - 2008-06-26 18:57:51
|
Thanks for responding! So I stumbled on a solution nearly the same as
what you suggested. In my below example, I separated out the value of
the text into a value tag just as you suggested, but kept everything
else the same. And the code suddenly started working (for reasons I
don't understand). I didn't have to create a list to put values into
a map. This was my new XML, code is still the same:
<foo level="0">
<value attr1="abc">value1</value>
</foo>
<foo level="1">
<value attr2="xyz">value2</value>
</foo>
<foo level="3">
<value attr1="123">value3</value>
</foo>
What is confusing to me on why this is working is I didn't create an
annotation to look for the tag "value". Does ElementMap do some
shortcutting to look for "value" tags for you?
-Micah
On Jun 24, 2008, at 10:16 AM, Niall Gallagher wrote:
> Hi,
>
> Have a look at the Dictionary class in org.simpleframework.xml.util.
> This looks like what you want. In order to use attributes in the
> value it has to be a sub-element of the entry and can not go in the
> entry element. For example:
>
> <entry key="key>
> <value a="a" b="b" c="c"/>
> </entry>
>
> In order to get a flat list you will need to use an ElementList. The
> Dictionary (which is a HashSet) object can make use of an attribute
> to do this. It has a get() method. If you want you can use an
> ElementList and transfer this via some key to a map using the
> @Commit annotation like so.
>
>
> public class MyClass {
>
> @ElementList(inline=true)
> private List<Foo> list;
> private Map<Integer, Foo> map;
>
> @Commit
> private void commit() {
> for(Foo foo : list) {
> map.put(foo.level, foo);
> }
> }
> }
>
> Hope this helps.
>
> Niall
>
>
> --- On Mon, 6/23/08, Micah Jaffe <mi...@af...> wrote:
>
>> From: Micah Jaffe <mi...@af...>
>> Subject: [Simple-support] ElementMap question
>> To: sim...@li...
>> Date: Monday, June 23, 2008, 2:37 PM
>> I have an ElementMap that I'm declaring as such:
>>
>> @ElementMap(entry="foo", key="level",
>> attribute=true, inline=true)
>> private HashMap<Integer, Foo> map = new
>> HashMap<Integer, Foo>();
>>
>> private static class Foo
>> {
>> @Attribute(required=false)
>> private String attr1;
>>
>> @Attribute(required=false)
>> private String attr2;
>>
>> @Text
>> private String value;
>> }
>>
>> The goal would be to be able to read XML like this:
>>
>> <foo level="0"
>> attr1="abc">value1</foo>
>> <foo level="1"
>> attr2="xyz">value2</foo>
>> <foo level="3"
>> attr1="123">value3</foo>
>>
>> When testing the code it seems to pull out the key
>> information
>> correctly, but is setting every value of Foo in the HashMap
>> to null.
>> The goal is to be able to retrieve an object from this map
>> that will
>> always be keyed off "level", and have N number of
>> attributes (meta
>> data) which are optional, and only a single value.
>>
>> What am I doing wrong?
>>
>> -Micah
>>
>> -------------------------------------------------------------------------
>> Check out the new SourceForge.net Marketplace.
>> It's the best place to buy or sell services for
>> just about anything Open Source.
>> http://sourceforge.net/services/buy/index.php
>> _______________________________________________
>> Simple-support mailing list
>> Sim...@li...
>> https://lists.sourceforge.net/lists/listinfo/simple-support
>
>
>
|