Simple Java Xml to POJO mapping/binding?

后端 未结 4 1853
一向
一向 2020-12-10 05:41

I\'m trying to figure out the simplest way to map an xml file to to a plain old java object.

Note: That in my example the xml doesn\'t quite match up with my intend

相关标签:
4条回答
  • 2020-12-10 05:47

    I consider JiBX the best of the bunch (JAXB, Castor, XMLBeans, etc.), particularly because I favor mapping files over annotations. Admittedly it has a decent learning curve, but the website has a lot of good examples. You must have missed the tutorial.

    If you are only going one way (XML --> POJO) you could use Digester.

    Side comment: I prefer mapping files over annotations because annotations:

    • clutter the code (especially when using annotations from several products)
    • mix concerns (XML, database, etc. in domain layer)
    • can only bind to a single XML (or database, or web service, etc.) representation
    0 讨论(0)
  • 2020-12-10 05:51

    This article may help you... it only requires you to know xpath http://onjava.com/onjava/2007/09/07/schema-less-java-xml-data-binding-with-vtd-xml.html

    0 讨论(0)
  • 2020-12-10 05:56

    EclipseLink JAXB (MOXy) allows you to do the path based mapping that you are looking for:

    @XmlRootElement 
    class Animal 
    { 
     @XmlPath("standardName/Name/text()")
     private String name; 
    
     @XmlPath("standardVersion/VersionIdentifier/text()");
     private String versionIdentifier; 
    } 
    

    For more information see:

    • http://bdoughan.blogspot.com/2010/09/xpath-based-mapping-geocode-example.html
    • http://bdoughan.blogspot.com/2010/07/xpath-based-mapping.html
    • http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted/MOXyExtensions

    EclipseLink also allows the metadata to be specified using an external configuration file:

    • http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted/ExternalizedMetadata
    0 讨论(0)
  • 2020-12-10 06:05

    Jakarta Commons Digester should do what you want.

    Alternatively, I would recommend writing a transformation class that uses XPath to retrieve elements from the XML.

    0 讨论(0)
提交回复
热议问题