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>
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;
}
I need to put all the xmlentries to a list of string.
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
List.entries List
and marshal them one by one.I stored 2 same XML files in a folder and tried to read it
XML files:
<?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>
Root:
@Data
@XmlRootElement(name = "Registry")
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {
@XmlElementWrapper(name = "Insitutions")
@XmlElement(name = "Insitiution")
private List<Insitiution> Insitiution;
}
Insitiution:
@Data
@XmlAccessorType(XmlAccessType.FIELD)
public class Insitiution {
private String name;
private String location;
}
Marshalling (Main):
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);
}
}
Output:
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>
]