如何用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)));
}
但我总是将数据字段设为null。所以我的问题是这种类型的xml的正确模型是什么?
答案 0 :(得分:0)
首先,虽然您的列表名为data
,但根据您的架构,其中的元素属于raw
类型。因此,您还需要在java中建模raw
。
其次,列表将是您的元素 wrapper 。要指定您可以使用注释XmlElementWrapper
。
最后,您的java类可能应该被称为Data
而不是Xml!
您的班级将如下所示:
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类