java xml 静态对象,Java的XML unmarshlling为对象,它是动态

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 Entities which properties are dynamic, or changed from time to time, so want to have something like automatic way of working with entities, without converting Object into predefine Entity object. Does something like that exists?

Workflow would be like this read from XML create class for each Entity with dynamic set of attributes and/or create ORM mapping part for those entities and then all manipulation retrieve/store into db or probably will going to use some NoSQL solution like MongoDB.

解决方案

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:

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.

xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"

package-name="blog.metadatasource.refresh">

java-attribute="middleName"

name="middle-name"

type="java.lang.String"/>

java-attribute="shippingAddress"

name="shipping-address"

type="blog.metadatasource.multiple.Address"/>

For More Information

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值