Tag: marshalling

如何正确序列化和反序列化CSV?

我一直在尝试将对象序列化为CSV String但该对象包含List而@JsonUnwrapped对List对象不起作用。 预期样本输出 : color,part.name\n red,gearbox\n red,door\n red,bumper 实际产量 : com.fasterxml.jackson.core.JsonGenerationException: Unrecognized column ‘name’: 这是我的代码 :(大部分是2 POJO) import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonPropertyOrder; import com.fasterxml.jackson.annotation.JsonRootName; import com.fasterxml.jackson.dataformat.csv.CsvMapper; import com.fasterxml.jackson.dataformat.csv.CsvSchema; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; import java.io.IOException; import static java.util.Arrays.asList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; public class NestedWrapping { @JsonRootName(“Car”) @JsonInclude(JsonInclude.Include.NON_DEFAULT) @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, […]

使用jaxb将java对象转换为xml,反之亦然(marshal和unmarshal)

我想有一个名为save()的方法,它应该将右侧面板中的计算机部件列表编组为XML文件。 反过来,另一个名为load()方法应该将保存的XML文件解组回一个对象。 基本上,“Save”事件将调用save()方法并将右侧面板中的部件列表保存到XML文件中。 “Load”事件应清除右侧面板,并调用load()方法。 调用load() ,它应在右侧面板中显示未编组的数据。 我得到“退出”工作。 我很难搞清楚“加载”和“保存”部分。 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class PCParts implements ActionListener{ JList destinationList, sourceList; JButton buttonin, buttonout; DefaultListModel source, destination; public JPanel createContentPane (){ JPanel totalGUI = new JPanel(); source = new DefaultListModel(); destination = new DefaultListModel(); String shoppingItems[] = {“Case”, “Motherboard”, “CPU”, “RAM”, “GPU”, “HDD”, “PSU”}; […]

使用XSD中的元素“anyAttribute”和“any”将XML编组为Java

我遇到了这个XSD和JAXB问题…请帮帮我… 我需要实现的是完全生成下面的xml。 …. …. 这是我需要从Java创建的完整xml文件的示例: SUBMISSION_REQUEST.xml 。 以下是我所做的总结: 从涉及的两个XSD文件生成Java类。 涉及的两个XSD文件是:envelope-v2-0-HMRC.xsd – 链接到文件:https:// drive.google.com/file/d/0Bwota60eLfeIN1duSGVhTE8xOWM/view?usp=sharing FullPaymentSubmission-2016-v1-2。 xsd – 链接到文件:https:// drive.google.com/file/d/0Bwota60eLfeIbUtMN1RaMmt0LWM/view?usp=sharing 创建了一个unit testing,根据SUBMISSION_REQUEST.xml中的值设置GovTalkMessage对象的值 (这是我遇到问题的地方)。 设置Body的值,我需要形成IRenvelope来完成xml,但我得到的是一个具有不完整setter的Body类(没有IRenvelope)。 主要的xml信封是GovTalkMessage,用于向英国政府的HMRC发送消息。 似乎GovTalkMessage信封已经可重复使用,它可以通过特别使用Body标签接受不同的消息。 我的困境是在Body标签内部我需要能够将来自FullPaymentSubmission-2016-v1-2.xsd的IRenvelope作为IRenvelope的XML Schema放置,并且能够在Java中设置与此对象关联的值,以便我可以在SUBMISSION_REQUEST.xml上看到创建整个xml GovTalkMessage的XSD是envelope-v2-0-HMRC.xsd。 您将看到匿名Body类型下的内容是anyAttribute和any 。 但我需要的是IRenvelope。 为了更清楚地解释,我已经为envelope-v2-0-HMRC.xsd生成了Java类,并给了我GovTalkMessage.java,它是我需要设置值的主要等效xml GovTalkMessage信封。 以下是我使用的xjc命令: xjc -p com.rti.rim2016.v1_2.envelope envelope-v2-0-HMRC.xsd xjc -p com.rti.rim2016.v1_2.fps FullPaymentSubmission-2016-v1-2.xsd 主要问题是GovTalkMessage “Body”元素使用## any和xsd:anyAttribute 为envelope-v2-0-HMRC.xsd生成了相应的Java类,并获得了GovTalkMessage.java的代码(我只提供了与“body”元素相关的代码段)。 显然,没有setter方法可以重定向地设置与IRenvelope相关的值,这样我就可以完成XML的构建。 @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = “”, propOrder = { “envelopeVersion”, “header”, […]

如何将JAXB类实例编组为其超类

是否可以将JAXB带注释的类实例编组为其超类 (也是一个JAXB注释类)? @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = “BenamningTYPE”, propOrder = {“benamningId”}) @XmlSeeAlso({MoreDetailedBenamningTYPE.class}) public class BenamningTYPE { … @XmlElement(name = “BenamningId”, required = true) protected IdentifierTYPE benamningId; … } 和扩展类型: @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = “MoreDetailedBenamningTYPE”, propOrder = {“modifyDetails”}) public class MoreDetailedBenamningTYPE extends BenamningTYPE { … @XmlElement(name = “ModifyDetails”, required = true) protected ModifyDetailsTYPE modifyDetails; … } 所以,如果这种情况: BenamningTYPE b […]

使用eclipselink.media-type值设置Marshaller属性时的PropertyException:application / json

我试图按照这里的示例,但得到一个javax.xml.bind.PropertyException。 由于以下代码行,我收到此exception: marshaller.setProperty(“eclipselink.media-type”, “application/json”); 我已经完全复制/粘贴了上面列出的示例,因此我的代码正是您在那里看到的。 搜索SO和谷歌对此没什么帮助,并且认为我会把这些带给SO的天才以获得一些帮助。 任何帮助都会受到最高的赞赏,(de)使用JSON和XML与json.org,Jackson和JAXB进行序列化已经变成了一个消耗了近一个月生命的黑色和无底洞。 我的第一印象是我没有正确指定eclipselink运行时(如此处所述),但没有产生解决方案。 堆栈跟踪: Exception in thread “main” javax.xml.bind.PropertyException: name: eclipselink.media-type value: application/json at org.eclipse.persistence.jaxb.JAXBMarshaller.setProperty(JAXBMarshaller.java:528) at com.dualoutput.DualOutput.main(DualOutput.java:20) SSCCE

JAXB封送由XmlAdapter创建的ArrayList

我想使用XmlAdapter调整HashMap字段的XML表示。 我使用ArrayList来做到这一点。 但是,当编组ArrayList根本没有编组。 这是为什么? 代码 @XmlRootElement public class Foo { private HashMap hashMap; public Foo() { this.hashMap = new HashMap(); } @XmlJavaTypeAdapter(HashMapAdapter.class) public HashMap getHashmap() { return hashMap; } public void setHashmap(HashMap hashMap) { this.hashMap = hashMap; } } public final class HashMapAdapter extends XmlAdapter<ArrayList, HashMap> { @Override public ArrayList marshal(HashMap arg0) throws Exception { […]

JAXB避免保存默认值

有没有办法让JAXB不保存哪些值是@Element注释中指定的默认值,然后在从XML加载null或空的元素时设置值? 一个例子: class Example { @XmlElement(defaultValue=”default1″) String prop1; } Example example = new Example(); example.setProp1(“default1”); jaxbMarshaller.marshal(example, aFile); 应该生成: 并在加载时 Example example = (Example) jaxbUnMarshaller.unmarshal(aFile); assertTrue(example.getProp1().equals(“default1”)); 我试图这样做,以生成一个干净的XML配置文件,并使其更好的可读性和更小的尺寸。 提前退位并表示感谢。

EclipseLink MOXy JSON序列化

我有一个示例课程: class Zoo { public Collection animals; } 当使用MOXy序列化时,我得到: { “bird”: [ { “name”: “bird-1”, “wingSpan”: “6 feets”, “preferredFood”: “food-1” } ], “cat”: [ { “name”: “cat-1”, “favoriteToy”: “toy-1” } ], “dog”: [ { “name”: “dog-1”, “breed”: “bread-1”, “leashColor”: “black” } ] } 为什么它使用数组指示符“[]”,而鸟,猫和狗不是数组? 第二,有没有办法摆脱“鸟”,“猫”和“狗”? 换句话说,我试图去: { { “name”: “bird-1”, “wingSpan”: “6 feets”, “preferredFood”: “food-1” […]

如何将JAXB对象封送到org.w3c.dom.Document?

这给了我一个Document对象,其顶级节点没有子节点: public static Document getDocument(Object jaxb) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); //dbf.setNamespaceAware(true); Document doc = dbf.newDocumentBuilder().newDocument(); JAXBContext context = JAXBContext.newInstance(jaxb.getClass()); context.createMarshaller().marshal(jaxb, doc); return doc; } 这是解决方法,它看起来效率更低,因为它转换为String然后转换为Document。 public static Document getDocument(Object jaxb) { StringWriter writer = new StringWriter(); JAXBContext context = JAXBContext.newInstance(jaxb.getClass()); context.createMarshaller().marshal(jaxb, writer); return DocumentBuilderFactory.newInstance().newDocumentBuilder(). parse(new InputSource(new StringReader(writer.toString())); } 是否有可能完成我想要完成的任务?

如何在使用JAXB进行编组时添加DOCTYPE和xml处理指令?

我正在将JAXB bean编组(序列化)为输出流。 如何添加DOCTYPE声明和xml处理指令以进行输出? 我正在做这样的编组: JAXBContext jaxbContext = JAXBContext.newInstance(“com.example.package”); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); SchemaFactory schemaFactory = SchemaFactory.newInstance(“http://www.w3.org/2001/XMLSchema”); Schema schema = schemaFactory.newSchema(schemaSource); marshaller.setSchema(schema); marshaller.marshal(object, output); 我想要输出看起来像这样: JAXB bean是生成的代码,所以我不想更改它们。 有一些hacks和无证技巧(请参阅使JAXB生成XML处理指令 )来添加xml处理指令和doctype 。 但是这样做的首选或正确方法是什么?