Hy, I'm trying to read from a xml file and save it's data in an ArrayList then a HashMap

my xml file looks like this:

        <component id="door1">
                <propertie name="allignment" value="horizontal" />
                <propertie name="orientation" value="top" />
                <propertie name="isOpened" value="false" />
                <propertie name="isLocked" value="true" />

        <component id="window1">
                <propertie name="allignment" value="vertical" />
                <propertie name="orientation" value="left" />
                <propertie name="isOpened" value="false" />

i read the bounds for each element and i store it in a HashMap, identified by an order number, it looks like this:

{0=[5, 15, 5, 10], 1=[5, 15, 5, 10]}

my problem is with the "properties" tag, because with the following code it attributes for each id everything it finds in the <propertie> tag and i need, of course, only the specific attributes of an "id"

public HashMap<Integer, ArrayList<String>> readComponentsProperties(
			File inFile) throws Exception {
		HashMap<Integer, ArrayList<String>> map = new HashMap<Integer, ArrayList<String>>();
		final ArrayList<String> list = new ArrayList<String>();
		int j = 0, i = 0, k = 0;
		String id = null, name = null, value = null;

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document document = builder.parse(inFile);

		NodeList idNode = document.getElementsByTagName("component");
		for (k = 0; k < idNode.getLength(); k++) {
			NodeList component = document.getElementsByTagName("propertie");

			for (i = 0; i < idNode.getLength(); i++) {
				for (j = 0; j < component.getLength(); j++) {

					Element el = (Element) idNode.item(i);
					id = el.getAttribute("id");
					System.out.println("the id: " + id);

					Element elem = (Element) component.item(j);
					name = elem.getAttribute("name");
					System.out.println("name: " + name);
					value = elem.getAttribute("value");
					System.out.println("value: " + value);

				list.add(id + " " + name + " " + value);
		map.put(i, list);
		// System.out.println("map: " + map);

		return null;


it's the first time i work with xml files and i don't know yet what is with the parent and child nodes, i don't know how to use them

Here is easy to read book Processing XML with Java, have look on JDOM which is very easy to use (after that you will have no problem to find some better/more suitable library)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.