import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;
public class CreateXML {
DocumentBuilderFactory documentBuilderFactory;
DocumentBuilder documentBuilder;
Document document;
public void createXML(Map order_details, ArrayList item_details, String fileName) {
Set set;
Iterator iterator;
try {
// Creating an empty XML Document
documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilder = documentBuilderFactory.newDocumentBuilder();
document = documentBuilder.newDocument();
// Creating XML tree
// create the root element and add it to the document
Element orderElement = document.createElement("book");
document.appendChild(orderElement);
Element orderDetailElement = document.createElement("bookDetails");
orderElement.appendChild(orderDetailElement);
set = order_details.keySet();
iterator = set.iterator();
// create child element acording to the key set,add an attribute, and add to root (orderDetails)
while (iterator.hasNext()) {
String key = (String) iterator.next();
Element detailElement = document.createElement(key);
detailElement.appendChild(document.createTextNode((String) order_details.get(key)));
orderDetailElement.appendChild(detailElement);
}
for (int i = 0; i < item_details.size(); i++) {
// create item element and add item details as child
Element itemElement = document.createElement("chapter");
orderElement.appendChild(itemElement);
Element itemId;
String itemdetails[] = (String[]) item_details.get(i);
Element itemId1 = document.createElement("chapterName");;
itemId1.appendChild(document.createTextNode(itemdetails[0]));
itemElement.appendChild(itemId1);
for (int j = 1; j < itemdetails.length; j++) {
itemId = document.createElement("Paragrap" + (i + 1));
itemId.appendChild(document.createTextNode(itemdetails[j]));
itemElement.appendChild(itemId);
}
}
// create XML file
File xmlFile = new File(fileName);
xmlFile.createNewFile();
FileOutputStream isod = new FileOutputStream(xmlFile);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(isod);
transformer.transform(source, result);
isod.flush();
isod.close();
} catch (IOException ex) {
ex.printStackTrace();
} catch (TransformerException ex) {
ex.printStackTrace();
} catch (ParserConfigurationException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
CreateXML cXML = new CreateXML();
Map order_details = new HashMap();
ArrayList item_details = new ArrayList();
String itemDetails[] = new String[4];
String itemDetails2[] = new String[4];
String itemDetails3[] = new String[4];
// add order details
order_details.put("ISBN", "2012314123");
order_details.put("bookName", "Pheonix Order");
order_details.put("authorName", "J. K. Rolings");
// add item details
itemDetails2[0] = "Chapter 1 inceput";
itemDetails2[1] = "ARR01";
itemDetails2[2] = "DCL White Arak";
itemDetails2[3] = "1";
item_details.add(itemDetails2);
itemDetails2[0] = "Chapter 2 continuare";
itemDetails2[1] = "ARR02";
itemDetails2[2] = "Lemon Gin";
itemDetails2[3] = "2";
item_details.add(itemDetails2);
itemDetails2[0] = "Chapter 3 Sfarsit";
itemDetails2[1] = "ARR03 wda ada hashd ahdau adhauhda uhsd ad audha. adasdauh . adasa";
itemDetails2[2] = "Ritz";
itemDetails2[3] = "10";
item_details.add(itemDetails2);
cXML.createXML(order_details, item_details, "d:\\book2.xml");
}
}
Why when i add to arraylist, the items what i add in it, it dosn't save in it.
i want to use this part
itemDetails2[0] = "Chapter 1 inceput";
itemDetails2[1] = "ARR01";
itemDetails2[2] = "DCL White Arak";
itemDetails2[3] = "1";
item_details.add(itemDetails2);
in a for but i can't becouse it save only the last one.