package br.com.waslleysouza.tt;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class XMLHandler {
public XMLHandler(){
}
public String createXML(){
String xmlStr = null;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
document.setXmlVersion("1.0");
Element scRkdInfo = document.createElement("scRkdInfo");
document.appendChild(scRkdInfo);
Element agentid = document.createElement("agentid");
agentid.setTextContent("1");
scRkdInfo.appendChild(agentid);
Element billtype = document.createElement("billtype");
billtype.setTextContent("1");
scRkdInfo.appendChild(billtype);
Element iodate = document.createElement("iodate");
iodate.setTextContent("2016-01-01");
scRkdInfo.appendChild(iodate);
Element orgid = document.createElement("orgid");
orgid.setTextContent("1");
scRkdInfo.appendChild(orgid);
Element warehouse = document.createElement("warehouse");
warehouse.setTextContent("1");
scRkdInfo.appendChild(warehouse);
/*
for(int i=0;i<2000;i++)
{
Element i =ocument.createElement("detail");
}
for(int i =0; i<=2000;i++){
Element i = document.createElement("aaa"+i);
} */
Element detail;
for(int i=0;i<20000;i++){
int j=0;
String str1="big"+i;
String str2="ssss"+j;
detail = document.createElement("detail");
Element lotno = document.createElement("lotno");
lotno.setTextContent(str1);
detail.appendChild(lotno);
Element matid = document.createElement("matid");
matid.setTextContent("010103");
detail.appendChild(matid);
Element quantity = document.createElement("quantity");
quantity.setTextContent("1");
detail.appendChild(quantity);
Element quantityunit = document.createElement("quantityunit");
quantityunit.setTextContent("0001083812");
detail.appendChild(quantityunit);
//detail标记的子标记xdetail
Element xdetail1 = document.createElement("xdetail");
//xdetail下字标记<remark>
Element remark1 = document.createElement("remark");
remark1.setTextContent("11aa"+str1);
xdetail1.appendChild(remark1);
detail.appendChild(xdetail1);
//detail标记的子标记xdetail
Element xdetail2 = document.createElement("xdetail");
//xdetail下字标记<remark>
Element remark2 = document.createElement("remark");
remark2.setTextContent("22aa"+str1);
xdetail2.appendChild(remark2);
detail.appendChild(xdetail2);
//detail标记的子标记xdetail
Element xdetail3 = document.createElement("xdetail");
Element remark3 = document.createElement("remark");
remark3.setTextContent("33aa"+str1);
xdetail3.appendChild(remark3);
detail.appendChild(xdetail3);
scRkdInfo.appendChild(detail);
}
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transFormer = transFactory.newTransformer();
DOMSource domSource = new DOMSource(document);
//export string
ByteArrayOutputStream bos = new ByteArrayOutputStream();
transFormer.transform(domSource, new StreamResult(bos));
xmlStr = bos.toString();
//-------
//save as file
File file = new File("TelePhone.xml");
if(!file.exists()){
file.createNewFile();
}
FileOutputStream out = new FileOutputStream(file);
StreamResult xmlResult = new StreamResult(out);
transFormer.transform(domSource, xmlResult);
//--------
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return xmlStr;
}
public void parserXML(String strXML){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
StringReader sr = new StringReader(strXML);
InputSource is = new InputSource(sr);
Document doc = builder.parse(is);
Element rootElement = doc.getDocumentElement();
NodeList phones = rootElement.getElementsByTagName("type");
for (int i = 0; i < phones.getLength(); i++) {
Node type = phones.item(i);
String phoneName = ((Element)type).getAttribute("name");
System.out.println("Phone name = "+phoneName);
NodeList properties = type.getChildNodes();
for (int j = 0; j < properties.getLength(); j++) {
Node property = properties.item(j);
String nodeName = property.getNodeName();
if (nodeName.equals("price")) {
String price=property.getFirstChild().getNodeValue();
System.out.println("price="+price);
} else if (nodeName.equals("operator")) {
String operator=property.getFirstChild().getNodeValue();
System.out.println("operator="+operator);
}
}
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
XMLHandler handler = new XMLHandler();
String xml = handler.createXML();
System.out.println(xml);
handler.parserXML(xml);
}
}
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class XMLHandler {
public XMLHandler(){
}
public String createXML(){
String xmlStr = null;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
document.setXmlVersion("1.0");
Element scRkdInfo = document.createElement("scRkdInfo");
document.appendChild(scRkdInfo);
Element agentid = document.createElement("agentid");
agentid.setTextContent("1");
scRkdInfo.appendChild(agentid);
Element billtype = document.createElement("billtype");
billtype.setTextContent("1");
scRkdInfo.appendChild(billtype);
Element iodate = document.createElement("iodate");
iodate.setTextContent("2016-01-01");
scRkdInfo.appendChild(iodate);
Element orgid = document.createElement("orgid");
orgid.setTextContent("1");
scRkdInfo.appendChild(orgid);
Element warehouse = document.createElement("warehouse");
warehouse.setTextContent("1");
scRkdInfo.appendChild(warehouse);
/*
for(int i=0;i<2000;i++)
{
Element i =ocument.createElement("detail");
}
for(int i =0; i<=2000;i++){
Element i = document.createElement("aaa"+i);
} */
Element detail;
for(int i=0;i<20000;i++){
int j=0;
String str1="big"+i;
String str2="ssss"+j;
detail = document.createElement("detail");
Element lotno = document.createElement("lotno");
lotno.setTextContent(str1);
detail.appendChild(lotno);
Element matid = document.createElement("matid");
matid.setTextContent("010103");
detail.appendChild(matid);
Element quantity = document.createElement("quantity");
quantity.setTextContent("1");
detail.appendChild(quantity);
Element quantityunit = document.createElement("quantityunit");
quantityunit.setTextContent("0001083812");
detail.appendChild(quantityunit);
//detail标记的子标记xdetail
Element xdetail1 = document.createElement("xdetail");
//xdetail下字标记<remark>
Element remark1 = document.createElement("remark");
remark1.setTextContent("11aa"+str1);
xdetail1.appendChild(remark1);
detail.appendChild(xdetail1);
//detail标记的子标记xdetail
Element xdetail2 = document.createElement("xdetail");
//xdetail下字标记<remark>
Element remark2 = document.createElement("remark");
remark2.setTextContent("22aa"+str1);
xdetail2.appendChild(remark2);
detail.appendChild(xdetail2);
//detail标记的子标记xdetail
Element xdetail3 = document.createElement("xdetail");
Element remark3 = document.createElement("remark");
remark3.setTextContent("33aa"+str1);
xdetail3.appendChild(remark3);
detail.appendChild(xdetail3);
scRkdInfo.appendChild(detail);
}
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transFormer = transFactory.newTransformer();
DOMSource domSource = new DOMSource(document);
//export string
ByteArrayOutputStream bos = new ByteArrayOutputStream();
transFormer.transform(domSource, new StreamResult(bos));
xmlStr = bos.toString();
//-------
//save as file
File file = new File("TelePhone.xml");
if(!file.exists()){
file.createNewFile();
}
FileOutputStream out = new FileOutputStream(file);
StreamResult xmlResult = new StreamResult(out);
transFormer.transform(domSource, xmlResult);
//--------
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return xmlStr;
}
public void parserXML(String strXML){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
StringReader sr = new StringReader(strXML);
InputSource is = new InputSource(sr);
Document doc = builder.parse(is);
Element rootElement = doc.getDocumentElement();
NodeList phones = rootElement.getElementsByTagName("type");
for (int i = 0; i < phones.getLength(); i++) {
Node type = phones.item(i);
String phoneName = ((Element)type).getAttribute("name");
System.out.println("Phone name = "+phoneName);
NodeList properties = type.getChildNodes();
for (int j = 0; j < properties.getLength(); j++) {
Node property = properties.item(j);
String nodeName = property.getNodeName();
if (nodeName.equals("price")) {
String price=property.getFirstChild().getNodeValue();
System.out.println("price="+price);
} else if (nodeName.equals("operator")) {
String operator=property.getFirstChild().getNodeValue();
System.out.println("operator="+operator);
}
}
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
XMLHandler handler = new XMLHandler();
String xml = handler.createXML();
System.out.println(xml);
handler.parserXML(xml);
}
}