Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用JAXB使用动态根元素解组XML_Java_Xml_Jaxb - Fatal编程技术网

Java 使用JAXB使用动态根元素解组XML

Java 使用JAXB使用动态根元素解组XML,java,xml,jaxb,Java,Xml,Jaxb,我正在尝试与第三方系统集成,根据对象的类型,返回的XML文档的根元素会发生变化。例如: GET /objecttype1-1/ returns: <?xml version="1.0" encoding="UTF-8"?> <objecttype1 xmlns="path"> <id>1</id> <description>obj1</description> </objecttype1> GET/

我正在尝试与第三方系统集成,根据对象的类型,返回的XML文档的根元素会发生变化。例如:

GET /objecttype1-1/ returns:
<?xml version="1.0" encoding="UTF-8"?>
<objecttype1 xmlns="path">
   <id>1</id>
   <description>obj1</description>
</objecttype1>
GET/objecttype1-1/返回:
1.
obj1
以及:

GET/objecttype2-3返回:
3.
家
由于子元素不能保证是相同的(id除外),因此我认为一个包含
@XmlMixed
@xmlanyement
的列表将处理它们。但是如何映射根元素呢
@XmlRootElement(name=“???”)


由于技术限制,我无法使用EclipseLink/MOXy。谢谢。

这个问题已经问了5年了,但我找到了这个问题的有效解决方案,为社区分享

首先,我们定义JAXB bean如下:

@XmlRootElement(name = "objecttype1")
@XmlAccessorType(XmlAccessType.NONE)
public class Objecttype1 {

  @XmlElement(name = "id")
  private String id;

  @XmlElement(name = "description")
  private String description;

  public String getId() {
      return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  public String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    this.description = description;
  }
}

@XmlRootElement(name = "objecttype2")
@XmlAccessorType(XmlAccessType.NONE)
public class Objecttype2 {

  @XmlElement(name = "id")
  private String id;

  @XmlElement(name = "address")
  private String address;

  public String getId() {
    return id;
   }

  public void setId(String id) {
    this.id = id;
  }

  public String getAddress() {
    return address;
  }

  public void setDescription(String address) {
    this.address = address;
  }
}
接下来,我们需要JAXB上下文和解组器本身:

private static final JAXBContext jaxbContext;

 public Unmarshaller getUnmarshaller() {
    try {
        return jaxbContext.createUnmarshaller();
    } catch (JAXBException ex) {
        throw new IllegalStateException(ex);
    }
}
这样,JAXB上下文将加载其候选对象,并通过根元素名称检查它们之间是否存在匹配。我们只是解组并检查接收对象的类型:

try {
  Object unmarshalledObject = getUnmarshaller().unmarshal(new StringReader(xmlString));

  if (unmarshalledObject instanceof Objecttype1) {
   //do Objecttype1 related work
  } else if (unmarshalledObject instanceof Objecttype2) {
   //do Objecttype2 related work
  } else {
   // unexpected object type
  }
} catch (JAXBException ex) {
 //handle ex
}

你有没有找到解决上述问题的办法?我也处于类似的情况
try {
  Object unmarshalledObject = getUnmarshaller().unmarshal(new StringReader(xmlString));

  if (unmarshalledObject instanceof Objecttype1) {
   //do Objecttype1 related work
  } else if (unmarshalledObject instanceof Objecttype2) {
   //do Objecttype2 related work
  } else {
   // unexpected object type
  }
} catch (JAXBException ex) {
 //handle ex
}