如果属性等于xxx,则JAXB获取元素

时间:2015-12-16 10:31:04

标签: java xml jaxb

所以我没什么问题。我的XML代码如下所示:

<Images>
    <boxart side="back" width="1525" height="2162" thumb="boxart/thumb/original/back/2-1.jpg">boxart/original/back/2-1.jpg</boxart>
    <boxart side="front" width="1525" height="2160" thumb="boxart/thumb/original/front/2-1.jpg">boxart/original/front/2-1.jpg</boxart>
    <banner width="760" height="140">graphical/2-g2.jpg</banner><banner width="760" height="140">graphical/2-g3.jpg</banner>
</Images>

我想只获得XmlElement属性是&#34;前面&#34;。我该怎么做?我试过这样的事情:

@XmlRootElement(name = "Images")
public class Image {
    private String boxart;

    public String getBoxart() {
        return boxart;
    }

    @XmlElement(name = "boxart")
    public void setBoxart(String boxart) {
        this.boxart = boxart;
    }
}

2 个答案:

答案 0 :(得分:0)

问题解决了:

图片类:

@XmlRootElement(name = "Images")
public class Image {
    private List<Boxart> boxarts;

    public List<Boxart> getBoxarts() {
        return boxarts;
    }

    @XmlElement(name = "boxart")
    public void setBoxarts(List<Boxart> boxarts) {
        this.boxarts = boxarts;
    }
}

Boxart课程:

public class Boxart {

    @XmlAttribute(name="side")
    private String side;
    @XmlAttribute(name="thumb")
    private String thumb;

    public String getSide() {
        return side;
    }

    public void setSide(String side) {
        this.side = side;
    }

    public String getThumb() {
        return thumb;
    }

    public void setThumb(String thumb) {
        this.thumb = thumb;
    }
}

答案 1 :(得分:0)

我没有在提供的XML中看到front属性.Better方式是使用它来编写XSD和生成Java类。我已经将evey字段作为可选字段,因为我不确定哪些属性是强制性的。

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Images">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="boxart" maxOccurs="unbounded" minOccurs="0">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:string">
                <xs:attribute type="xs:string" name="side" use="optional"/>
                <xs:attribute type="xs:short" name="width" use="optional"/>
                <xs:attribute type="xs:short" name="height" use="optional"/>
                <xs:attribute type="xs:string" name="thumb" use="optional"/>
                <xs:attribute type="xs:string" name="front" use="optional"/>
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
        <xs:element name="banner" maxOccurs="unbounded" minOccurs="0">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:string">
                <xs:attribute type="xs:short" name="width" use="optional"/>
                <xs:attribute type="xs:short" name="height" use="optional"/>
                <xs:attribute type="xs:string" name="front" use="optional"/>
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

将此文件另存为some.xsd并放在eclipse中的resources文件夹中。右键单击xsd文件并生成 - &gt; JAXB类。

它将生成两个类Image.java和ObjectFactory.java

注意:如果您使用的是JRE而不是JDK,则会出现以下错误。

   Error: Could not find or load main class com.sun.tools.internal.xjc.XJCFacade

从这里开始,您可以使用JAXB获取所需属性的值。