Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java XML converter to POJO  RSS feed

 
Sammy Ward
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, I have been stuck on a problem for a while. I've been having trouble to convert this http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml xml document to a POJO class since there are multiple 'Cube' tags. Can anyone here help me on how a valid pojo class would look like?


this is my setup so far, but it is obviously not working.
 
Stephan van Hulst
Saloon Keeper
Posts: 7927
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

  • The root isn't named Gesmes, but Envelope.
  • Envelope doesn't have a name, Sender has a name.
  • Envelope doesn't have a time, the inner Cube has a time.
  • Envelope doesn't have a list of Cube, it just has a single Cube.
  • Cube doesn't contain a Cube:currency element list, it contains a Cube element list of Cube elements.
  • You still need to set namespaces on your elements.
  •  
    Sammy Ward
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I created the following classes:



    But I now get this error: " org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.Attribute(empty=, name=name, required=true) on field 'name' private from class Sender"
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 7927
    143
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    name is not an attribute of Sender, but a child element.

    You also still need to specify the namespaces properly.
     
    Sammy Ward
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for the help so far, I'm not quite sure how to use namepaces. I have managed to convert everything except time to java classes.



    I'm not quite sure how to get the time value, would be really thankful if you could help me with that. Thanks.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 7927
    143
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well, as you can see, Envelope, subject, Sender and name use the gesmes prefix, while Cube, time, currency and rate don't use a prefix. In the document's root element, gesmes is declared as a short-hand for the "http://www.gesmes.org/xml/2002-08-01" namespace, and the default namespace is "http://www.ecb.int/vocabulary/2002-08-01/eurofxref".

    You still need to properly annotate your classes and members with these namespaces:

    Now, when you say you can't convert time, what is happening?
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!