0

如何使用 jaxb 读取以下 xml?

       <xml xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
    xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
    xmlns:rs='urn:schemas-microsoft-com:rowset'
    xmlns:z='#RowsetSchema'>
<s:Schema id='RowsetSchema'>
    <s:ElementType name='row' content='eltOnly' rs:updatable='true'>
        <s:AttributeType name='ATCCode' rs:number='1' rs:nullable='true' rs:writeunknown='true' rs:basecatalog='ATC'
             rs:basetable='#t1' rs:basecolumn='ATCCode'>
            <s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='10'/>
        </s:AttributeType>
        <s:AttributeType name='DDD' rs:number='2' rs:nullable='true' rs:writeunknown='true' rs:basecatalog='ATC' rs:basetable='#t1'
             rs:basecolumn='DDD'>
            <s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='20'/>
        </s:AttributeType>
        <s:AttributeType name='UnitType' rs:number='3' rs:nullable='true' rs:writeunknown='true' rs:basecatalog='ATC'
             rs:basetable='#t1' rs:basecolumn='UnitType'>
            <s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='255'/>
        </s:AttributeType>
        <s:AttributeType name='AdmCode' rs:number='4' rs:nullable='true' rs:writeunknown='true' rs:basecatalog='ATC'
             rs:basetable='#t1' rs:basecolumn='AdmCode'>
            <s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='255'/>
        </s:AttributeType>
        <s:AttributeType name='DDDComment' rs:number='5' rs:nullable='true' rs:writeunknown='true' rs:basecatalog='ATC'
             rs:basetable='#t1' rs:basecolumn='DDDComment'>
            <s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='255'/>
        </s:AttributeType>
        <s:extends type='rs:rowbase'/>
    </s:ElementType>
</s:Schema>
<rs:data>
    <z:row ATCCode='A01AA01' DDD='1.1' UnitType='mg' AdmCode='O' DDDComment='0.5 mg Fluoride'/>
    <z:row ATCCode='A01AA03' DDD='1.1' UnitType='mg' AdmCode='O'/>
    <z:row ATCCode='A01AB02' DDD='60' UnitType='mg' AdmCode='O'/>
    <z:row ATCCode='A01AB03' DDD='30' UnitType='mg' AdmCode='O'/>
    <z:row ATCCode='A01AB04' DDD='40' UnitType='mg' AdmCode='O'/>
    <z:row ATCCode='A01AB05' DDD='0.18' UnitType='g' AdmCode='O'/>
    <z:row ATCCode='A01AB06' DDD='3' UnitType='mg' AdmCode='O'/>
    <z:row ATCCode='A01AB07' DDD='80' UnitType='mg' AdmCode='O'/>
    <z:row ATCCode='A01AB09' DDD='0.2' UnitType='g' AdmCode='O'/>
    <z:row ATCCode='A01AB10' DDD='20' UnitType='mg' AdmCode='O'/>
    <z:row ATCCode='A01AB23' DDD='1' UnitType='mg' AdmCode='O'/>
    <z:row ATCCode='A02AA04' DDD='3' UnitType='g' AdmCode='O'/>
    <z:row ATCCode='A02AB02' DDD='5' UnitType='g' AdmCode='O'/>
</rs:data>
</xml>

Java模型类:-

@XmlAccessorType(XmlAccessType.FIELD)
    @XmlRootElement(name = "xml")   
    public class Xml{
        @XmlElement 
        private List<Data> data;

    }

然后加载它

private Xml unmarshall() throws JAXBException, IOException {
    JAXBContext jc = JAXBContext.newInstance(Xml.class);
    Unmarshaller unmarshaller = jc.createUnmarshaller();        
    return (Xml) unmarshaller.unmarshal(new BufferedReader(new FileReader(new File(fileName)));
}

但我总是将数据字段设为空。所以我的问题是这种类型的 xml 的正确模型是什么?

4

1 回答 1

0

首先,虽然您的 list 被称为data,但根据您的架构,其中的 Elements 是 type raw。因此,您还需要raw在 java 中建模。

其次,该列表将是您的元素包装器。要指定您可以使用 Annotation XmlElementWrapper

最后,您的 java 类可能不应该被称为DataXml!

您的班级将如下所示:

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

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Data{

    @XmlElementWrapper(name="rows")
    @XmlElement(name="row")
    private List<Row> rows;

    // Getters and Setters!!

}

基本上,注释XmlElementWrapper告诉 JAXB 该字段是一个集合,并且属性name指定它映射到的 Xml 元素。而该集合元素的 xml 标记由注释的name属性指定。XmlElement

对于一个完整的例子(也是我的片段的来源)检查:http ://blog.bdoughan.com/2010/09/jaxb-collection-properties.html

当然,或者,您可以尝试使用xjc从模式生成 java 类

于 2013-08-28T12:41:30.213 回答