【发布时间】:2021-11-05 06:28:57
【问题描述】:
我有 4 个 XML 文件,其结构如下:
<?xml version="1.0"?>
<Registry>
<Insitutions>
<Insitiution>
<name>A</name>
<location>B</location>
</Insitiution>
<Insitiution>
<name>C</name>
<location>D</location>
</Insitiution>
<Insitiution>
<name>E</name>
<location>F</location>
</Insitiution>
</Insitutions>
</Registry>
我已经为各个 XML 标记编写了类,并使用 jaxb 将它们解组。 我解组所有文件并将所有条目存储在一个列表中,如下所示:
private static List<String> loadBaseNodesets() throws JAXBException {
log.info("Loading nodesets");
Registry xmlentries = null;
JAXBContext jaxbContext = null;
List<Registry> entries = new ArrayList<>();
List<String> privateBaseNodeSets= new ArrayList<>();
File dir = new File("XMLFiles");
if (dir.exists() && dir.isDirectory()) {
FileFilter filter = new FileFilter() {
public boolean accept(File file) {
return file.isFile() && file.getName().endsWith(".xml");
}
};
log.info("iterating through all files");
File [] files = dir.listFiles(filter);
if (files != null) {
for (int i =0;i <1;i++) {
jaxbContext = JAXBContext.newInstance(Registry.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
xmlentries = (Registry) jaxbUnmarshaller.unmarshal(files[i]);
privateBaseNodeSets.add(xmlentries.toString());
log.info(files[i].getName()+" NodeSet loaded");
entries.add(xmlentries);
}
}
}
log.info("Nodeset loading complete");
return privateBaseNodeSets;
}
我需要将所有 xmlentries 放到一个字符串列表中。
现在从主程序中,我想检查是否可以获得相同格式的 XML 条目。
public static void main(String[] args) throws JAXBException {
log.info("Started local test main");
List<String> baseNodesets = loadBaseNodesets();
ListIterator<String> litr = baseNodesets.listIterator();
while (litr.hasNext()) {
JAXBContext jaxbContext=JAXBContext.newInstance(Registry.class);
Marshaller marshaller = jaxbContext.createMarshaller();#
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(, System.out);// DONT KNOW THE FIRST PARAMETER
}
我无法在此处获取第一个参数。有人可以帮忙吗?
【问题讨论】:
-
答案对您有帮助吗?能否解决问题?
-
当然做到了!抱歉有点耽误了。再次感谢:)
标签: java xml jaxb marshalling unmarshalling