package com.prolog.cs.biz.inbound.service.impl;
 
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import com.prolog.cs.biz.inbound.dao.SupervisionCodeMapper;
import com.prolog.cs.biz.inbound.model.SupervisionCode;
import com.prolog.cs.biz.inbound.model.task.TaskInstockDetail;
import com.prolog.wms.base.util.mapper.MapperCustom;
import lombok.SneakyThrows;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
//import org.jdom.Document;
//import org.jdom.Element;
 
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
//import org.w3c.dom.Document;
//import org.w3c.dom.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
 
public class xmlHelper {
	@SneakyThrows
	public static void main(String[] args) {
		xmlHelper xmlHelper =new xmlHelper();
		xmlHelper.createXml2();
	}
	
	public static void createXml2() throws Exception{
		//创建Document对象
		Document document=DocumentHelper.createDocument();
		//创建元素
		Element Document = document.addElement("Document");
		Element events = Document.addElement("Events");
		Element EventElement=events.addElement("Event");
		EventElement.addAttribute("Name", "PurchaseWareHouseIn");
		EventElement.addAttribute("MainAction", "WareHouseIn");
		Element dataField = EventElement.addElement("DataField");

		List<SupervisionCode> taskLineResultList = new ArrayList<>();
		SupervisionCode supervisionCode =new SupervisionCode();
		supervisionCode.setSupervisionCode("0000001");
		supervisionCode.setId("123546");
		supervisionCode.setRefBillId("892025905248079872");
		taskLineResultList.add(supervisionCode);
		SupervisionCode supervisionCode2 =new SupervisionCode();
		supervisionCode2.setSupervisionCode("0000002");
		supervisionCode2.setId("6789655");
		supervisionCode.setRefBillId("892025905248079872");
		taskLineResultList.add(supervisionCode2);
		//遍历集合
		for(SupervisionCode supervisionCode1:taskLineResultList){
			//创建子元素并直接添加进根元素
			Element data = dataField.addElement("Data");
			data.addAttribute("Code", String.valueOf(supervisionCode1.getSupervisionCode()));
			data.addAttribute("CorpOrderID", String.valueOf(supervisionCode1.getId()));
			data.addAttribute("ActDate", "2020-07-16 10:32:06");
			data.addAttribute("ToCorpID", "");
		}
		try {
			// 使用 OutputFormat 进行格式化设置
			OutputFormat format = OutputFormat.createCompactFormat();
			// 设置是否缩进，默认为 false
			format.setIndent(false);
			// 创建 StringWriter 来保存转换后的无格式 XML 字符串
			java.io.StringWriter stringWriter = new java.io.StringWriter();
			// 创建 XMLWriter 对象并设置输出流和格式化参数
			XMLWriter writer = new XMLWriter(stringWriter, format);
			// 将 Document 对象写入到输出流中，并触发转换操作
			writer.write(document);
			String xmlStringWithoutFormatting = stringWriter.toString();
			System.out.println(xmlStringWithoutFormatting);
		} catch (Exception e) {
			e.printStackTrace();
		}

//		//设置生成xml的格式
//		OutputFormat format=OutputFormat.createPrettyPrint();
//		//设置编码格式
//		format.setEncoding("UTF-8");
//		//创建xml字符输出流
//		XMLWriter writer=new XMLWriter(new FileOutputStream(new File("d://"+taskLineResultList.get(0).getRefBillId()+".xml")),format);
//		//设置是否转义，默认使用转义字符
//		writer.setEscapeText(false);
//		//写出Document对象
//		writer.write(document);
//		//关闭流
//		writer.close();


	}
}