JAXB - 如何解组这个XML?

时间:2010-07-08 17:04:12

标签: java xml jaxb

Theres是一些非常讨厌的XML,我想使用JaxB解组到java对象。到目前为止,大部分内容似乎都很简单 - 但我仍然坚持这一点:

            <assets>
                <asset type="fixed">74,414</asset>
                <asset type="current">1,022,069</asset>
                <asset type="other">0</asset>
                <total type="assets">1,096,483</total>
            </assets>

这是dtd的相关部分

<!ELEMENT assets (asset|total)*> <!ELEMENT asset (#PCDATA)> <!ATTLIST asset type CDATA #REQUIRED> <!ELEMENT total (#PCDATA)> <!ATTLIST total type CDATA #REQUIRED>

有什么想法吗?或者我应该放弃尝试使用JAXB吗?

由于

2 个答案:

答案 0 :(得分:5)

查看XML和DTD,我创建了结构的XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  elementFormDefault="qualified">
  <xs:element name="assets">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" ref="asset"/>
        <xs:element maxOccurs="unbounded" ref="total"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="asset">
    <xs:complexType mixed="true">
      <xs:attribute name="type" use="required" type="xs:string"/>
    </xs:complexType>
  </xs:element>
  <xs:element name="total">
    <xs:complexType mixed="true">
      <xs:attribute name="type" use="required" type="xs:string"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

使用xjc生成使用XSD中的JAXB绑定注释注释的Java类。然后使用unmarshaller将其解组为Java对象。

修改

生成的Java类:

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlValue;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "asset",
    "total"
})
@XmlRootElement(name = "assets")
public class Assets {

    @XmlElement(required = true)
    protected List<Asset> asset;
    @XmlElement(required = true)
    protected List<Total> total;

    public List<Asset> getAsset() {
        if (asset == null) {
            asset = new ArrayList<Asset>();
        }
        return this.asset;
    }

    public List<Total> getTotal() {
        if (total == null) {
            total = new ArrayList<Total>();
        }
        return this.total;
    }

}

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "content"
})
@XmlRootElement(name = "asset")
public class Asset {

    @XmlValue
    protected String content;
    @XmlAttribute(required = true)
    protected String type;

    public String getContent() {
        return content;
    }

    public void setContent(String value) {
        this.content = value;
    }

    public String getType() {
        return type;
    }

    public void setType(String value) {
        this.type = value;
    }

}

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "content"
})
@XmlRootElement(name = "total")
public class Total {

    @XmlValue
    protected String content;
    @XmlAttribute(required = true)
    protected String type;

    public String getContent() {
        return content;
    }

    public void setContent(String value) {
        this.content = value;
    }

    public String getType() {
        return type;
    }

    public void setType(String value) {
        this.type = value;
    }

}

答案 1 :(得分:1)

你有一个你正在解组的课吗?听起来它需要以下内容:

/** AssetContainer */
@XmlRootElement(namespace = "project/schema")
@XmlAccessorType(XmlAccessType.FIELD)
public class AssetContainer implements Unmarshallable {
    private List<Asset> assetList;
    private int totalAssets;
}

/** Asset */
@XmlType
@XmlAccessorType(XmlAccessType.FIELD)
public class Asset implements Unmarshallable {
    private AssetTypeEnum type;
    private int count;
}

/** Unmarshallable */
public interface Unmarshallable {
    // Marker interface
}

然后使用XmlTypeAdapter将XML元素映射到正确的类。