1

从我的 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 中完成吗?

4

1 回答 1

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);
    }

}

关系适配器

我们将使用 anXmlAdapterRelations对象转换为更好地映射到 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>

输入.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 -你的.html )

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
于 2012-07-18T18:28:54.477 回答