Java unmarshlling XML to Object which are dynamic

后端 未结 2 1315
死守一世寂寞
死守一世寂寞 2021-01-13 12:55

I\'m looking for best tool/way to create and load JAVA objects from XML definitions. I had checked out JAXB, seems pretty nice, but didn\'t find is there a way to work with

2条回答
  •  没有蜡笔的小新
    2021-01-13 13:32

    Note: I'm the EclipseLink JAXB (MOXy) lead, and a member of the JAXB 2 (JSR-222) expert group.


    Check out the following EclipseLink example. It demonstrates how to use dynamic properties with both the JPA and JAXB implementations:

    • http://wiki.eclipse.org/EclipseLink/Examples/MySports

    Option #1 - Static Objects with Dynamic Properties

    MOXy has an @XmlVirtualAccessMethods extension which allows you to map entries in a map to XML. This allows you to add properties to a static class. In the example below the Customer class has a "real" name property and may have many "virtual" properties.

    package blog.metadatasource.refresh;
    
    import java.util.*;
    import javax.xml.bind.annotation.*;
    import org.eclipse.persistence.oxm.annotations.XmlVirtualAccessMethods;
    
    @XmlRootElement
    @XmlType(propOrder={"firstName", "lastName", "address"})
    @XmlVirtualAccessMethods
    public class Customer {
    
        private String name;
        private Map extensions = new HashMap();
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Object get(String key) {
            return extensions.get(key);
        }
    
        public void set(String key, Object value) {
            extensions.put(key, value);
        }
    
    }
    

    The virtual properties are defined via MOXy's XML metadata. In the example below we will add two properties: middleName and shippingAddress.

    
    
        
            
                
                    
                    
                
            
        
    
    

    For More Information

    • http://blog.bdoughan.com/2011/06/extensible-models-with-eclipselink-jaxb.html
    • http://blog.bdoughan.com/2011/06/moxy-extensible-models-multi-tenant.html
    • http://blog.bdoughan.com/2011/06/moxy-extensible-models-multiple.html
    • http://blog.bdoughan.com/2011/06/moxy-extensible-models-refresh-example.html

    Option #2 - Dynamic Objects

    MOXy also offers full dynamic object models:

    DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromXSD(xsdInputStream, null, null, null);
    
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    DynamicEntity customer = (DynamicEntity) unmarshaller.unmarshal(inputStream);
    
    DynamicEntity address = jaxbContext.newDynamicEntity("org.example.Address");
    address.set(street, "123 A Street");
    address.set(city, "Any Town");
    customer.set("address", address);
    
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.marshal(customer, System.out);
    

    For More Information

    • http://wiki.eclipse.org/EclipseLink/UserGuide/MOXy
    • http://wiki.eclipse.org/EclipseLink/Examples/MOXy/Dynamic

提交回复
热议问题