Moin Moin,
ich sitze gerade an folgendem Problem:
Ich habe von einem XML-File Mitarbeiter mit ihren Attributen eingelesen - und will diese nun einem Mitarbeiterobjekt zuordnen. Leider will das in der For-schleife nicht so ganz - bzw ich kann die Objekte ja nicht zur Laufzeit mit einem Index erzeugen.
Mitarbeiter:
Die Getter/Setter lass ich an der Stelle mal weg.
Ausschnitt aus dem Parser:
Vor allem den Teil mit der Objekterzeugung und der Zuweisung der Werte finde ich mehr als nur unelegant. Vor allem wenn ich in meinem XML-Dokument dann weitere Mitarbeiter erstelle.
Gibt es eine Lösung um die Zuweisung eleganter zu gestalten? Bspweise mit dem Konstruktor?
ich sitze gerade an folgendem Problem:
Ich habe von einem XML-File Mitarbeiter mit ihren Attributen eingelesen - und will diese nun einem Mitarbeiterobjekt zuordnen. Leider will das in der For-schleife nicht so ganz - bzw ich kann die Objekte ja nicht zur Laufzeit mit einem Index erzeugen.
Mitarbeiter:
Java:
import java.util.*;
public class Employee {
private int empNr;
private String empName;
private final List<String> skills;
public Employee() {
skills = new ArrayList<String>();
}
public Employee(int empNr, String empName, String... skills) {
this.empNr = empNr;
this.empName = empName;
this.skills = Arrays.asList(skills);
}
Die Getter/Setter lass ich an der Stelle mal weg.
Ausschnitt aus dem Parser:
Java:
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class Parser {
static Document employeeDocument;
static Document invoiceDocument;
/*public static void main(String[] args) {
createParserAndDocument();
readXMLFile();
//writeXMLFile();
}*/
public void readXMLFile() {
Element employeeElement = Parser.employeeDocument.getDocumentElement();
Employee e1 = new Employee();
Employee e2 = new Employee();
Employee e3 = new Employee();
Employee e4 = new Employee();
Employee e5 = new Employee();
Employee e6 = new Employee();
Employee e7 = new Employee();
Employee e8 = new Employee();
Employee e9 = new Employee();
List<Employee> liste = new ArrayList<Employee>();
liste.add(e1);
liste.add(e2);
liste.add(e3);
liste.add(e4);
liste.add(e5);
liste.add(e6);
liste.add(e7);
liste.add(e8);
liste.add(e9);
NodeList employeeItems = employeeElement.getElementsByTagName("employeeItem");
for (int i = 0; i < employeeItems.getLength(); i++) {
Element employeeItem = (Element) employeeItems.item(i);
String number = employeeItem.getAttribute("number");
int nr = Integer.decode(number);
String name = employeeItem.getAttribute("name");
String skill1 = employeeItem.getAttribute("skillname1");
String skill2 = employeeItem.getAttribute("skillname2");
String skill3 = employeeItem.getAttribute("skillname3");
// System.out.println(number);
// System.out.println(name);
// System.out.println(skill1+ "\n" + skill2+ "\n" + skill3);
String bla = "e"+i;
liste.get(i).setEmpName(name);
liste.get(i).setEmpNr(nr);
liste.get(i).addSkill(skill1);
liste.get(i).addSkill(skill2);
liste.get(i).addSkill(skill3);
System.out.println(liste.get(i).getEmpName() + liste.get(i).getEmpNr() + liste.get(i).getSkills());
}
}
/*private static void writeXMLFile() {
Element invoiceElement = invoiceDocument.createElement("invoice");
invoiceDocument.appendChild(invoiceElement);
invoiceElement.setAttribute("customer", "xy");
invoiceElement.setAttribute("day", "05");
invoiceElement.setAttribute("month", "05");
invoiceElement.setAttribute("year", "07");
Element invoiceItem1 = invoiceDocument.createElement("InvoiceItem");
invoiceElement.appendChild(invoiceItem1);
invoiceItem1.setAttribute("Product", "a");
invoiceItem1.setAttribute("Amount", "40");
Element invoiceItem2 = invoiceDocument.createElement("InvoiceItem");
invoiceElement.appendChild(invoiceItem2);
invoiceItem2.setAttribute("Product", "b");
invoiceItem2.setAttribute("Amount", "60");
}*/
public void createParserAndDocument() {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
try {
DocumentBuilder builder = factory.newDocumentBuilder();
employeeDocument = builder.parse(new File("employeeDocument.xml"));
invoiceDocument = builder.newDocument();
} catch (SAXParseException spe) {
System.out.println("\n** Parsing error" + ", line " +
spe.getLineNumber() + ", uri " + spe.getSystemId());
System.out.println(" " + spe.getMessage());
System.exit(1);
} catch (SAXException sxe) {
System.out.println(sxe.getMessage());
System.exit(1);
} catch (ParserConfigurationException pce) {
System.out.println(pce.getMessage());
System.exit(1);
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
System.exit(1);
}
}
}
Vor allem den Teil mit der Objekterzeugung und der Zuweisung der Werte finde ich mehr als nur unelegant. Vor allem wenn ich in meinem XML-Dokument dann weitere Mitarbeiter erstelle.
Gibt es eine Lösung um die Zuweisung eleganter zu gestalten? Bspweise mit dem Konstruktor?