从我的JAXB模型中我可以输出这个xml
<metadata xmlns="http://musicbrainz.org/ns/mmd-2.0#" xmlns:ext="http://musicbrainz.org/ns/ext#-2.0">
<work-list>
<work id="4ff89cf0-86af-11de-90ed-001fc6f176ff">
<relation-list target-type="artist">
<relation type="composer">
<direction>backward</direction>
</relation>
</relation-list>
</work>
</work-list>
</metadata>
目前使用MOXy和oxml.xml我可以输出以下JSON
{
"work" : [ {
"relations": [ {
"target-type" : "artist",
"relation" : [ {
"type" : "composer",
"direction" : "backward",
},
} ]
} ]
} ]
}
(在我的oxml.xml中,我将工作列表和关系列表对象展平,并重命名为关系关系。)
但实际要求是一个更复杂的转换,我不知道如何用正确的术语解释它,但这里是所需输出的一个例子。
{
"work" : [ {
"relations": {
"artist": [{
"direction": "backward",
"type": "composer",
}
],
}
}]
}
可以在eclipselink MOXy中完成吗?
答案 0 :(得分:0)
以下是如何做到这一点的示例:
<强>注册表强>
对于五个不同元素值中的每一个,您需要一个带@XmlRegistry
注释的类,其中的方法标有@XmlElementDecl
。
package forum11537931;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
import javax.xml.namespace.QName;
@XmlRegistry
public class Registry {
private static final String ARTIST = "artist";
private static final String FOO = "foo";
@XmlElementDecl(name=ARTIST)
public JAXBElement<Relation> createArtist(Relation relation) {
return new JAXBElement<Relation>(new QName(ARTIST), Relation.class, relation);
}
@XmlElementDecl(name=FOO, substitutionHeadName=ARTIST)
public JAXBElement<Relation> createFoo(Relation relation) {
return new JAXBElement<Relation>(new QName(FOO), Relation.class, relation);
}
}
<强> RelationsAdapter 强>
我们将使用XmlAdapter
将Relations
对象转换为更好地映射到JSON表示的对象。我们将利用@XmlElementRef
注释来执行此操作(请参阅http://blog.bdoughan.com/2010/12/represent-string-values-as-element.html)。
package forum11537931;
import java.util.*;
import javax.xml.bind.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.namespace.QName;
public class RelationsAdapter extends XmlAdapter<RelationsAdapter.AdaptedRelations, Relations> {
@Override
public Relations unmarshal(AdaptedRelations v) throws Exception {
// TODO Auto-generated method stub
return null;
}
@Override
public AdaptedRelations marshal(Relations relations) throws Exception {
AdaptedRelations adaptedRelations = new AdaptedRelations();
for(Relation relation : relations.relations) {
adaptedRelations.relations.add(new JAXBElement<Relation>(new QName(relations.targetType), Relation.class, relation));
}
return adaptedRelations;
}
@XmlSeeAlso({Registry.class})
public static class AdaptedRelations {
@XmlElementRef(type=JAXBElement.class, name="artist")
public List<JAXBElement<Relation>> relations = new ArrayList<JAXBElement<Relation>>();
}
}
<强> oxm.xml 强>
由于您不希望XmlAdapter
应用于XML表示,我们将使用MOXy的外部映射文档指定它(请参阅http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html)。
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="forum11537931">
<java-types>
<java-type name="Work">
<java-attributes>
<xml-element java-attribute="relations">
<xml-java-type-adapter value="forum11537931.RelationsAdapter"/>
</xml-element>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
<强> input.xml中强>
出于本示例的目的,我简化了您的XML文档。
<?xml version="1.0" encoding="UTF-8"?>
<work id="4ff89cf0-86af-11de-90ed-001fc6f176ff">
<relation-list target-type="artist">
<relation type="composer">
<direction>backward</direction>
</relation>
</relation-list>
</work>
<强>演示强>
package forum11537931;
import java.io.File;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
public class Demo {
public static void main(String[] args) throws Exception {
// XML
JAXBContext jc = JAXBContext.newInstance(Work.class, Registry.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum11537931/input.xml");
Work work = (Work) unmarshaller.unmarshal(xml);
// JSON
Map<String, Object> properties = new HashMap<String, Object>(2);
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum11537931/oxm.xml");
properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
JAXBContext jsonJC = JAXBContext.newInstance(new Class[] {Work.class, Registry.class}, properties);
Marshaller jsonMarshaller = jsonJC.createMarshaller();
jsonMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jsonMarshaller.marshal(work, System.out);
}
}
<强>输出强>
{
"work" : {
"relations" : [ {
"artist" : [ {
"type" : "composer",
"direction" : "backward"
} ]
} ]
}
}
域名模型
<强>工作强>
package forum11537931;
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Work {
@XmlElement(name="relation-list")
List<Relations> relations;
}
<强>关系强>
package forum11537931;
import java.util.*;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class Relations {
@XmlAttribute(name="target-type")
String targetType;
@XmlElement(name="relation")
List<Relation> relations = new ArrayList<Relation>();
}
<强>关系强>
package forum11537931;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class Relation {
@XmlAttribute
String type;
String direction;
}
的 jaxb.properties 强>
要将MOXy指定为JAXB提供程序,您需要在与域模型相同的包中包含名为jaxb.properties
的文件,并带有以下条目(请参阅:http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html)
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory