I am trying to read XML files from a Folder. After I read the files I am extracting an element and write that in a text file.
The files Name Looks like that
zey_3_20161020-092152_JK6_NO.xml
zey_57_20161020-092152_A_K6.xml
zey_256_20161020-092152_B_A.xml
zey_1000_20161020-092152_B_A.xml
But when I am reading the files I dont get the right order. I am getting the order like this :
zey_1000_20161020-092152_B_A.xml
zey_256_20161020-092152_B_A.xml
zey_3_20161020-092152_JK6_NO.xml
zey_57_20161020-092152_A_K6.xml
Here is my code:
private String rgId;
private NodeList rgIdList;
ArrayList<String> list = new ArrayList<String>();
ArrayList<String> counter = new ArrayList<String>();
String splitFile = null;
public ReadXML() throws ParserConfigurationException, SAXException
{
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
File folder = new File("C:/...");
File[] listFiles = folder.listFiles();
for (File file : listFiles) {
if (file.isFile()) {
System.out.println(file.getName());
Document document = builder.parse(file);
rgIdList = document.getElementsByTagName("id");
if (rgIdList.getLength() > 0) {
rgId = rgIdList.item(0).getTextContent().toString();
list.add( rgId);
}
}
}
FileWriter fw = new FileWriter("C:/....");
for (String str : list) {
fw.write(str + "\r\n");
}
fw.close();
System.out.println(list);
} catch (IOException e) {
System.out.println(e);
}
}
public static void main(String[] args) throws IOException, ParserConfigurationException, SAXException {
new ReadXML();
}
Can anyone help how to sort the files in the right order? Thank you in advance