simple-support Mailing List for Simple (Page 31)
Brought to you by:
niallg
You can subscribe to this list here.
| 2007 |
Jan
|
Feb
(2) |
Mar
(2) |
Apr
(13) |
May
(13) |
Jun
(27) |
Jul
(4) |
Aug
(14) |
Sep
(7) |
Oct
|
Nov
(6) |
Dec
(24) |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 2008 |
Jan
|
Feb
(21) |
Mar
(10) |
Apr
(15) |
May
(24) |
Jun
(24) |
Jul
(30) |
Aug
(5) |
Sep
(19) |
Oct
(27) |
Nov
(16) |
Dec
(24) |
| 2009 |
Jan
(34) |
Feb
(24) |
Mar
(35) |
Apr
(26) |
May
(8) |
Jun
(17) |
Jul
(28) |
Aug
(31) |
Sep
(36) |
Oct
(35) |
Nov
(20) |
Dec
(16) |
| 2010 |
Jan
(40) |
Feb
(21) |
Mar
(47) |
Apr
(45) |
May
(34) |
Jun
(68) |
Jul
(46) |
Aug
(39) |
Sep
(47) |
Oct
(20) |
Nov
(42) |
Dec
(13) |
| 2011 |
Jan
(41) |
Feb
(16) |
Mar
(32) |
Apr
(44) |
May
(28) |
Jun
(35) |
Jul
(37) |
Aug
(33) |
Sep
(60) |
Oct
(20) |
Nov
(35) |
Dec
(23) |
| 2012 |
Jan
(34) |
Feb
(23) |
Mar
(34) |
Apr
(21) |
May
(48) |
Jun
(24) |
Jul
(31) |
Aug
(39) |
Sep
(25) |
Oct
(10) |
Nov
(27) |
Dec
(28) |
| 2013 |
Jan
(32) |
Feb
(24) |
Mar
(24) |
Apr
(9) |
May
(4) |
Jun
(6) |
Jul
(2) |
Aug
(5) |
Sep
|
Oct
(5) |
Nov
(1) |
Dec
(12) |
| 2014 |
Jan
(14) |
Feb
(16) |
Mar
(5) |
Apr
(3) |
May
(2) |
Jun
(8) |
Jul
(2) |
Aug
|
Sep
(6) |
Oct
|
Nov
(6) |
Dec
|
| 2015 |
Jan
(3) |
Feb
(15) |
Mar
(7) |
Apr
|
May
|
Jun
(1) |
Jul
|
Aug
|
Sep
(2) |
Oct
|
Nov
|
Dec
(1) |
| 2016 |
Jan
|
Feb
(6) |
Mar
(2) |
Apr
|
May
|
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
| 2017 |
Jan
|
Feb
|
Mar
|
Apr
|
May
|
Jun
(1) |
Jul
|
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
| 2018 |
Jan
(1) |
Feb
|
Mar
|
Apr
|
May
|
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
(1) |
Dec
|
| 2019 |
Jan
|
Feb
|
Mar
|
Apr
(1) |
May
|
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
|
From: Niall G. <gal...@ya...> - 2011-07-03 05:43:19
|
Hi, Thanks, I will take a look at this. Niall --- On Sun, 12/6/11, Robert Massaioli <rob...@gm...> wrote: From: Robert Massaioli <rob...@gm...> Subject: [Simple-support] Simple XML: Consider Using This To: sim...@li... Received: Sunday, 12 June, 2011, 6:39 AM Hi Simple XML, There was this really interesting question on SO regarding how to have nested elements that appear in every class. I answered the question but you might have a better way to do it. Either way after you have a look at it it might be worth incorporating it into your tutorial in some way: http://stackoverflow.com/q/6197196/83446 Enjoy, Robert -----Inline Attachment Follows----- ------------------------------------------------------------------------------ All of the data generated in your IT infrastructure is seriously valuable. Why? It contains a definitive record of application performance, security threats, fraudulent activity, and more. Splunk takes this data and makes sense of it. IT sense. And common sense. http://p.sf.net/sfu/splunk-d2d-c2 -----Inline Attachment Follows----- _______________________________________________ Simple-support mailing list Sim...@li... https://lists.sourceforge.net/lists/listinfo/simple-support |
|
From: Niall G. <gal...@ya...> - 2011-07-03 05:41:44
|
Hi,
I think you are missing some code here. You are likely missing a no-arg constructor, or you are trying to deserialize a non-static inner class. Or you have either a final field annotated or a getter method annotated without a setter method annotated.
Provide a complete listing of your source and I can answer here.
Niall
--- On Fri, 10/6/11, joh sar..... <eh...@gm...> wrote:
From: joh sar..... <eh...@gm...>
Subject: [Simple-support] Problem during deserializing
To: sim...@li...
Received: Friday, 10 June, 2011, 6:35 AM
Hi all,
Thanks for the library, it's very helpful and very efficient.
I am writing to you because I have a deserializing problem.
I try to deserializing a xml that i have serialized with your library and i don't find my bug.
May be someone could help me or give me a clue;
The log is : ERROR/erreur(15302): org.simpleframework.xml.core.PersistenceException: Constructor not matched for class construction_Xml_Data.IDE
where IDE is one of my class that i have implemented. I don't know why it did not find my class !
Thanks !
Johan
This is my code :
/-----------------------
@Root(strict = false)
public class Utilisateurs {
@ElementList(type = Medecin.class)
private ArrayList<Medecin> medecins;
@ElementList(type = IDE.class)
private ArrayList<IDE> ides;
... constructors and getters
------------------------------/
/---------------------------------
@Root
public class Medecin {
@Element
private String nom;
@Element
private String prenom;
@Element
private String specialite;
@Element
private int encours;
.... constructors and getters
-------------------------------------/
/---------------------------------------
@Root
public class IDE {
@Element
private String nom;
@Element
private String prenom;
@Element
private String specialite;
@Element
private int encours;
... constructor and getters
-------------------------------------------/
And that is the read code:
try {
Serializer serializer = new Persister();
File file = new File(path);
Utilisateurs util = serializer.read(Utilisateurs.class, file,false); <--- the problem and i'm sure that my path is good
} catch (Exception e) {
// TODO Auto-generated catch block
Log.e("erreur", "pas d'ecriture xml", e);
}
-----Inline Attachment Follows-----
------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2
-----Inline Attachment Follows-----
_______________________________________________
Simple-support mailing list
Sim...@li...
https://lists.sourceforge.net/lists/listinfo/simple-support
|
|
From: Niall G. <gal...@ya...> - 2011-07-03 05:34:14
|
Hi Jens, I think you are confused here about what Simple is doing. Simple is an XML framework, it deals with XML only. Also, Simple does not parse any XML, it delegates this to thirdparty libraries such as StAX, W3C DOM, and KXML depending on the platform its running on. These parsers are standardised, they are correct in their use of XML. I have introduced no special cases here. Niall --- On Thu, 23/6/11, Stumpf, Jens Thorsten <Tho...@st...> wrote: From: Stumpf, Jens Thorsten <Tho...@st...> Subject: [Simple-support] Simple XML framework can't handle foreign markup in Strings - forced use of CDATA To: sim...@li... Received: Thursday, 23 June, 2011, 11:16 AM Hi! I’m quite disappointed: Simple XML framework forces the usage of superfluous CDATA-blocks. On unserialising XML-Files with elements containing markup, e.g. HTML, Simple XML framework transforms the HTML to pure text discarding all mark up: <description id="eid-191" composed="0"> <div> <p>The quick brown <a href=”www.example.com/fox”>fox</a>.</p> </div> </description> and @Element String description; yields description to contain “The quick brown fox” but not <div> <p>The quick brown <a href=”www.example.com/fox”>fox</a>.</p> </div> This is crippling the provided data, undocumented and most probably unnecessary. The only way to get around it is to use CDATA blocks which is error prone and evil: Every additional CDATA-block bloats the XML files. Every CDATA block renders substructures invalidateable. Every CDATA block makes the generated *and* read data less suitable for true XML chains. CDATA is *only* recommendable if the substructure is not valid itself. The classes exactly define the parts which should be read into attributes so there is no need to cripple the contained data. It is a fairly common use case of XML based markup languages to contain foreign other markup languages and it is not sensible to have to implement the whole set of elements of these. It is highly undesirable to be forced to try to sidestep existing standards fixed in published RFCs. Additionally obviously Simple XML framework shall not alter the content of an element if there isn’t a provided method (without a warning). To alter the content somehow breaches the essential rules of object orientation. The mixed content of an element annotated as element of type string e.g. can and should always simply be stored as String containing the markup elements. If the library is decent it might though integrate an optional switch to do so or code to get rid of the putatively superfluous child nodes. To preserve a reasonable XML chain and gain valid XML only such texts should have to be contained in a CDATA block which contain not well formed data. This is a sensible usage for CDATA. The decision to insert the data as CDATA might even be deferred till runtime. Usage should be transparent and painless for the developer and an alternative provider of XML data. There has to be an option (annotation) to enabling an developer to keep the markup without bloating the code at least. As said before it’s much more preferable to conserve per default contents of String elements as provided. For example I need the contained HTML mark up (see above) but I can’t alter the provider. I’m looking forward for an answer – but according to my experiences before I unfortunately think nobody will reply at all so this might just end up as being a warning for other developers… Jens -----Inline Attachment Follows----- ------------------------------------------------------------------------------ Simplify data backup and recovery for your virtual environment with vRanger. Installation's a snap, and flexible recovery options mean your data is safe, secure and there when you need it. Data protection magic? Nope - It's vRanger. Get your free trial download today. http://p.sf.net/sfu/quest-sfdev2dev -----Inline Attachment Follows----- _______________________________________________ Simple-support mailing list Sim...@li... https://lists.sourceforge.net/lists/listinfo/simple-support |
|
From: Niall G. <gal...@ya...> - 2011-07-03 05:27:25
|
Hi, The prefix here is "atom" so the annotation is @Namespace(prefix="atom", reference="http://www.domain.com/etc") The string "xmlns" is a reserved attribute name used to declare a namespace, it is never a prefix. Niall --- On Mon, 27/6/11, Paul Teale <pt...@xe...> wrote: > From: Paul Teale <pt...@xe...> > Subject: [Simple-support] Using Simple with RSS feed > To: sim...@li... > Received: Monday, 27 June, 2011, 6:24 AM > I am trying to use Simple XML for > handling RSS feeds and am currently > annotating the classes used to represent the RSS channel > and items. > > In the RSS feed I am trying to process one of the lines > looks like this.. > > <atom:link href="http://feeds.bbci.co.uk/news/science_and_environment/rss.xml" > rel="self" type="application/rss+xml"/> > > >From what I have read I need to declare the namespace > for this to work > as it also uses the link tag but I am not very clear on how > to do this > and the documentation didn't help. > > I have 3 classes, NewsFeed, Channel and AtomLink listed > below with annotations. > > @Root > @Namespace(reference="http://search.yahoo.com/mrss/") > public class NewsFeed { > > @Attribute > public String version; > > @Element > @Namespace(reference="http://www.w3.org/2005/Atom", prefix="xmlns") > public Channel channel; > > public Channel getChannel() { > return channel; > } > } > > @Root > @Namespace(reference="http://www.w3.org/2005/Atom") > public class Channel { > > @Element > public String title; > @Element > public String link; > @Element > public String description; > @Element > public String language; > @Element > public String lastBuildDate; > @Element > public String copyright; > @Element > public String ttl; > @Element > @Namespace(reference="http://www.w3.org/2005/Atom") > public AtomLink atomLink; > } > > > @Root > @Namespace(reference="http://www.w3.org/2005/Atom") > public class AtomLink { > > @Element > public String href; > @Element > public String rel; > @Element > public String type; > } > > > When I try this I get the error Element 'link' declared > twice at line 13 > If anyone can help with why this is happening I would be > very grateful. > > Thanks > Paul > > ------------------------------------------------------------------------------ > All of the data generated in your IT infrastructure is > seriously valuable. > Why? It contains a definitive record of application > performance, security > threats, fraudulent activity, and more. Splunk takes this > data and makes > sense of it. IT sense. And common sense. > http://p.sf.net/sfu/splunk-d2d-c2 > _______________________________________________ > Simple-support mailing list > Sim...@li... > https://lists.sourceforge.net/lists/listinfo/simple-support > |
|
From: Niall G. <gal...@ya...> - 2011-07-02 14:30:02
|
Hi,
Why not transform with
if(string.equalsIgnoreCase("true") || string.equalsIgnoreCase("Y")) {return true;}if(string.equalsIgnoreCase("false") || string.equalsIgnoreCase("N")) { return false;}throw exception;
If this does not work, they why not use a Visitor or a custom strategy.
Niall
--- On Tue, 28/6/11, Owen Mathews <ow...@bi...> wrote:
From: Owen Mathews <ow...@bi...>
Subject: [Simple-support] Multiple custom boolean formats using @Convert
To: sim...@li...
Received: Tuesday, 28 June, 2011, 6:05 AM
Hi all,
I'm dealing with some inconsistent XML that I must parse. See below:
<example tfBool="true" ynBool="Y" />
You can see that the tfBool attribute will be parsed properly, but the ynBool attribute needs special conversion. Here's my attempted solution:
@Rootpublic class Example {
private static String testXML = "<example tfBool=\"true\" ynBool=\"Y\" />";
@Attribute
public boolean tfBool;
@Attribute @Convert(Example.YesNoBooleanConverter.class)
public boolean ynBool;
public static class YesNoBooleanConverter implements Converter<Boolean> {
public Boolean read(InputNode node) throws Exception {
return new Boolean(node.getValue().equals("Y"));
}
public void write(OutputNode node, Boolean value) throws Exception {
node.setValue(value.booleanValue()?"Y":"N");
}
}
public static void main(String[] args) throws Exception {
Serializer s = new Persister(new AnnotationStrategy());
Example e = s.read(Example.class, new StringReader(testXML));
System.out.println("tfBool = " + e.tfBool);
System.out.println("ynBool = " + e.ynBool);
}
}
This code does not work. The output is:
tfBool = true
ynBool = false
As far as I can tell, YesNoBooleanConverter is never used. I'm assuming that it's because the boolean primitive type already matches to an existing Transformer implementation, so there's no way to use @Convert to use a custom converter if it's already mapped by the PrimitiveMatcher.
I can't use my own custom Transform/Match setup in this case, because the implementation is inconsistent (and as far as I can tell, this also eliminates the RegistryStrategy as an option as well). If all boolean values were represented by "Y" and "N", then I could set up a Matcher for boolean and put together a Transform that converts between strings properly. I need to convert standard boolean string representations for the first attribute and custom boolean string representations for the second.
Is there any way to do this easily (without having to have a custom converter for the whole class, for example)?
Owen
-----Inline Attachment Follows-----
------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2
-----Inline Attachment Follows-----
_______________________________________________
Simple-support mailing list
Sim...@li...
https://lists.sourceforge.net/lists/listinfo/simple-support
|
|
From: Owen M. <ow...@bi...> - 2011-06-28 13:05:33
|
Hi all,
I'm dealing with some inconsistent XML that I must parse. See below:
<example tfBool="true" ynBool="Y" />
You can see that the tfBool attribute will be parsed properly, but the
ynBool attribute needs special conversion. Here's my attempted solution:
@Root
public class Example {
private static String testXML = "<example tfBool=\"true\" ynBool=\"Y\"
/>";
@Attribute
public boolean tfBool;
@Attribute
@Convert(Example.YesNoBooleanConverter.class)
public boolean ynBool;
public static class YesNoBooleanConverter implements Converter<Boolean>
{
public Boolean read(InputNode node) throws Exception {
return new Boolean(node.getValue().equals("Y"));
}
public void write(OutputNode node, Boolean value) throws Exception {
node.setValue(value.booleanValue()?"Y":"N");
}
}
public static void main(String[] args) throws Exception {
Serializer s = new Persister(new AnnotationStrategy());
Example e = s.read(Example.class, new StringReader(testXML));
System.out.println("tfBool = " + e.tfBool);
System.out.println("ynBool = " + e.ynBool);
}
}
This code does not work. The output is:
tfBool = true
ynBool = false
As far as I can tell, YesNoBooleanConverter is never used. I'm assuming that
it's because the boolean primitive type already matches to an existing
Transformer implementation, so there's no way to use @Convert to use a
custom converter if it's already mapped by the PrimitiveMatcher.
I can't use my own custom Transform/Match setup in this case, because the
implementation is inconsistent (and as far as I can tell, this also
eliminates the RegistryStrategy as an option as well). If all boolean values
were represented by "Y" and "N", then I could set up a Matcher for boolean
and put together a Transform that converts between strings properly. I need
to convert standard boolean string representations for the first attribute
and custom boolean string representations for the second.
Is there any way to do this easily (without having to have a custom
converter for the whole class, for example)?
Owen
|
|
From: Stumpf, J. T. <Tho...@st...> - 2011-06-28 11:10:53
|
Yeah, the list is working working in principle. Though, unfortunately, you shouldn't expect vivid participation. I send three requests on simple to more complex issues and topics at least worth to be discussed. As you might guess there was no response at all. And I'm afraid I can't give you assistance. Regards JT ----- Reply message ----- Von: "Paul Teale" <pt...@xe...> Datum: Di., Jun. 28, 2011 12:36 Betreff: [Simple-support] list working? An: "sim...@li..." <sim...@li...> Is this list still working and active? I posted something yesterday and I didn't receive any email to suggest it got distributed via the mailing list, even though my setting is to receive email. Also I haven't received any other emails from anyone else on the list. Paul ------------------------------------------------------------------------------ All of the data generated in your IT infrastructure is seriously valuable. Why? It contains a definitive record of application performance, security threats, fraudulent activity, and more. Splunk takes this data and makes sense of it. IT sense. And common sense. http://p.sf.net/sfu/splunk-d2d-c2 _______________________________________________ Simple-support mailing list Sim...@li... https://lists.sourceforge.net/lists/listinfo/simple-support |
|
From: Paul T. <pt...@xe...> - 2011-06-28 10:35:54
|
Is this list still working and active? I posted something yesterday and I didn't receive any email to suggest it got distributed via the mailing list, even though my setting is to receive email. Also I haven't received any other emails from anyone else on the list. Paul |
|
From: <ses...@al...> - 2011-06-28 08:36:27
|
Hi all,
I'm dealing with some inconsistent XML that I must parse. See below:
<example tfBool="true" ynBool="Y" />
You can see that the tfBool attribute will be parsed properly, but the ynBool attribute needs special conversion. Here's my attempted solution:
@Root
public class Example {
private static String testXML = "<example tfBool=\"true\" ynBool=\"Y\" />";
@Attribute
public boolean tfBool;
@Attribute
@Convert(Example.YesNoBooleanConverter.class)
public boolean ynBool;
public static class YesNoBooleanConverter implements Converter<Boolean> {
public Boolean read(InputNode node) throws Exception {
return new Boolean(node.getValue().equals("Y"));
}
public void write(OutputNode node, Boolean value) throws Exception {
node.setValue(value.booleanValue()?"Y":"N");
}
}
public static void main(String[] args) throws Exception {
Serializer s = new Persister(new AnnotationStrategy());
Example e = s.read(Example.class, new StringReader(testXML));
System.out.println("tfBool = " + e.tfBool);
System.out.println("ynBool = " + e.ynBool);
}
}
This code does not work. The output is:
tfBool = true
ynBool = false
As far as I can tell, YesNoBooleanConverter is never used. I'm assuming that it's because the boolean primitive type already matches to an existing Transformer implementation, so there's no way to use @Convert to use a custom converter if it's already mapped by the PrimitiveMatcher.
I can't use my own custom Transform/Match setup in this case, because the implementation is inconsistent (and as far as I can tell, this also eliminates the RegistryStrategy as an option as well). If all boolean values were represented by "Y" and "N", then I could set up a Matcher for boolean and put together a Transform that converts between strings properly. I need to convert standard boolean string representations for the first attribute and custom boolean string representations for the second.
Is there any way to do this easily (without having to have a custom converter for the whole class, for example)?
Owen |
|
From: Paul T. <pt...@xe...> - 2011-06-27 14:26:41
|
I am trying to use Simple XML for handling RSS feeds and am currently annotating the classes used to represent the RSS channel and items. In the RSS feed I am trying to process one of the lines looks like this.. <atom:link href="http://feeds.bbci.co.uk/news/science_and_environment/rss.xml" rel="self" type="application/rss+xml"/> >From what I have read I need to declare the namespace for this to work as it also uses the link tag but I am not very clear on how to do this and the documentation didn't help. I have 3 classes, NewsFeed, Channel and AtomLink listed below with annotations. @Root @Namespace(reference="http://search.yahoo.com/mrss/") public class NewsFeed { @Attribute public String version; @Element @Namespace(reference="http://www.w3.org/2005/Atom", prefix="xmlns") public Channel channel; public Channel getChannel() { return channel; } } @Root @Namespace(reference="http://www.w3.org/2005/Atom") public class Channel { @Element public String title; @Element public String link; @Element public String description; @Element public String language; @Element public String lastBuildDate; @Element public String copyright; @Element public String ttl; @Element @Namespace(reference="http://www.w3.org/2005/Atom") public AtomLink atomLink; } @Root @Namespace(reference="http://www.w3.org/2005/Atom") public class AtomLink { @Element public String href; @Element public String rel; @Element public String type; } When I try this I get the error Element 'link' declared twice at line 13 If anyone can help with why this is happening I would be very grateful. Thanks Paul |
|
From: user <xx...@ra...> - 2011-06-27 10:06:31
|
Is there any updates? |
|
From: Nugroho S. <nug...@gm...> - 2011-06-26 10:29:22
|
Hi Niall,
Thank you. it works.
Regards,
Nugroho
On Sat, Jun 18, 2011 at 12:59 PM, Niall Gallagher <gal...@ya...
> wrote:
> To add a child element in the OutputNode use getChild(String), this should
> work for you. Also, check out the test cases. There are loads of examples.
>
> --- On *Mon, 13/6/11, Nugroho Saputro <nug...@gm...>* wrote:
>
>
> From: Nugroho Saputro <nug...@gm...>
> Subject: [Simple-support] Dynamic element name
> To: sim...@li...
> Received: Monday, 13 June, 2011, 9:58 PM
>
>
> Hi,
>
> Simple XML is a great framework, it support most of our requirements but I
> have a question regarding dynamic element name.
> I have some am working with XML that has dynamic element name. Please see
> the sample below, in this case custom fields are dynamic:
>
> <person>
> <name>Nugroho</name>
> .....
> <custom_fields>
> <birthdate>1976-12-26</birthdate>
> <opted-out>true</opted-out>
> </custom_fields>
> </person>
>
> It is possible to map custom fields in the above XML to a Map with element
> name as map key ?
>
> so the java class will be
>
> class Person {
> String name;
> Map<String, String> customFields;
> }
>
> I have tried creating Converted for it. I can convert
> from <custom_fields> InputNode to a Map but I am unable to convert Map to
> OutputNode in write() method, because there are no method I can use to add a
> child element in OutputNode.
>
> Thank you.
>
> Regards,
> Nugroho
>
>
> -----Inline Attachment Follows-----
>
>
> ------------------------------------------------------------------------------
> EditLive Enterprise is the world's most technically advanced content
> authoring tool. Experience the power of Track Changes, Inline Image
> Editing and ensure content is compliant with Accessibility Checking.
> http://p.sf.net/sfu/ephox-dev2dev
>
> -----Inline Attachment Follows-----
>
> _______________________________________________
> Simple-support mailing list
> Sim...@li...<http://mc/compose?to=...@li...>
> https://lists.sourceforge.net/lists/listinfo/simple-support
>
>
|
|
From: Stumpf, J. T. <Tho...@st...> - 2011-06-23 18:16:53
|
Hi!
I'm quite disappointed:
Simple XML framework forces the usage of superfluous CDATA-blocks.
On unserialising XML-Files with elements containing markup, e.g. HTML,
Simple XML framework transforms the HTML to pure text discarding all
mark up:
<description id="eid-191" composed="0">
<div>
<p>The quick brown <a href="www.example.com/fox">fox</a>.</p>
</div>
</description>
and
@Element
String description;
yields description to contain "The quick brown fox" but not
<div>
<p>The quick brown <a href="www.example.com/fox">fox</a>.</p>
</div>
This is crippling the provided data, undocumented and most probably
unnecessary.
The only way to get around it is to use CDATA blocks which is error
prone and evil:
Every additional CDATA-block bloats the XML files. Every CDATA block
renders substructures invalidateable. Every CDATA block makes the
generated *and* read data less suitable for true XML chains.
CDATA is *only* recommendable if the substructure is not valid itself.
The classes exactly define the parts which should be read into
attributes so there is no need to cripple the contained data.
It is a fairly common use case of XML based markup languages to contain
foreign other markup languages and it is not sensible to have to
implement the whole set of elements of these.
It is highly undesirable to be forced to try to sidestep existing
standards fixed in published RFCs.
Additionally obviously Simple XML framework shall not alter the content
of an element if there isn't a provided method (without a warning). To
alter the content somehow breaches the essential rules of object
orientation.
The mixed content of an element annotated as element of type string e.g.
can and should always simply be stored as String containing the markup
elements.
If the library is decent it might though integrate an optional switch to
do so or code to get rid of the putatively superfluous child nodes.
To preserve a reasonable XML chain and gain valid XML only such texts
should have to be contained in a CDATA block which contain not well
formed data. This is a sensible usage for CDATA.
The decision to insert the data as CDATA might even be deferred till
runtime.
Usage should be transparent and painless for the developer and an
alternative provider of XML data.
There has to be an option (annotation) to enabling an developer to keep
the markup without bloating the code at least.
As said before it's much more preferable to conserve per default
contents of String elements as provided.
For example I need the contained HTML mark up (see above) but I can't
alter the provider.
I'm looking forward for an answer - but according to my experiences
before I unfortunately think nobody will reply at all so this might just
end up as being a warning for other developers...
Jens
|
|
From: Owen M. <ow...@bi...> - 2011-06-20 16:45:49
|
Hi there...
I'm trying to parse some XML that is flattened and I want to construct an
object with sub-elements. Here's what I'm talking about. If the XML looks
like this:
<person name="Bob">
<address>
<line1>111 11th Street Unit 1A</line1>
<city>New York</city>
<state>NY</state>
<zip>12345</zip>
</address>
<phone>123 456 7890</phone>
</person>
Then I can use the following classes:
public class PersonInfo {
@Attribute
private String name;
@Element(required=false)
private Address address;
@Element(name="phone")
private String phoneNumber;
//implementation
}
But if the XML looks like this:
<person name="Bob">
<line1>111 11th Street Unit 1A</line1>
<city>New York</city>
<state>NY</state>
<zip>12345</zip>
<phone>123 456 7890</phone>
</person>
Then I would have to create a PersonInfo class with the address fields
(line1, etc.) as siblings of the phone field. For reasons required by my
project, I must be able to parse the above flattened XML but serialize and
deserialize the PersonInfo object as it's coded, with an Address object as a
field.
I've looked into using a Visitor to intercept those fields that should
belong to the Address object, but the parser is complaining. Here's the code
I have so far (these are members of the PersonInfo class; I realize it's an
incomplete implementation; just trying to get it to parse for starters).
public static void readFlat() throws Exception {
Serializer s = new Persister(new VisitorStrategy(newPersonVisitor()));
PersonInfo p = s.read(PersonInfo.class, new StringReader(PERSONFLAT
));
System.out.println(p);
}
private static class PersonVisitor implements Visitor {
private Address address;
public void read(Type type, NodeMap<InputNode> node) {
if (node.getName().equals("line1")) {
address = new Address();
node.put("test", "testvalue");
try {
address.setLine1(node.getNode().getValue());
} catch (Exception e) {
address.setLine1("Unable to set line 1 of address");
}
} else if (node.getName().equals("city")) {
try {
address.setCity(node.getNode().getValue());
} catch (Exception e) {
address.setCity("Unable to set city of address");
}
} else if (node.getName().equals("state")) {
try {
address.setState(node.getNode().getValue());
} catch (Exception e) {
address.setState("Unable to set state of address");
}
} else if (node.getName().equals("zip")) {
try {
address.setZip(node.getNode().getValue());
} catch (Exception e) {
address.setZip("Unable to set zip of address");
}
}
//System.out.println("Hi " + node.getName());
}
public void write(Type type, NodeMap<OutputNode> node) {
}
}
Here's what I get from the console as a result:
Exception in thread "main" org.simpleframework.xml.core.ElementException:
Element 'line1' does not have a match in class PersonInfo at line 1
at org.simpleframework.xml.core.Composite.readElement(Composite.java:580)
at org.simpleframework.xml.core.Composite.readElements(Composite.java:502)
at org.simpleframework.xml.core.Composite.readSection(Composite.java:388)
at org.simpleframework.xml.core.Composite.read(Composite.java:368)
at org.simpleframework.xml.core.Composite.readDefault(Composite.java:262)
at org.simpleframework.xml.core.Composite.read(Composite.java:232)
at org.simpleframework.xml.core.Composite.read(Composite.java:202)
at org.simpleframework.xml.core.Composite.read(Composite.java:150)
at org.simpleframework.xml.core.Traverser.read(Traverser.java:92)
at org.simpleframework.xml.core.Persister.read(Persister.java:632)
at org.simpleframework.xml.core.Persister.read(Persister.java:613)
at org.simpleframework.xml.core.Persister.read(Persister.java:591)
at org.simpleframework.xml.core.Persister.read(Persister.java:569)
at org.simpleframework.xml.core.Persister.read(Persister.java:469)
at PersonInfo.readFlat(PersonInfo.java:70)
at PersonInfo.main(PersonInfo.java:115)
I have a feeling I'm using the wrong strategy; that using a Visitor will not
work for my problem. I understand the message above: it's saying that if a
<line1> element exists in the XML, it has to be declared in the PersonInfo
class. I've only just gotten into SimpleXML and so I'm a little unsure of
how I'm going to get it to work properly.
Do you have an idea of how I can make this work?
|
|
From: Stumpf, J. T. <Tho...@st...> - 2011-06-18 09:13:50
|
Hi!
I tried a lot of different annotations following the tutorials but none of it was able to deserialize the following given structure (correctly):
<document>
...
<media>
<image id="media-1" src="img.png" name="Arthur Spooner" title="img one">my car</image>
<image id="media-2" src="img2.png" name="Arthur Spooner" title="img two">a better image of my car</image>
<image id="media-3" src="img3.png" name="Poison Ivy" title="img three">awesome flower</image>
<video id="video-1" src="mov1.mpg" name="Rohrschach" title="cool movie">man running in circles</video>
</media>
...
Image and Video class simply implement all attributes and Elements.
I tried, similar as shown in the tutorials, within class document
@ElementMap(inline=true,entry="image",key="id",attribute=true)
@Path("media")
Map<String, Image> images;
@ElementMap(inline=true,entry="video",key="id",attribute=true)
@Path("media")
Map<String, Video> videos;
Because this seemed to be problematic because of the mixed content I created an own class Media.
(Question: How could this issue be solved with @Union? The combination of Union and ElementMap annotation seems not to work)
Within Media class I tried
@ElementMap(inline=true,entry="image",key="id",attribute=true)
Map<String, Image> images;
...not further going to mention video, it's basically the same...
And I tried
@ElementMap(inline=true,key="id",attribute=true)
Map<String, Image> images;
and
@ElementMap(inline=true,value="image",key="id",attribute=true)
Map<String, Image> images;
as well as
@ElementMap(entry="image",key="id",attribute=true)
Map<String, Image> images;
and so on.
None of them worked as intended.
How can I generate a map of images, videos, you-name-it elements with the attribute id as key?
In fact I would like to get a sortable/navigable Map, e.g. TreeMap, and it would be good not to be forced to implement a Media class.
Can anybody help?
I most probably miss something.
For the time being I implemented it as Dictionary - the drawback is the name clash of the attributes and the methods.
I appreciate any input!
Thx in advance
Jens
|
|
From: Mikael G. <gr...@mi...> - 2011-06-18 06:35:37
|
It would be good to make it default to line.separator but also settable by the developer, since you might work on a Mac but want to make the XML editable by users on a PC (my current case). Cheers, Mikael Mikael Grev Systems Architect MiG InfoCom AB Hästkvarnsg 2 371 31 Karlskrona Sweden Ph +46 76 88 22 427 gr...@mi... www.miginfocom.com www.migcalendar.com This message is intended only for the use of the Addressee and may contain information that is PRIVILEGED and CONFIDENTIAL. If you are not the intended recipient, you are hereby notified that any dissemination of this communication is strictly prohibited. If you have received this communication in error, please erase all copies of the message and its attachments and notify us immediately. Thank you - MiG InfoCom Staff On Jun 18, 2011, at 5:07 AM, Niall Gallagher wrote: > Not at the moment, currently it will always use \n. I may later change to use System.getProperty("line.separator") > > --- On Thu, 16/6/11, Mikael Grev <gr...@mi...> wrote: > > From: Mikael Grev <gr...@mi...> > Subject: [Simple-support] Line separator > To: sim...@li... > Received: Thursday, 16 June, 2011, 12:44 AM > > Hello, > > Is it possible to change the line separator used when persisting XML? > > Cheers, > Mikael Grev > > > -----Inline Attachment Follows----- > > ------------------------------------------------------------------------------ > EditLive Enterprise is the world's most technically advanced content > authoring tool. Experience the power of Track Changes, Inline Image > Editing and ensure content is compliant with Accessibility Checking. > http://p.sf.net/sfu/ephox-dev2dev > > -----Inline Attachment Follows----- > > _______________________________________________ > Simple-support mailing list > Sim...@li... > https://lists.sourceforge.net/lists/listinfo/simple-support |
|
From: Niall G. <gal...@ya...> - 2011-06-18 03:10:10
|
Hi,
This can be serialized, but how would you deserialize it? Provide a setter method or a constructor to get this working. See the tutorial for reference.
Niall
--- On Thu, 16/6/11, simon mottram <sim...@ho...> wrote:
From: simon mottram <sim...@ho...>
Subject: [Simple-support] Exporting Java bean methods to XML
To: sim...@li...
Received: Thursday, 16 June, 2011, 9:16 PM
I'm sure I'm missing something terribly simple here but, while I can export fields with no trouble, methods always give me a constructor error.
For example a simple class
@Root
class MyClass {
@Element(name="name")
public String getName() {
return "Simon"
}
}
Always throws an Exception.
I am hoping to expose a bunch of classes as XML and they use getters for the most part, to export fields directly would be a right pain. This must be simple right?
Cheers
Simon
Simon Mottram
TEL
+64 (09) 575 4574
MOB
+64 (021) 243 7745
WEB
Linked In |
Soundcloud
-----Inline Attachment Follows-----
------------------------------------------------------------------------------
EditLive Enterprise is the world's most technically advanced content
authoring tool. Experience the power of Track Changes, Inline Image
Editing and ensure content is compliant with Accessibility Checking.
http://p.sf.net/sfu/ephox-dev2dev
-----Inline Attachment Follows-----
_______________________________________________
Simple-support mailing list
Sim...@li...
https://lists.sourceforge.net/lists/listinfo/simple-support
|
|
From: Niall G. <gal...@ya...> - 2011-06-18 03:07:53
|
Not at the moment, currently it will always use \n. I may later change to use System.getProperty("line.separator")
--- On Thu, 16/6/11, Mikael Grev <gr...@mi...> wrote:
From: Mikael Grev <gr...@mi...>
Subject: [Simple-support] Line separator
To: sim...@li...
Received: Thursday, 16 June, 2011, 12:44 AM
Hello,
Is it possible to change the line separator used when persisting XML?
Cheers,Mikael Grev
-----Inline Attachment Follows-----
------------------------------------------------------------------------------
EditLive Enterprise is the world's most technically advanced content
authoring tool. Experience the power of Track Changes, Inline Image
Editing and ensure content is compliant with Accessibility Checking.
http://p.sf.net/sfu/ephox-dev2dev
-----Inline Attachment Follows-----
_______________________________________________
Simple-support mailing list
Sim...@li...
https://lists.sourceforge.net/lists/listinfo/simple-support
|
|
From: Niall G. <gal...@ya...> - 2011-06-18 03:01:29
|
Hi,
Thanks for the contribution, I will take a close look at this and perhaps add it to a future release.
Thanks,
Niall
--- On Tue, 14/6/11, Raphael Jolivet <rap...@gm...> wrote:
From: Raphael Jolivet <rap...@gm...>
Subject: Re: [Simple-support] Support for almost-primitive types, Converters in attributes and Arrays in attributes
To: "Niall Gallagher" <gal...@ya...>
Cc: sim...@li...
Received: Tuesday, 14 June, 2011, 6:25 AM
2011/5/26 Niall Gallagher <gal...@ya...>
Hi,
Yes, it does work for attributes. Check out the test cases, in particular PrimitiveArrayTransformTest.
Niall
Sorry, you were right, it works for attributes. But it works only for arrays ('[]'), not for Lists or Collections.
However, there should not be any fundamental differences between those two types, they are still "a bunch of something".
This does not work :
@Attribute(name="group-by", required=false)
private List<Field> groupBy = new List<Field>();
Also, I have implemented a core Converter and integrated it in the framework so that all objects with a "Constructor(String value)" are serialized out of the box without the need of any further work, annotation, or strategy. I have integrated it only for parsing attributes at the moment; not for elements.
I hope this can be useful to someone.
Regards,Raphael
===== The converter ==================================================================
package org.simpleframework.xml.core;
import java.lang.reflect.Constructor;
import org.simpleframework.xml.stream.InputNode;import org.simpleframework.xml.stream.OutputNode;
/** * Converter for classes that have a constructor with a single String.
* It uses this constructor for new instances, and toString for serialiasing. */
public class StringConstructorConverter implements Converter {
// ----------------------------------------------------
// Static method // ----------------------------------------------------
static public boolean hasStringConstructor(Class<?> type) {
return (getStringConstructor(type) != null); }
// ----------------------------------------------------
// Attributes // ----------------------------------------------------
Class<?> type;
// ----------------------------------------------------
// Private methods // ----------------------------------------------------
static private Constructor<?> getStringConstructor(Class<?> type) {
try { return type.getConstructor(String.class);
} catch (NoSuchMethodException e) { return null;
} }
// ----------------------------------------------------
// Constructor // ----------------------------------------------------
public StringConstructorConverter(Class<?> type) {
this.type = type; }
/** Instantiate the object with the string constructor */
public Object read(InputNode node) throws Exception { return getStringConstructor(this.type).newInstance(node.getValue());
}
/** Use "toString" as the representation of this object */
public void write(OutputNode node, Object value) throws Exception {
node.setValue(value.toString()); }
/** This should not happen, ginore the existing value */
public Object read(InputNode node, Object value) throws Exception {
return this.read(node); }
/** Do not validate, we'll see ... */
public boolean validate(InputNode node) throws Exception { return true;
}
}
============ Integration into the Framework : AttributeLabel#getConverter ====================
public Converter getConverter(Context context) throws Exception {
String ignore = getEmpty(context); Type type = getContact();
if(!context.isPrimitive(type)) {
if (!StringConstructorConverter.hasStringConstructor(type.getType())) {
throw new AttributeException("Cannot use %s to represent %s", label, type);
} else { return new StringConstructorConverter(type.getType());
} }
return new Primitive(context, type, ignore); }
=================================================================================================
|
|
From: Niall G. <gal...@ya...> - 2011-06-18 02:59:16
|
To add a child element in the OutputNode use getChild(String), this should work for you. Also, check out the test cases. There are loads of examples.
--- On Mon, 13/6/11, Nugroho Saputro <nug...@gm...> wrote:
From: Nugroho Saputro <nug...@gm...>
Subject: [Simple-support] Dynamic element name
To: sim...@li...
Received: Monday, 13 June, 2011, 9:58 PM
Hi,
Simple XML is a great framework, it support most of our requirements but I have a question regarding dynamic element name.I have some am working with XML that has dynamic element name. Please see the sample below, in this case custom fields are dynamic:
<person> <name>Nugroho</name>.....<custom_fields> <birthdate>1976-12-26</birthdate> <opted-out>true</opted-out>
</custom_fields></person>
It is possible to map custom fields in the above XML to a Map with element name as map key ?
so the java class will be
class Person { String name; Map<String, String> customFields;}
I have tried creating Converted for it. I can convert from <custom_fields> InputNode to a Map but I am unable to convert Map to OutputNode in write() method, because there are no method I can use to add a child element in OutputNode.
Thank you.
Regards,Nugroho
-----Inline Attachment Follows-----
------------------------------------------------------------------------------
EditLive Enterprise is the world's most technically advanced content
authoring tool. Experience the power of Track Changes, Inline Image
Editing and ensure content is compliant with Accessibility Checking.
http://p.sf.net/sfu/ephox-dev2dev
-----Inline Attachment Follows-----
_______________________________________________
Simple-support mailing list
Sim...@li...
https://lists.sourceforge.net/lists/listinfo/simple-support
|
|
From: simon m. <sim...@ho...> - 2011-06-17 04:16:32
|
I'm sure I'm missing something terribly simple here but, while I can export fields with no trouble, methods always give me a constructor error.
For example a simple class
@Root
class MyClass {
@Element(name="name")
public String getName() {
return "Simon"
}
}
Always throws an Exception.
I am hoping to expose a bunch of classes as XML and they use getters for the most part, to export fields directly would be a right pain. This must be simple right?
Cheers
Simon
Simon Mottram
TEL
+64 (09) 575 4574
MOB
+64 (021) 243 7745
WEB
Linked In |
Soundcloud
|
|
From: Mikael G. <gr...@mi...> - 2011-06-16 08:04:27
|
Hello, Is it possible to change the line separator used when persisting XML? Cheers, Mikael Grev |
|
From: user <xx...@ra...> - 2011-06-15 07:31:02
|
You can try the following test. It faults:
package testsimplearraybug;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import junit.framework.Assert.*;
import org.junit.Test;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
@Root
public class ParamContainer {
private static final String FILENAME = "params.xml";
@ElementList
private List<ParamItem> items;
public ParamContainer() {
items = new ArrayList();
items.add(new ParamItem());
items.add(new ParamItem());
}
public void save() {
Serializer saver = new Persister();
File f = new File(FILENAME);
try {
saver.write(this, f);
} catch (Exception ex) {
Logger.getLogger(ParamContainer.class.getName()).log(Level.SEVERE,
null, ex);
}
}
public static ParamContainer load() {
Serializer reader = new Persister();
File f = new File(FILENAME);
try {
return reader.read(ParamContainer.class, f);
} catch (Exception ex) {
Logger.getLogger(ParamContainer.class.getName()).log(Level.SEVERE,
null, ex);
}
return null;
}
@Test
public static void checkIdentity() {
ParamContainer c1 = new ParamContainer();
c1.save();
ParamContainer c2 = ParamContainer.load();
org.junit.Assert.assertTrue(c1.items.size() == c2.items.size());
}
public static void main(String[] args) {
checkIdentity();
}
@Root
public static class ParamItem {
@Attribute
private int columnsCount = 0;
@Attribute
private int rowsCount = 0;
}
}
* Niall Gallagher <gal...@ya...> [Thu, 9 Jun 2011 04:35:40
-0700 (PDT)]:
> Hi,
>
> Can you write this as a unit test?
>
> Thanks,
> Niall
>
> --- On Tue, 7/6/11, user <xx...@ra...> wrote:
>
> > From: user <xx...@ra...>
> > Subject: [Simple-support] Bug when read XML with ArrayList and
HashMap
> created by simple
> > To: sim...@li...
> > Received: Tuesday, 7 June, 2011, 5:38 AM
> > There is a small class which creates
> > default configuration and saves it
> > into file:
> >
> > package videocontroldesk.params;
> >
> > import java.io.File;
> > import java.util.ArrayList;
> > import java.util.HashMap;
> > import java.util.List;
> > import java.util.Map;
> > import java.util.logging.Level;
> > import java.util.logging.Logger;
> > import org.simpleframework.xml.Element;
> > import org.simpleframework.xml.ElementList;
> > import org.simpleframework.xml.ElementMap;
> > import org.simpleframework.xml.Root;
> > import org.simpleframework.xml.Serializer;
> > import org.simpleframework.xml.core.Persister;
> >
> > @Root
> > public class VCDParams {
> >
> > private static VCDParams instance;
> > private static final String filename =
> > "VCDParams.xml";
> > private static final Logger logger =
> > Logger.getLogger(VCDParams.class.getName());
> >
> > @Element
> > private int selectedLayoutIdx;
> >
> > @ElementList
> > private List<LayoutParams> layouts;
> >
> > @ElementMap
> > private Map<String, String>
> > ipForCamera;
> >
> > private VCDParams() {
> > selectedLayoutIdx = 0;
> > layouts = new
> > ArrayList<LayoutParams>();
> > for (int i = 0; i < 4; ++i)
> > {
> > layouts.add(new
> > LayoutParams());
> > }
> > ipForCamera = new
> > HashMap<String, String>();
> > ipForCamera.put("БК1",
> > "192.168.0.101");
> > }
> >
> > public int getLayoutsCount() {
> > return layouts.size();
> > }
> >
> > public LayoutParams getLayout(int layoutIdx)
> > {
> > return layouts.get(layoutIdx);
> > }
> >
> > public static VCDParams getInstance() {
> > if (instance == null) {
> > try {
> >
> > instance = read();
> > }
> > catch (Exception
> > ex) {
> >
> > instance = new VCDParams();
> > }
> > }
> > return instance;
> > }
> >
> > private static VCDParams read() throws
> > Exception {
> > Serializer reader = new
> > Persister();
> > File f = new File(filename);
> > return
> > reader.read(VCDParams.class, f);
> > }
> >
> > public void save() {
> > Serializer saver = new
> > Persister();
> > File f = new File(filename);
> > try {
> > saver.write(this,
> > f);
> > } catch (Exception ex) {
> >
> > logger.log(Level.WARNING, "Не могу сохранить
> > файл {0}",
> > filename);
> > }
> > }
> >
> > public static void main(String[] args) {
> >
> > //VCDParams.getInstance().save();
> > VCDParams params =
> > VCDParams.getInstance();
> > //String[] cams =
> > VCDParams.getInstance().getLayout(0).getAllCamIds();
> > }
> >
> > }
> >
> >
> > If I uncomment "save()" method call I get the following
> > XML:
> >
> > <VCDParams>
> > <selectedLayoutIdx>0</selectedLayoutIdx>
> > <layouts
> > class="java.util.ArrayList">
> > <layoutParams columnsCount="2"
> > rowsCount="1">
> > <cell
> > cellId="0">БК1</cell>
> > </layoutParams>
> > <layoutParams columnsCount="2"
> > rowsCount="1">
> > <cell
> > cellId="0">БК1</cell>
> > </layoutParams>
> > <layoutParams columnsCount="2"
> > rowsCount="1">
> > <cell
> > cellId="0">БК1</cell>
> > </layoutParams>
> > <layoutParams columnsCount="2"
> > rowsCount="1">
> > <cell
> > cellId="0">БК1</cell>
> > </layoutParams>
> > </layouts>
> > <ipForCamera
> > class="java.util.HashMap">
> > <entry>
> >
> > <string>БК1</string>
> >
> > <string>192.168.0.101</string>
> > </entry>
> > </ipForCamera>
> > </VCDParams>
> >
> >
> > It looks OK. Then I comment "save()" and uncomment "
> > VCDParams.getInstance()" call.
> > When I set breakpoint right after this call I check the
> > value of
> > layouts.size(). It return 8 instead of 4.
> > Please fix it.
> > Tested on 2.5.3 and 2.4.1.
> >
> >
>
------------------------------------------------------------------------------
> > EditLive Enterprise is the world's most technically
> > advanced content
> > authoring tool. Experience the power of Track Changes,
> > Inline Image
> > Editing and ensure content is compliant with Accessibility
> > Checking.
> > http://p.sf.net/sfu/ephox-dev2dev
> > _______________________________________________
> > Simple-support mailing list
> > Sim...@li...
> > https://lists.sourceforge.net/lists/listinfo/simple-support
> >
|
|
From: Justin M. <jm...@me...> - 2011-06-14 14:58:24
|
I'm am using simple-xml to parse multiple feed sources. All have a very similar format but occasionally a feed provider will add a new node and that will break my parsing. Currently I just keep expanding my model to account for new nodes but this isn't sustainable. How can I tell simple xml to ignore nodes it doesn't recognize so that I stop getting "org.simpleframework.xml.core.ElementException: Element '?' does not have a match in class" errors? Any advice is appreciated. |
|
From: Raphael J. <rap...@gm...> - 2011-06-14 13:26:02
|
2011/5/26 Niall Gallagher <gal...@ya...>
> Hi,
>
> Yes, it does work for attributes. Check out the test cases, in particular
> PrimitiveArrayTransformTest.
>
> Niall
Sorry, you were right, it works for attributes.
But it works only for arrays ('[]'), not for Lists or Collections.
However, there should not be any fundamental differences between those two
types, they are still "a bunch of something".
This does not work :
@Attribute(name="group-by", required=false)
private List<Field> groupBy = new List<Field>();
Also, I have implemented a core Converter and integrated it in the framework
so that all objects with a "Constructor(String value)" are serialized out of
the box without the need of any further work, annotation, or strategy. I
have integrated it only for parsing attributes at the moment; not for
elements.
I hope this can be useful to someone.
Regards,
Raphael
===== The converter
==================================================================
package org.simpleframework.xml.core;
import java.lang.reflect.Constructor;
import org.simpleframework.xml.stream.InputNode;
import org.simpleframework.xml.stream.OutputNode;
/**
* Converter for classes that have a constructor with a single String.
* It uses this constructor for new instances, and toString for
serialiasing.
*/
public class StringConstructorConverter implements Converter {
// ----------------------------------------------------
// Static method
// ----------------------------------------------------
static public boolean hasStringConstructor(Class<?> type) {
return (getStringConstructor(type) != null);
}
// ----------------------------------------------------
// Attributes
// ----------------------------------------------------
Class<?> type;
// ----------------------------------------------------
// Private methods
// ----------------------------------------------------
static private Constructor<?> getStringConstructor(Class<?> type) {
try {
return type.getConstructor(String.class);
} catch (NoSuchMethodException e) {
return null;
}
}
// ----------------------------------------------------
// Constructor
// ----------------------------------------------------
public StringConstructorConverter(Class<?> type) {
this.type = type;
}
/** Instantiate the object with the string constructor */
public Object read(InputNode node) throws Exception {
return getStringConstructor(this.type).newInstance(node.getValue());
}
/** Use "toString" as the representation of this object */
public void write(OutputNode node, Object value) throws Exception {
node.setValue(value.toString());
}
/** This should not happen, ginore the existing value */
public Object read(InputNode node, Object value) throws Exception {
return this.read(node);
}
/** Do not validate, we'll see ... */
public boolean validate(InputNode node) throws Exception {
return true;
}
}
============ Integration into the Framework : AttributeLabel#getConverter
====================
public Converter getConverter(Context context) throws Exception {
String ignore = getEmpty(context);
Type type = getContact();
if(!context.isPrimitive(type)) {
if (!StringConstructorConverter.hasStringConstructor(type.getType()))
{
throw new AttributeException("Cannot use %s to represent %s", label,
type);
} else {
return new StringConstructorConverter(type.getType());
}
}
return new Primitive(context, type, ignore);
}
=================================================================================================
|