Re: [Simple-support] ElementMap with unknown child elements
Brought to you by:
niallg
|
From: Max S. <ma...@se...> - 2013-03-12 19:28:18
|
Hi,
I repost to the mailing list, so everybody gets the result. What you
need - at least it works - is a Converter for your Parent class:
public class ParentConverter implements Converter<Parent> {
@Override
public Parent read(InputNode arg0) throws Exception {
Parent newParent = new Parent();
InputNode curNode;
while ((curNode = arg0.getNext()) != null) {
newParent.children.put(curNode.getName(), curNode.getValue());
}
return newParent;
}
@Override
public void write(OutputNode arg0, Parent arg1) throws Exception {
for (Entry<String, String> curEntry : arg1.children.entrySet()) {
OutputNode newNode = arg0.getChild(curEntry.getKey());
newNode.setValue(curEntry.getValue());
newNode.commit();
}
}
}
So that this will be used to serialize/deserialize your Parent object,
you need to do the following (actually add the
"@Convert(ParentConverter.class)" annotation):
@Convert(ParentConverter.class)
public class Parent {
public HashMap<String,String> children = new HashMap<>();
}
Simple will only use the Convert annotation, if you create your
Persister object by passing an AnnotationStrategy:
Strategy strategy = new AnnotationStrategy();
Serializer serializer = new Persister(strategy);
But attention: Using this converter will ignore any other annotations
you did in your class, so you actually have to implement the full
reading and writing in the converter!
I just got another idea. Let me check this ...
Best regards
Max
-------- Original-Nachricht --------
Betreff: Re: [Simple-support] ElementMap with unknown child elements
Von: Alex Boyd <ale...@ou...>
An: Max Senft <ma...@se...>
Datum: 12.03.2013 20:00
> Yup that is exactly what I want.
>
> I think it is possible, but I haven't been able to get the behavior I
> want.
>
> Alex Boyd
>
> ------------------------------------------------------------------------
> Date: Tue, 12 Mar 2013 19:45:53 +0100
> From: ma...@se...
> To: sim...@li...
> Subject: Re: [Simple-support] ElementMap with unknown child elements
>
> Hi Alex,
>
> I'm not quite sure if I understood your problem: You need the element
> <Child1>Value1</Child1> to be inserted into the HashMap with "Child1"
> as key and "Value1" as value?
>
> Best regards
> Max
>
>
> schrieb Alex Boyd:
>
> Hey All,
>
> Given the XML :
>
> <Parent>
>
> <Child1>Value1</Child1>
>
> <Child2>Value2</Child2>
>
> <Child3>Value3</Child3>
>
> </Parent>
>
> How would I serialize / deserialize so that I have a Java Object like below. I don't know the name of the child elements, I just want to get key-value pairs from the children, with key being the element name, and value being the text element inside the xml.
>
> class Parent
>
> {
>
> public HashMap<String,String> children;
>
> }
>
> I have tried multiple different combinations of ElementMap and ElementList but couldn't seem to get it to work the way I wanted.
>
> Thanks in advance,
>
> Alex Boyd
>
>
>
> ------------------------------------------------------------------------------
> Symantec Endpoint Protection 12 positioned as A LEADER in The Forrester
> Wave(TM): Endpoint Security, Q1 2013 and "remains a good choice" in the
> endpoint security space. For insight on selecting the right partner to
> tackle endpoint security challenges, access the full report.
> http://p.sf.net/sfu/symantec-dev2dev
>
>
>
> _______________________________________________
> Simple-support mailing list
> Sim...@li... <mailto:Sim...@li...>
> https://lists.sourceforge.net/lists/listinfo/simple-support
>
>
>
> ------------------------------------------------------------------------------
> Everyone hates slow websites. So do we. Make your web apps faster with
> AppDynamics Download AppDynamics Lite for free today:
> http://p.sf.net/sfu/appdyn_d2d_mar
> _______________________________________________ Simple-support mailing
> list Sim...@li...
> https://lists.sourceforge.net/lists/listinfo/simple-support
|