验证 List(Jaxb) 中未编组的条目 [英] Verifying unmarshalled entries from List( Jaxb)
问题描述
我有 4 个 XML 文件,其结构如下:
I have a 4 XML files having a structure as follows:
<?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 对它们进行了解组.我解组所有文件并将所有条目存储在列表中,如下所示:
I have written the classes for individual XML tags, and have unmarshalled them using jaxb. I unmarshall all the files and store all the entries in a list as follows:
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 放入一个字符串列表中.
I need to put all the xmlentries to a list of string.
现在从主程序中,我想检查是否可以获得相同格式的 XML 条目.
Now from the main program, I would like to check if I can get the same format of the XML entries.
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
}
我无法在此处获取第一个参数.有人可以帮忙吗?
I am not able to get the first parameter here. Could anyone please help?
推荐答案
我不确定我是否正确理解了你的问题,但我猜你正在尝试做这样的事情:
I am not sure if I understood your question correctly but I am guessing you are trying to do something like this:
- 逐个读取每个文件并解组并存储在
entries
列表中. - 然后遍历
entries List
并将它们一一编组. - 将转换后的 XML 存储到 List 中
- Read each file one by one and unmarshal and store in
entries
List. - Then loop over the
entries List
and marshal them one by one. - Store the converted XML into List
我在一个文件夹中存储了 2 个相同的 XML 文件并试图读取它
I stored 2 same XML files in a folder and tried to read it
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>
根:
@Data
@XmlRootElement(name = "Registry")
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {
@XmlElementWrapper(name = "Insitutions")
@XmlElement(name = "Insitiution")
private List<Insitiution> Insitiution;
}
机构:
@Data
@XmlAccessorType(XmlAccessType.FIELD)
public class Insitiution {
private String name;
private String location;
}
编组(主要):
public class Marshalling {
public static void main(String[] args) throws JAXBException, XMLStreamException, FactoryConfigurationError, IOException, SAXException {
final File dir = new File("/Users/Downloads/test");
final List<Root> entries = new ArrayList<>();
final Unmarshaller unmarshaller = JAXBContext.newInstance(com.newJAXB.Root.class).createUnmarshaller();
final StringWriter singleXmlEvent = new StringWriter();
final List<String> xmlOutput = new ArrayList<>();
final Marshaller marshaller = JAXBContext.newInstance(Root.class).createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
for (File file : dir.listFiles()) {
if (!file.isDirectory() && file.getName().endsWith(".xml")) {
//Storing all the files into entries List
final DataInputStream inputStream = new DataInputStream(new FileInputStream(file.getAbsolutePath()));
final XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
final com.newJAXB.Root root = unmarshaller.unmarshal(xmlStreamReader, com.newJAXB.Root.class).getValue();
entries.add(root);
System.out.println(root.toString());
}
}
//Loop through each entry in entries and marshal it
for (Root item : entries) {
marshaller.marshal(item, singleXmlEvent);
xmlOutput.add(singleXmlEvent.toString());
// Clear the StringWriter for next event
singleXmlEvent.getBuffer().setLength(0);
}
//Final all the XML
System.out.println(xmlOutput);
}
}
输出:
Root(Insitiution=[Insitiution(name=A, location=B), Insitiution(name=C, location=D), Insitiution(name=E, location=F)])
Root(Insitiution=[Insitiution(name=A, location=B), Insitiution(name=C, location=D), Insitiution(name=E, location=F)])
[<?xml version="1.0" encoding="UTF-8"?>
<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 version="1.0" encoding="UTF-8"?>
<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>
]
这篇关于验证 List(Jaxb) 中未编组的条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!