我要求更大的 XML 文件表示的原因是您的代码将完全取决于 XML 文件的要求。例如,您的这段代码适用于以下 XML 文件:
代码:
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.*;
import javax.xml.bind.*;
public class JaxbTest {
private static final String RESOURCE_NAME = "data.txt";
public static void main(String[] args) {
// marshallTest();
unmarshallTest();
}
private static void unmarshallTest() {
JAXBContext context;
try {
context = JAXBContext.newInstance(RtSuperQuickMetadata.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
RtSuperQuickMetadata metaData = (RtSuperQuickMetadata) unmarshaller
.unmarshal(JaxbTest.class.getResourceAsStream(RESOURCE_NAME));
System.out.println(metaData);
} catch (JAXBException e) {
e.printStackTrace();
}
}
private static void marshallTest() {
RtSuperQuickMetadata data = new RtSuperQuickMetadata();
List<RtSuperQuickMetadataItem> metaItemList = new ArrayList<RtSuperQuickMetadataItem>();
RtSuperQuickMetadataItem metaDataItem = new RtSuperQuickMetadataItem();
List<FileNumber> fileNumbers = new ArrayList<FileNumber>();
fileNumbers.add(new FileNumber("123"));
fileNumbers.add(new FileNumber("124"));
fileNumbers.add(new FileNumber("125"));
fileNumbers.add(new FileNumber("126"));
metaDataItem.setFileNumbers(fileNumbers);
metaItemList.add(metaDataItem);
data.setRtSuperQuickMetadataItems(metaItemList);
JAXBContext context;
try {
context = JAXBContext.newInstance(RtSuperQuickMetadata.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(data, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
@XmlRootElement(name = "contentFiles")
class RtSuperQuickMetadata {
private List<RtSuperQuickMetadataItem> rtSuperQuickMetadataItems;
@XmlElement(name = "contentFile")
public final List<RtSuperQuickMetadataItem> getRtSuperQuickMetadataItems() {
return rtSuperQuickMetadataItems;
}
public void setRtSuperQuickMetadataItems(
List<RtSuperQuickMetadataItem> rtSuperQuickMetadataItems) {
this.rtSuperQuickMetadataItems = rtSuperQuickMetadataItems;
}
@Override
public String toString() {
return "RtSuperQuickMetadata [rtSuperQuickMetadataItems="
+ rtSuperQuickMetadataItems + "]";
}
}
class RtSuperQuickMetadataItem {
private List<FileNumber> fileNumbers;
public RtSuperQuickMetadataItem() {
fileNumbers = new ArrayList<FileNumber>();
}
@XmlElement(name = "fileNumbers")
public List<FileNumber> getFileNumbers() {
return fileNumbers;
}
public void setFileNumbers(List<FileNumber> fileNumbers) {
this.fileNumbers = fileNumbers;
}
@Override
public String toString() {
return "RtSuperQuickMetadataItem [fileNumbers=" + fileNumbers + "]";
}
}
@XmlRootElement(name = "fileNumber")
class FileNumber {
private String fileNumber;
public FileNumber() {}
public FileNumber(final String fileNumber) {
this.fileNumber = fileNumber;
}
// @XmlElement(name = "fileNumber")
public final String getFileNumber() {
return fileNumber;
}
public final void setFileNumber(final String fileNumber) {
this.fileNumber = fileNumber;
}
@Override
public final String toString() {
return fileNumber;
}
}
xml 文件,假设它与您的类文件位于同一目录中:jaxbTest.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<contentFiles>
<contentFile>
<fileNumbers>
<fileNumber>123</fileNumber>
</fileNumbers>
<fileNumbers>
<fileNumber>124</fileNumber>
</fileNumbers>
<fileNumbers>
<fileNumber>125</fileNumber>
</fileNumbers>
<fileNumbers>
<fileNumber>126</fileNumber>
</fileNumbers>
</contentFile>
</contentFiles>
但是,如果这不是您想要的 XML 结构,则必须更改代码。
编辑
如果您不想将每个 fileNumber 元素嵌套在 fileNumbers 元素中,则摆脱 FileNumbers 类,而是使用List<String>
带有@XmlElementWrapper
注释的 a 。
@XmlElementWrapper(name = "fileNumbers")
@XmlElement(name = "fileNumber")
public List<String> getFileNumbers() {
return fileNumbers;
}
例如,请查看以下代码:
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.*;
import javax.xml.bind.*;
public class JaxbTest {
private static final String RESOURCE_NAME = "data.xml";
public static void main(String[] args) {
// marshallTest();
unmarshallTest();
}
private static void unmarshallTest() {
JAXBContext context;
try {
context = JAXBContext.newInstance(RtSuperQuickMetadata.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
RtSuperQuickMetadata metaData = (RtSuperQuickMetadata) unmarshaller
.unmarshal(JaxbTest.class.getResourceAsStream(RESOURCE_NAME));
System.out.println(metaData);
} catch (JAXBException e) {
e.printStackTrace();
}
}
private static void marshallTest() {
RtSuperQuickMetadata data = new RtSuperQuickMetadata();
List<RtSuperQuickMetadataItem> metaItemList = new ArrayList<RtSuperQuickMetadataItem>();
RtSuperQuickMetadataItem metaDataItem = new RtSuperQuickMetadataItem();
// List<FileNumber> fileNumbers = new ArrayList<FileNumber>();
// fileNumbers.add(new FileNumber("123"));
// fileNumbers.add(new FileNumber("124"));
// fileNumbers.add(new FileNumber("125"));
// fileNumbers.add(new FileNumber("126"));
List<String> fileNumbers = new ArrayList<String>();
fileNumbers.add("123");
fileNumbers.add("124");
fileNumbers.add("125");
fileNumbers.add("126");
metaDataItem.setFileNumbers(fileNumbers);
metaItemList.add(metaDataItem);
data.setRtSuperQuickMetadataItems(metaItemList);
JAXBContext context;
try {
context = JAXBContext.newInstance(RtSuperQuickMetadata.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(data, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
@XmlRootElement(name = "contentFiles")
class RtSuperQuickMetadata {
private List<RtSuperQuickMetadataItem> rtSuperQuickMetadataItems;
@XmlElement(name = "contentFile")
public final List<RtSuperQuickMetadataItem> getRtSuperQuickMetadataItems() {
return rtSuperQuickMetadataItems;
}
public void setRtSuperQuickMetadataItems(
List<RtSuperQuickMetadataItem> rtSuperQuickMetadataItems) {
this.rtSuperQuickMetadataItems = rtSuperQuickMetadataItems;
}
@Override
public String toString() {
return "RtSuperQuickMetadata [rtSuperQuickMetadataItems="
+ rtSuperQuickMetadataItems + "]";
}
}
class RtSuperQuickMetadataItem {
private List<String> fileNumbers;
public RtSuperQuickMetadataItem() {
fileNumbers = new ArrayList<String>();
}
@XmlElementWrapper(name = "fileNumbers")
@XmlElement(name = "fileNumber")
public List<String> getFileNumbers() {
return fileNumbers;
}
public void setFileNumbers(List<String> fileNumbers) {
this.fileNumbers = fileNumbers;
}
@Override
public String toString() {
return "RtSuperQuickMetadataItem [fileNumbers=" + fileNumbers + "]";
}
}
将使用此 XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<contentFiles>
<contentFile>
<fileNumbers>
<fileNumber>123</fileNumber>
<fileNumber>124</fileNumber>
<fileNumber>125</fileNumber>
<fileNumber>126</fileNumber>
</fileNumbers>
</contentFile>
</contentFiles>