2

我有一个看起来像这样的 xml 文档

<root>
<subElement>
    <a>an instance</a>
    <b>another instance</b>
    <problemElement>
      <option>1st instance</option>
      <option>2nd instance</option>
      <option>3rd instance</option>
    </problemElement>

</subElement>

<subElement>
    <a></a>
    <b></b>
    <problemElement>
      <option>instance</option>
    </problemElement>

</subElement>
</root>

我的 jaxb 类看起来像这样;

@XmlRootElement(name = "root")
@XmlType(name = "Root")
public class Root{

  @XmlElement(name = "subElement", required = true)
  private final List<SubElement> subElementList = new ArrayList<SubElement>();

  public List<SubElement> getSubElementList() {
    return subElementList;
  }
}



@XmlType(name = "SubElement", propOrder = {"a", "b", "problemElement"})
public abstract class SubElement{

  @XmlElement(required = true)
  private String a;

  @XmlElement(required = true)
  private String b;

  @XmlElement(name = "problemElement", required = true)
  private List<ProblemElement> problemElement= new ArrayList<ProblemElement>();

  public String getA() {
    return a;
  }

  public String getB() {
    return b;
  }

  public List<ProblemElement> getProblemElement() {
    return problemElement;
  }
}



@XmlType(name = "ProblemElement" )
public class ProblemElement {

  @XmlElement(required = true)
  private String option;


  public String getOption() {
    return option;
  }
}

除了problemElement,一切正常。该列表仅返回 xml 中的最后一个选项节点值,在本例中为“3rd instance”。我究竟做错了什么?

4

2 回答 2

3

简短的回答

对于您的用例,我将利用属性@XmlElementWrapper上的注释problemElement

@XmlElementWrapper(name="problemElement")
@XmlElement(name="option")
private List<ProblemElement> problemElement = new ArrayList<ProblemElement>();

我会在该@XmlValue物业上使用该option物业:

  @XmlValue
  private String option;

长答案

下面是一个完全映射的示例。

package forum10531285;

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

@XmlRootElement(name = "root")
@XmlType(name = "Root")
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

    @XmlElement(name = "subElement", required = true)
    private final List<SubElement> subElementList = new ArrayList<SubElement>();

}

子元素

package forum10531285;

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

@XmlType(name = "SubElement", propOrder = { "a", "b", "problemElement" })
@XmlAccessorType(XmlAccessType.FIELD)
public class SubElement {

    @XmlElement(required = true)
    private String a;

    @XmlElement(required = true)
    private String b;

    @XmlElementWrapper(name="problemElement")
    @XmlElement(name="option")
    private List<ProblemElement> problemElement = new ArrayList<ProblemElement>();

}

问题元素

package forum10531285;

import javax.xml.bind.annotation.*;

@XmlType(name = "ProblemElement" )
@XmlAccessorType(XmlAccessType.FIELD)
public class ProblemElement {

  @XmlValue
  private String option;

}

演示

package forum10531285;

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum10531285/input.xml");
        Root root = (Root) unmarshaller.unmarshal(xml);

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

}

输入.xml/输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
    <subElement>
        <a>an instance</a>
        <b>another instance</b>
        <problemElement>
            <option>1st instance</option>
            <option>2nd instance</option>
            <option>3rd instance</option>
        </problemElement>
    </subElement>
    <subElement>
        <a></a>
        <b></b>
        <problemElement>
            <option>instance</option>
        </problemElement>
    </subElement>
</root>
于 2012-05-10T10:19:42.700 回答
2

抱歉,但是...您的 ProblemElement 可能有无限数量的选项?如果是这种情况,那么您应该编写此代码而不是 ProblemElement 类:

public class ProblemElement {
    private List<String> options;

    public List<String> getOptions() {
        return options;
    }
    // other stuff here
}

否则,您的 JAXBinding 将仅检索 XML 中的最后一个选项,在本例中为第三个。当然,您应该相应地重写您的绑定注释。

于 2012-05-10T09:44:59.240 回答