You can use Java's built in XML parser in the the javax.xml.parsers
package to load the XML into a document, then loop over the NodeList
to find the right attributes. Add the right attributes to a list in array form and return the list's iterator when finished:
public static Iterator<String> getAttributes(File xmlFile) throws ParserConfigurationException, SAXException, IOException {
ArrayList<String> attributes = new ArrayList<String>();
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);
/*
* optional, but recommended read this -
* http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with
* -java-how-does-it-work
*/
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("setting");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
String group = eElement.getElementsByTagName("group").item(0).getTextContent();
String name = eElement.getElementsByTagName("name").item(0).getTextContent();
String job = eElement.getElementsByTagName("job").item(0).getTextContent();
attributes.add(group);
attributes.add(name);
attributes.add(job);
}
}
return attributes.iterator();
}
Now all you have to do is call getAttributes()
before reading the URL and use Iterator#next()
to get the info in the correct order:
Iterator<String> iterator;
try {
iterator = getAttributes(new File("..."));
} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
return;
}
ArrayList<Employee> pArray = new ArrayList<SettingForm>();
try {
URL url = new URL(urlLink);
BufferedReader input = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line= input.readLine()) != null){
String[] value = line.split("=");
if(value.length > 1){
pArray .add(new Employee(value[0], value[1], iterator.next(), iterator.next(), iterator.next()));
}
}
input.close();
} catch (MalformedURLException e) {
e.printStackTrace();
}