JAXB 新手,我正在尝试解组XML 文档。我用的是xjc
从 XSD 文件构建 DataSet 和 ObjectFactory 的命令:
<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="NewDataSet">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Table">
<xs:complexType>
<xs:sequence>
<xs:element name="AUTHOR" type="xs:string" minOccurs="0"/>
<xs:element name="TITLE" type="xs:string" minOccurs="0"/>
<xs:element name="ISBN" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
The NewDataSet
生成的类如下:
package generated;
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;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"table"
})
@XmlRootElement(name = "NewDataSet")
public class NewDataSet {
@XmlElement(name = "Table")
protected List<NewDataSet.Table> table;
public List<NewDataSet.Table> getTable() {
if (table == null) {
table = new ArrayList<NewDataSet.Table>();
}
return this.table;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"author",
"title",
"isbn"
})
public static class Table {
@XmlElement(name = "AUTHOR")
protected String author;
@XmlElement(name = "TITLE")
protected String title;
@XmlElement(name = "ISBN")
protected String isbn;
public String getAUTHOR() {
return author;
}
public void setAUTHOR(String value) {
this.author = value;
}
public String getTITLE() {
return title;
}
public void setTITLE(String value) {
this.title = value;
}
public String getISBN() {
return isbn;
}
public void setISBN(String value) {
this.isbn = value;
}
}
}
The ObjectFactory
is:
package generated;
import javax.xml.bind.annotation.XmlRegistry;
@XmlRegistry
public class ObjectFactory {
public ObjectFactory() {
}
public NewDataSet createNewDataSet() {
return new NewDataSet();
}
public NewDataSet.Table createNewDataSetTable() {
return new NewDataSet.Table();
}
}
我尝试解组的 XML 文件如下:
<NewDataSet xmlns="">
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<Table diffgr:id="Table1" msdata:rowOrder="0">
<AUTHOR>Kubo Tite</AUTHOR>
<TITLE>Bleach</TITLE>
<ISBN>1234456</ISBN>
</Table>
<Table diffgr:id="Table2" msdata:rowOrder="2">
<AUTHOR>Masashi Kishimoto</AUTHOR>
<TITLE>Naruto</TITLE>
<ISBN>435345</ISBN>
</Table>
<Table diffgr:id="Table3" msdata:rowOrder="3">
<AUTHOR>Eiichiro Oda</AUTHOR>
<TITLE>One Piece</TITLE>
<ISBN>56767</ISBN>
</Table>
</diffgr:diffgram>
</NewDataSet>
进行解组的代码如下:
package consume;
import generated.NewDataSet;
import generated.NewDataSet.Table;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class UnmarshallingCode {
public static void main(String[] args) {
try{
File file=new File("data.xml");
JAXBContext jb=JAXBContext.newInstance(NewDataSet.class);
Unmarshaller unmarshaller=jb.createUnmarshaller();
NewDataSet newDataSet=(NewDataSet)unmarshaller.unmarshal(file);
for(Table t: newDataSet.getTable()){
System.out.println(t.getTITLE());
}
}catch(JAXBException e){
e.printStackTrace();
}
}
}
上面的代码不会产生任何错误,但不会产生任何结果。电话newDataSet.getTable()
返回一个空列表。
但是如果我删除<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
及其相关属性,上面的代码运行良好并生成 XML 文档中的所有标题。但这不是一个允许的解决方案,因为 XML 文档非常大,而且很可能不允许我对结构进行巨大的更改。如何解组上述 XML 文件?请指教。