gpt4 book ai didi

java - 在 MOXy 中,如果元素包含子元素和属性,则无法合并元素

转载 作者:行者123 更新时间:2023-11-30 09:33:12 25 4
gpt4 key购买 nike

这是我的 Xml 输出

<worklist>
<work>
<relation-list target-type="artist">
<relation type="composer">
<name>Пётр Ильич Чайковский</name>
<sort-name>Пётр Ильич Чайковский</sort-name>
</relation>
<relation type="writer">
<name>Frank Turner/name>
<sort-name>Turner Frank</sort-name>
</relation>
</relation-list>
</work>
</worklist>

我正在尝试使用 EclipseLInk Moxy 来获取此 json 输出

{
"work" : [ {
"relations" : [ {
"type" : "composer",
"name" : "Пётр Ильич Чайковский",
"sort-name" : "Пётр Ильич Чайковский"
}, {
"type" : "writer",
"name" : "frank turner",
"sort-name" : "turner, frank"
} ]
} ]
}

但我能做的最好的就是

{
"work" : [ {
"relationList" : [ {
"relations" : [ {
"type" : "composer",
"name" : "Пётр Ильич Чайковский",
"sort-name" : "Пётр Ильич Чайковский"
}, {
"type" : "writer",
"name" : "frank turner",
"sort-name" : "turner, frank"

} ]
} ],
} ]
}

请注意,我已经删除了 relationList 的 target-type 属性,并且我已经将 relation 重命名为 relations 但是如果我尝试使用

<java-type name="Work">
<java-attributes>
<xml-element java-attribute="relationList" xml-path="."/>
</java-attributes>
</java-type>

我得到以下异常

Caused by: java.lang.NullPointerException
at java.io.Writer.write(Writer.java:140)
at org.eclipse.persistence.oxm.record.JSONWriterRecord.writeKey(JSONWriterRecord.java:580)
at org.eclipse.persistence.oxm.record.JSONFormattedWriterRecord.openStartElement(JSONFormattedWriterRecord.java:147)
at org.eclipse.persistence.internal.oxm.XPathNode.startElement(XPathNode.java:359)
at org.eclipse.persistence.internal.oxm.XMLCompositeCollectionMappingNodeValue.marshalSingleValue(XMLCompositeCollectionMappingNodeValue.java:292)
at org.eclipse.persistence.internal.oxm.XMLCompositeCollectionMappingNodeValue.marshal(XMLCompositeCollectionMappingNodeValue.java:91)
at org.eclipse.persistence.internal.oxm.NodeValue.marshal(NodeValue.java:151)
at org.eclipse.persistence.internal.oxm.NodeValue.marshal(NodeValue.java:104)
at org.eclipse.persistence.internal.oxm.record.ObjectMarshalContext.marshal(ObjectMarshalContext.java:60)
at org.eclipse.persistence.internal.oxm.XPathNode.marshal(XPathNode.java:350)
at org.eclipse.persistence.internal.oxm.TreeObjectBuilder.buildRow(TreeObjectBuilder.java:467)
at org.eclipse.persistence.internal.oxm.XMLCompositeCollectionMappingNodeValue.marshalSingleValue(XMLCompositeCollectionMappingNodeValue.java:299)
at org.eclipse.persistence.internal.oxm.XMLCompositeCollectionMappingNodeValue.marshal(XMLCompositeCollectionMappingNodeValue.java:91)
at org.eclipse.persistence.internal.oxm.NodeValue.marshal(NodeValue.java:151)
at org.eclipse.persistence.internal.oxm.NodeValue.marshal(NodeValue.java:104)
at org.eclipse.persistence.internal.oxm.record.ObjectMarshalContext.marshal(ObjectMarshalContext.java:60)
at org.eclipse.persistence.internal.oxm.XPathNode.marshal(XPathNode.java:350)
at org.eclipse.persistence.internal.oxm.TreeObjectBuilder.buildRow(TreeObjectBuilder.java:467)
at org.eclipse.persistence.internal.oxm.XMLCompositeObjectMappingNodeValue.marshalSingleValue(XMLCompositeObjectMappingNodeValue.java:224)
at org.eclipse.persistence.internal.oxm.XMLCompositeObjectMappingNodeValue.marshal(XMLCompositeObjectMappingNodeValue.java:144)
at org.eclipse.persistence.internal.oxm.NodeValue.marshal(NodeValue.java:104)
at org.eclipse.persistence.internal.oxm.record.ObjectMarshalContext.marshal(ObjectMarshalContext.java:60)
at org.eclipse.persistence.internal.oxm.XPathNode.marshal(XPathNode.java:350)
at org.eclipse.persistence.internal.oxm.TreeObjectBuilder.buildRow(TreeObjectBuilder.java:467)
at org.eclipse.persistence.oxm.XMLMarshaller.marshal(XMLMarshaller.java:1074)
at org.eclipse.persistence.oxm.XMLMarshaller.marshal(XMLMarshaller.java:689)
at org.eclipse.persistence.oxm.XMLMarshaller.marshal(XMLMarshaller.java:602)
at org.eclipse.persistence.jaxb.JAXBMarshaller.marshal(JAXBMarshaller.java:584)

我想我明白了这个问题。因为 Relation-List 元素同时包含关系元素和 target-type 属性,所以直接转换为 json 需要 relation-list 元素封装这两个元素,即我认为如果 Relation-List 元素没有 target-type 属性在模型中它可以正常工作。

但无论如何我都不想输出目标类型,所以它应该可以工作,有没有办法解决这个问题?

编辑:感谢您的回答,但它似乎与我正在做的相同,除了我已经尝试过的示例代码可以正常工作。我尝试向示例代码中添加更多元素以尝试破解它,但没有成功。

我最初的问题显示了一个比现实更简单的案例,所以下面我实际上输出了完整的输出,以防有人看到可能导致问题的原因

{
"count" : 1,
"offset" : 0,
"work" : [ {
"id" : "4ff89cf0-86af-11de-90ed-001fc6f176ff",
"type" : "Opera",
"score" : "100",
"title" : "Symphony No. 5",
"language" : "eng",
"iswcs" : [ "T-101779304-1", "B-101779304-1" ],
"disambiguation" : "demo",
"aliases" : [ "Symp5" ],
"relation-list" : [ {
"relations" : [ {
"type" : "composer",
"direction" : "backward",
"attributes" : [ "additional" ],
"artist" : {
"id" : "1f9df192-a621-4f54-8850-2c5373b7eac9",
"name" : "Пётр Ильич Чайковский",
"sort-name" : "Пётр Ильич Чайковский"
}
}, {
"type" : "writer",
"direction" : "backward",
"artist" : {
"id" : "abcdefgh-a621-4f54-8850-2c5373b7eac9",
"name" : "frank",
"sort-name" : "turner"
}
} ]
} ],
"tags" : [ {
"count" : 10,
"name" : "classical"
} ]
} ]
}

工作正常但添加

<java-type name="Work">
<java-attributes>
<xml-element java-attribute="relationList" xml-path="."/>
</java-attributes>
</java-type>

导致堆栈跟踪。

也许堆栈跟踪包含解决方案

更新

完整的项目可以在线获取

数据模型在http://svn.musicbrainz.org/mmd-schema/trunk/brainz-mmd2-jaxb/下载并运行 mvn install

那么使用这个的项目在http://svn.musicbrainz.org/search_server/trunk/下载并运行 mvn 包,

这包含三个子项目,问题之一是 servlet 项目,复制问题:

CD 小服务程序

编辑 src/main/resources/oxml.xml 更改

<xml-element java-attribute="relationList" name="relationList"/>      

<xml-element java-attribute="relationList" xml-path="."/>

mvn包

src/test/java/org/musicbrainz/search/servlet/FindWorkTest 现在将失败testOutputAsJsonNew() 和 testOutputAsJsonNewPretty()

进一步更新Blaise 解决了这个例子没有显示(我不知道)你实际上可以有多个 relationList 元素的问题,即 Xml 可以是

<worklist>
<work>
<relation-list target-type="artist">
<relation type="composer">
<name>Jane Doe</name>
<sort-name>Doe Jane</sort-name>
</relation>
<relation type="writer">
<name>Frank Turner</name>
<sort-name>Turner Frank</sort-name>
</relation>
</relation-list>
<relation-list target-type="release">
<relation type="cover">
<name>Hey Jude</name>
<sort-name>Hey Jude</sort-name>
</relation>
</relation-list>
</work>
</worklist>

我想要输出的是

{
"work" : [ {
"relations" : [ {
"type" : "composer",
"name" : "Jane Doe",
"sort-name" : "Doe Jane"
}, {
"type" : "writer",
"name" : "Frank Turner",
"sort-name" : "Turner Frank"
}, {
"type" : "cover",
"name" : "Hey Jude",
"sort-name" : "Hey Jude"
} ]
} ]
}

尽我所能

{
"work" : [ {
"relationLists" : [ {
"relations" : [ {
"type" : "composer",
"name" : "Jane Doe",
"sort-name" : "Doe Jane"
}, {
"type" : "writer",
"name" : "Frank Turner",
"sort-name" : "Turner Frank"
} ]
}, {
"relations" : [ {
"type" : "cover",
"name" : "Hey Jude",
"sort-name" : "Hey Jude"
} ]
} ]
} ]
}

所以本质上我想合并所有关系,使它们在一个列表中并丢失关系列表标签。我们合并了不同的关系列表并丢弃了标识每个关系列表的目标类型属性,但我们不需要能够标识这些子列表,这似乎很奇怪。

这有可能吗?

最佳答案

更新 #2

根据调查结果,我更新了示例代码以使用 XmlAdapter 而不是 . XPath。这种方法将更适合您的用例。


更新 #1

从堆栈跟踪来看,您似乎正在为不允许的集合属性设置 self (".") XPath(我们将更改代码以便抛出更好的异常)。

Work 类中,您有一个名为 relationList 的集合属性

@XmlElement(name = "relation-list")
protected List<RelationList> relationList;

出现问题是因为在元数据文件中您试图将 xml-path 设置为 '.'

<xml-element java-attribute="relationList" xml-path="."/>

相反,您需要在引用包含集合的对象的属性上使用 '.' 路径。看下面的例子:


XML 适配器

不使用 . XPath,您的用例将由 XmlAdapter 更好地处理。 XmlAdapter 允许我们将一个对象结构转换为另一个更好地映射到所需输入/输出的对象结构。对于这个用例,XmlAdapter 看起来像这样。

package forum12338288;

import java.util.*;
import javax.xml.bind.annotation.adapters.XmlAdapter;

public class WorkAdapter extends XmlAdapter<WorkAdapter.AdaptedWork, Work> {

public static class AdaptedWork {
public List<Relation> relations = new ArrayList<Relation>();
}

@Override
public AdaptedWork marshal(Work work) throws Exception {
AdaptedWork adaptedWork = new AdaptedWork();
for(RelationList relationList : work.relationList) {
for(Relation relation : relationList.relation) {
adaptedWork.relations.add(relation);
}
}
return adaptedWork;
}

@Override
public Work unmarshal(AdaptedWork adaptedWork) throws Exception {
Work work = new Work();
RelationList relationList = new RelationList();
for(Relation relation : adaptedWork.relations) {
relationList.relation.add(relation);
}
work.relationList.add(relationList);
return work;
}

}

映射文档

您的映射文档应如下所示:

oxm.xml

<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="forum12338288">
<java-types>
<java-type name="WorkList">
<java-attributes>
<xml-element java-attribute="work">
<xml-java-type-adapter value="forum12338288.WorkAdapter"/>
</xml-element>
</java-attributes>
</java-type>
<java-type name="RelationList">
<java-attributes>
<xml-transient java-attribute="targetType"/>
<xml-element java-attribute="relation" name="relations"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>

演示代码

下面的演示代码演示了如何利用映射文档读取 XML 并输出所需的 JSON。

演示

package forum12338288;

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(WorkList.class);

Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum12338288/input.xml");
WorkList workList = (WorkList) unmarshaller.unmarshal(xml);

// JSON
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum12338288/oxm.xml");
properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
JAXBContext jsonJC = JAXBContext.newInstance(new Class[] {WorkList.class}, properties);

Marshaller marshaller = jsonJC.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(workList, System.out);
}

}

input.xml

<worklist>
<work>
<relation-list target-type="artist">
<relation type="composer">
<name>Jane Doe</name>
<sort-name>Doe Jane</sort-name>
</relation>
<relation type="writer">
<name>Frank Turner</name>
<sort-name>Turner Frank</sort-name>
</relation>
</relation-list>
<relation-list target-type="release">
<relation type="cover">
<name>Hey Jude</name>
<sort-name>Hey Jude</sort-name>
</relation>
</relation-list>
</work>
</worklist>

输出

{
"work" : [ {
"relations" : [ {
"type" : "composer",
"name" : "Jane Doe",
"sort-name" : "Doe Jane"
}, {
"type" : "writer",
"name" : "Frank Turner",
"sort-name" : "Turner Frank"
}, {
"type" : "cover",
"name" : "Hey Jude",
"sort-name" : "Hey Jude"
} ]
} ]
}

Java 模型

下面是我用来确保一切正常的 Java 模型。

工作列表

package forum12338288;

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlRootElement(name="worklist")
@XmlAccessorType(XmlAccessType.FIELD)
public class WorkList {

List<Work> work;

}

工作

package forum12338288;

import java.util.*;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Work {

@XmlElement(name="relation-list")
List<RelationList> relationList = new ArrayList<RelationList>();

}

关系列表

package forum12338288;

import java.util.List;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class RelationList {

@XmlAttribute(name="target-type")
String targetType;

List<Relation> relation;

}

关系

package forum12338288;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Relation {

@XmlAttribute
String type;

String name;

@XmlElement(name="sort-name")
String sortName;

}

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

关于java - 在 MOXy 中,如果元素包含子元素和属性,则无法合并元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12338288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com