

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
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.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;


public class WriteXML 
{
	
	public static final String xmlFilePath = "D:\\xmlfile.xml";

	public WriteXML()
	{
		
	}

	// https://examples.javacodegeeks.com/core-java/xml/parsers/documentbuilderfactory/create-xml-file-in-java-using-dom-parser-example/
	public void aaaWirteXML()
	{
		
		try 
		{

			DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();
			Document document = documentBuilder.newDocument();

			// root element
			Element root = document.createElement("VERIF_PLAN");
			document.appendChild(root);

			// employee element
			Element employee = document.createElement("employee");

			root.appendChild(employee);

			// set an attribute to staff element
			Attr attr = document.createAttribute("id");
			attr.setValue("10");
			employee.setAttributeNode(attr);

			//you can also use staff.setAttribute("id", "1") for this

			// firstname element
			Element firstName = document.createElement("firstname");
			firstName.appendChild(document.createTextNode("James"));
			employee.appendChild(firstName);

			// lastname element
			Element lastname = document.createElement("lastname");
			lastname.appendChild(document.createTextNode("Harley"));
			employee.appendChild(lastname);

			// email element
			Element email = document.createElement("email");
			email.appendChild(document.createTextNode("james@example.org"));
			employee.appendChild(email);

			// department elements
			Element department = document.createElement("department");
			department.appendChild(document.createTextNode("Human Resources"));
			employee.appendChild(department);

			// department elements
			Element salary = document.createElement("salary");
			salary.appendChild(document.createTextNode("123456.78"));
			employee.appendChild(salary);
			
			// create the xml file
			//transform the DOM Object to an XML File
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource domSource = new DOMSource(document);
			StreamResult streamResult = new StreamResult(new File(xmlFilePath));

			// If you use
			// StreamResult result = new StreamResult(System.out);
			// the output will be pushed to the standard output ...
			// You can use that for debugging 

			transformer.transform(domSource, streamResult);

			System.out.println("Done creating XML File");

		} 
		
		catch (ParserConfigurationException pce) 
		{
			pce.printStackTrace();
		} 
		
		catch (TransformerException tfe) 
		{
			tfe.printStackTrace();
		}
		
	}
	
	
	
	public void xxxWirteXML(String store[])
	{
		
		try 
		{

			DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();
			Document document = documentBuilder.newDocument();
			
			Element root = document.createElement("VERIF_PLAN");
			document.appendChild(root);
			
			for(int i = 0; i < store.length ; i++) 
            {
				Element element = document.createElement("elem");
				root.appendChild(element);
	
				boolean retval = store[i].contains("-");
				if(retval == false )
				{
				    Element aaa = document.createElement("name");
					aaa.appendChild(document.createTextNode(store[i]));
					element.appendChild(aaa);
				}
				else
				{	
				    String[] parts = store[i].split("-");
				     
				    Element aaa = document.createElement("name");
					aaa.appendChild(document.createTextNode(parts[0]));
					element.appendChild(aaa);
			
					Element bbb = document.createElement("val1");
					bbb.appendChild(document.createTextNode(parts[1]));
					element.appendChild(bbb);
					
					Element ccc = document.createElement("val2");
					ccc.appendChild(document.createTextNode(parts[2]));
					element.appendChild(ccc);
				}
				
             }	
			
		
			//transform the DOM Object to an XML File
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource domSource = new DOMSource(document);
			StreamResult streamResult = new StreamResult(new File(xmlFilePath));

			transformer.transform(domSource, streamResult);

			System.out.println("Done creating XML File");

		} 
		
		catch (ParserConfigurationException pce) 
		{
			pce.printStackTrace();
		} 
		
		catch (TransformerException tfe) 
		{
			tfe.printStackTrace();
		}
		
	}
	
}
