簡體   English   中英

JAXB-如何解組此XML?

[英]JAXB - how to unmarshall this XML?

我想使用JaxB將一些漂亮的討厭的XML解組到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?

謝謝

通過查看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類。 然后使用解組器將其解組到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;
    }

}

您有要取消編排的課程嗎? 聽起來它需要以下各項:

/** 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元素映射到適當的類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM