How to deserialize Java objects from XML?

后端 未结 5 741
暗喜
暗喜 2021-01-16 08:01

I\'m sure this might have been discussed at length or answered before, however I need a bit more information on the best approach for my situation...

Problem

相关标签:
5条回答
  • 2021-01-16 08:26

    JAXB, the Java API for XML Binding might be what you want. You use it to inflate an XML document into a Java object graph made up of "Java content objects". These content objects are instances of classes generated by JAXB to match the XML document's schema

    But if you already have a set of Java classes, or don't yet have a schema for the document, JAXB probably isn't the best way to go. I'd suggest doing a SAX parse and then building up your Java objects during the parse. Alternatively you could try a DOM parse and then walk the resulting Document tree to pull out the parts of interest (maybe with XPath) -- but 5MB of XML might turn into 50MB of DOM tree objects in Java.

    0 讨论(0)
  • 2021-01-16 08:32

    Use XSLT to transform the large XML files into a local domain model that is mapped to java objets with JAXB.

    Start with the JDK 5+ built in XML libraries (unless you absolutely need XSLT 2.0, in which case use Saxon)

    Don't focus on relative performance of SAX/DOM, focus on learning how to write XPath expressions and use XSLT, and then worry about performance later if and only if you find it to be a problem.

    The Eclipse XML editors are decent, but if you can afford it, spring for Oxygen XML, which will let you do XPath evaluation in realtime.

    0 讨论(0)
  • 2021-01-16 08:48

    DOM, SAX and XSLT are different animals.

    DOM parsing loads the entire document into memory, which for 100K to 5MB (very small by today's standards) would work.

    SAX is a stream parser which reads the XML and delivers events to your code for each tag.

    XSLT is a system for transforming one XML tree into another. Even if you wrote a transform that converts the input to a more suitable format, you'd still have to write something using DOM or SAX to convert it into Java objects.

    0 讨论(0)
  • 2021-01-16 08:48

    You can use the @XmlPath extension in EclipseLink JAXB (MOXy) to easily handle this use case. For a detailed example see:

    • http://bdoughan.blogspot.com/2010/09/xpath-based-mapping-geocode-example.html

    Sample Code:

    package blog.geocode;
    
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.XmlType;
    
    import org.eclipse.persistence.oxm.annotations.XmlPath;
    
    @XmlRootElement(name="kml")
    @XmlType(propOrder={"country", "state", "city", "street", "postalCode"})
    public class Address {
    
        @XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:SubAdministrativeArea/ns:Locality/ns:Thoroughfare/ns:ThoroughfareName/text()")
        private String street;
    
        @XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:SubAdministrativeArea/ns:Locality/ns:LocalityName/text()")
        private String city;
    
        @XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:AdministrativeAreaName/text()")
        private String state;
    
        @XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:CountryNameCode/text()")
        private String country;
    
        @XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:SubAdministrativeArea/ns:Locality/ns:PostalCode/ns:PostalCodeNumber/text()")
        private String postalCode;
    
    }
    
    0 讨论(0)
  • 2021-01-16 08:50

    We had a similar situation and I just threw together some XPath code that parsed the stuff I needed.

    It was amazingly quick even on 100k+ XML files. We went as low tech as possible. We handle around 1000 files a day of that size and parsing time is very low. We have no memory issues, leaks etc.

    We wrote a quick prototype in Groovy (if my memory is accurate) - proof of concept took me about 10 minutes

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