
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
import java.util.ArrayList;


public class ReadXML 
{

	
	public static final String xmlFilePath = "D:\\xmlfile.xml";
	
	ArrayList<String> tmp_tree_datas = new ArrayList<String>();

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

			File xmlFile = new File(xmlFilePath);

			DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();

			DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

			Document doc = documentBuilder.parse(xmlFile);

			doc.getDocumentElement().normalize();

			System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

			NodeList nodeList = doc.getElementsByTagName("employee");

			System.out.println("===============================================================");

			//do this the old way, because nodeList is not iterable
			for (int itr = 0; itr < nodeList.getLength(); itr++) {

				Node node = nodeList.item(itr);

				System.out.println("\nNode Name :" + node.getNodeName());

				if (node.getNodeType() == Node.ELEMENT_NODE) {

					Element eElement = (Element) node;

					System.out.println("Employee id : "	+ eElement.getAttribute("id"));
					System.out.println("First Name : "	+ eElement.getElementsByTagName("firstname").item(0).getTextContent());
					System.out.println("Last Name : "	+ eElement.getElementsByTagName("lastname").item(0).getTextContent());
					System.out.println("Email : "		+ eElement.getElementsByTagName("email").item(0).getTextContent());
					System.out.println("Department : "	+ eElement.getElementsByTagName("department").item(0).getTextContent());
					System.out.println("Salary : "		+ eElement.getElementsByTagName("salary").item(0).getTextContent());

				}
			}
		} 
		
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
	
	/******************************************************************************/
	
	public ArrayList<String> Tree_ReadXML(String path)
	{
		try 
		{

			File xmlFile = new File(path); // xmlFilePath

			DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();

			DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

			Document doc = documentBuilder.parse(xmlFile);

			doc.getDocumentElement().normalize();

			System.out.println("Root :" + doc.getDocumentElement().getNodeName());

			NodeList nodeList = doc.getElementsByTagName("elem");
			
			for (int itr = 0; itr < nodeList.getLength(); itr++) 
			{

				Node node = nodeList.item(itr);

				System.out.println("\n Name :" + node.getNodeName());

				if (node.getNodeType() == Node.ELEMENT_NODE) 
				{
					Element eElement = (Element) node;
					
					System.out.println("name : "	+ eElement.getElementsByTagName("name").item(0).getTextContent());
					System.out.println("val1 : "	+ eElement.getElementsByTagName("val1").item(0).getTextContent());
					System.out.println("val2 : "	+ eElement.getElementsByTagName("val2").item(0).getTextContent());
					
					String name = eElement.getElementsByTagName("name").item(0).getTextContent();
					String val1 = eElement.getElementsByTagName("val1").item(0).getTextContent();
					String val2 = eElement.getElementsByTagName("val2").item(0).getTextContent();
					
				    if(val1.equals("") && val2.equals(""))
				    {
				    	tmp_tree_datas.add(name);
				    }
				    else
				    {
				    	tmp_tree_datas.add(name + "-" + val1 + "-" + val2);
				    }
					
				}
			}
		} 
		
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		
		return(tmp_tree_datas);
	}
	
}



