0

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:

<components>
        <component id="door1">
            <bounds>
                <left>10</left>
                <top>5</top>
                <width>25</width>
                <height>10</height>
            </bounds>
            <properties>
                <propertie name="allignment" value="horizontal" />
                <propertie name="orientation" value="top" />
                <propertie name="isOpened" value="false" />
                <propertie name="isLocked" value="true" />
            </properties>
        </component>

        <component id="window1">
            <bounds>
                <left>5</left>
                <top>15</top>
                <width>5</width>
                <height>10</height>
            </bounds>
            <properties>
                <propertie name="allignment" value="vertical" />
                <propertie name="orientation" value="left" />
                <propertie name="isOpened" value="false" />
            </properties>
        </component>
</components>

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);
		document.getDocumentElement().normalize();

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

			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);
				System.out.println(list);
			}
		}
		map.put(i, list);
		list.clear();
		// 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

2
Contributors
1
Reply
4
Views
6 Years
Discussion Span
Last Post by peter_budo
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.