public Document export(Document doc, Element root, Customer cus, List<CardBean> cards) { Element customer = doc.createElement("Kunde"); root.appendChild(customer); Attr cusNum = doc.createAttribute("Kundennummer"); cusNum.setValue(cus.getCustomernumber()); customer.setAttributeNode(cusNum); Element cusName = doc.createElement("Name"); cusName.appendChild(doc.createTextNode(cus.getName())); customer.appendChild(cusName); Iterator<CardBean> it = cards.iterator(); while (it.hasNext()) { CardBean card = it.next(); Element cusCard = doc.createElement("Karte"); customer.appendChild(cusCard); Attr cardNum = doc.createAttribute("Kartennummer"); cardNum.setValue(card.getCardNumber()); cusCard.setAttributeNode(cardNum); Element telFirst = doc.createElement("Telefonnummer"); telFirst.appendChild(doc.createTextNode(card.getPhoneString())); cusCard.appendChild(telFirst); } return doc; }
/** @param args */ public static void main(String[] args) { CustomerController cc = new CustomerController(); List<DaoObject> customerList = cc.listObjects(); List<String> customerNumbers = new ArrayList<String>(); SearchCardController scc = new SearchCardController(); try { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document doc = docBuilder.newDocument(); Element root = doc.createElement("Root"); doc.appendChild(root); CustomersAndCardToXMLExporter exporter = new CustomersAndCardToXMLExporter(); if (customerList != null && customerList.size() > 0) { System.out.println(customerList.size() + " Kunden gefunden"); if (customerList.size() > 1) { Iterator it = customerList.iterator(); while (it.hasNext()) { Customer cus = (Customer) it.next(); String cNumber = cus.getCustomernumber(); List<CardBean> cards = scc.performSearch(new Integer(cus.getId())); doc = exporter.export(doc, root, cus, cards); } } } else { System.out.println("Kein Customer gefunden"); } TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); String dfs = "yyyy-MM-dd_HH-mm-ss"; StreamResult result = new StreamResult(new File("C:\\temp\\Export-" + DateUtils.now(dfs) + ".xml")); // Output to console for testing // StreamResult result = new StreamResult(System.out); transformer.transform(source, result); System.out.println("File saved!"); } catch (ParserConfigurationException pce) { pce.printStackTrace(); } catch (TransformerException tfe) { tfe.printStackTrace(); } // sc.readData(customerNumbers); }