read xml file java code example

Example 1: java read xml

String dir = System.getProperty("user.dir");
File inputFile = new File(dir + File.separator + "dats.xml");
DocumentBuilderFactory dbFactory =
	DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
/*
Elimina nodos vacíos y combina adyacentes en caso de que los
hubiera
*/

Example 2: how to read an xml file

import xmltodict as xtd
import os

xml_file_content = ""

catalog_dir = os.chdir('directory')
catalog_dir_array = os.listdir(catalog_dir)

if("nameOfFile.xml" in catalog_dir_array):
    catalog_file_content = open("nameOfFile.xml", "r").read()

catalog_file_content_dict = xtd.parse(catalog_file_content)