如何使用错误的父/子模型解组xml消息

编程入门 行业动态 更新时间:2024-10-23 13:29:04
本文介绍了如何使用错误的父/子模型解组xml消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试将第三方XML有效负载解组到一个类中。问题是有效负载具有父/子关系,根节点,父节点和子节点都具有相同的元素名称。以下是有效负载的示例。

I am trying to unmarshal a 3rd party XML payload into a class. The problem is that the payload has a parent/child relationship and the root node, the parent and the children all have the same element name. Here is a sample of the payload.

<?xml version="1.0" encoding="UTF-8"?> <Directory> <id>2</id> <name>Media</name> <Directory> <id>5</id> <name>Default_Content</name> <Directory> <id>9</id> <name>Images</name> </Directory> <Directory> <id>8</id> <name>Icons</name> </Directory> <Directory> <id>6</id> <name>Additional_Content</name> </Directory> </Directory> <Directory> <id>12</id> <name>IC</name> </Directory> </Directory>

所以我试图注释一个类,以便JAXB / JAX-RS可以将其解组为有用的东西。

So I am trying to annotate a class so JAXB/JAX-RS can unmarshal this into something useful.

我尝试过这样的事情

@XmlRootElement(name="Directory") public class Directory { private int id; private String name; @XmlElement(name="Directory"); private List<Directory> directories = new ArrayList<Directory>(); }

但是,可以预见,它会抛出 IllegalAnnotationException 因为有2个属性具有相同的名称。

But, predictably, it throws an IllegalAnnotationException because of having 2 properties with the same name.

关于如何使用JAXB / JAX-RS来干净地处理这个混乱的任何想法或应该我只是自己解析它?

Any ideas as to how I can use JAXB/JAX-RS to cleanly handle this mess or should I just parse it on my own?

推荐答案

简答

该异常是由于字段/属性冲突造成的。您可以注释属性(获取方法)或在类型上设置以下注释:

The exception is due to a field/property collision. You can either annotate the properties (get methods) or set the following annotation on your type:

@XmlAccessorType(XmlAccessType.FIELD) public class Directory { ... }

长答案

JAXB的默认访问类型为 PUBLIC_MEMBER this表示JAXB将映射所有公共字段(实例变量)和属性(get / set方法)。

JAXB's default access type is PUBLIC_MEMBER this means that JAXB will map all public fields (instance variables) and properties (get/set methods).

public class Foo { private String bar; public String getBar() { return bar; } public void setBar(String bar) { this.bar = bar; } }

如果您注释一个字段:

public class Foo { @XmlAttribute private String bar; public String getBar() { return bar; } public void setBar(String bar) { this.bar = bar; } }

然后JAXB认为它有两个 bar 属性映射并抛出异常:

Then JAXB will think it has two bar properties mapped and thrown an exception:

Exception in thread "main" com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions Class has two properties of the same name "bar" this problem is related to the following location: at public java.lang.String example.Foo.getBar() at example.Foo this problem is related to the following location: at private java.lang.String example.Foo.bar at example.Foo

解决方案是注释属性并将XmlAccessType类型设置为 FIELD

The solution is to annotate the property and set the XmlAccessType type to FIELD

@XmlAccessorType(XmlAccessType.FIELD) public class Foo { @XmlAttribute private String bar; public String getBar() { return bar; } public void setBar(String bar) { this.bar = bar; } }


您的模型

目录

Directory

import java.util.ArrayList; import java.util.List; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name="Directory") @XmlAccessorType(XmlAccessType.FIELD) public class Directory { private int id; private String name; @XmlElement(name="Directory") private List<Directory> directories = new ArrayList<Directory>(); public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Directory> getDirectories() { return directories; } public void setDirectories(List<Directory> directories) { this.directories = directories; } }

演示

Demo

import java.io.File; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; public class Demo { public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance(Directory.class); Unmarshaller unmarshaller = jc.createUnmarshaller(); Directory directory = (Directory) unmarshaller.unmarshal(new File("input.xml")); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(directory, System.out); } }

更多推荐

如何使用错误的父/子模型解组xml消息

本文发布于:2023-11-22 15:09:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617914.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   模型   错误   消息   xml

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!