package
jsupport.xml;
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) {
Set set;
Iterator iterator;
try
{
documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilder = documentBuilderFactory.newDocumentBuilder();
document = documentBuilder.newDocument();
Element orderElement = document.createElement(
"order"
);
document.appendChild(orderElement);
Element orderDetailElement = document.createElement(
"orderDetails"
);
orderElement.appendChild(orderDetailElement);
set = order_details.keySet();
iterator = set.iterator();
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++) {
Element itemElement = document.createElement(
"item"
);
orderElement.appendChild(itemElement);
Element itemId;
String itemdetails[] = (String[]) item_details.get(i);
itemId = document.createElement(
"itemid"
);
itemId.appendChild(document.createTextNode(itemdetails[
0
]));
itemElement.appendChild(itemId);
itemId = document.createElement(
"itemName"
);
itemId.appendChild(document.createTextNode(itemdetails[
1
]));
itemElement.appendChild(itemId);
itemId = document.createElement(
"itemQty"
);
itemId.appendChild(document.createTextNode(itemdetails[
2
]));
itemElement.appendChild(itemId);
}
File xmlFile =
new
File(
"C:\\Test.xml"
);
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[
3
];
String itemDetails2[] =
new
String[
3
];
String itemDetails3[] =
new
String[
3
];
order_details.put(
"kitchName"
,
"Chines Kitchen"
);
order_details.put(
"waiterName"
,
"Siridasa"
);
order_details.put(
"tableName"
,
"20"
);
itemDetails[
0
] =
"ARR01"
;
itemDetails[
1
] =
"DCL White Arak"
;
itemDetails[
2
] =
"1"
;
item_details.add(itemDetails);
itemDetails2[
0
] =
"ARR02"
;
itemDetails2[
1
] =
"Lemon Gin"
;
itemDetails2[
2
] =
"2"
;
item_details.add(itemDetails2);
itemDetails3[
0
] =
"ARR03"
;
itemDetails3[
1
] =
"Ritz"
;
itemDetails3[
2
] =
"10"
;
item_details.add(itemDetails3);
cXML.createXML(order_details, item_details);
}
}
No comments:
Post a Comment