Search code examples
javaxmldomxml-parsingdomparser

Java DOM XML Parsing How to walk through multiple node levels


I have the following xml structure

<clinic>
    <category>
        <employees>
            <medic>
                <medic_details>
                    <medic_name />
                    <medic_address />
                </medic_details>
                <pacients>
                    <pacient>
                        <pacient_details>
                            <pacient_name> ...
                            <pacient_address> ...             
                        </pacient_details>
                        <diagnostic>
                            <disease>
                                <disease_name>Disease</disease_name>
                                <treatment>Treatment</treatment>
                            </disease>
                            <disease>
                                <disease_name>Disease</disease_name>
                                <treatment>Treatment</treatment>
                            </disease>
                        </diagnostic>
                    </pacient>
                </pacients>
            <medic>
        </employees>
    </category>
</clinic>

I have a JTextArea where I want to show information from the xml file. For example, for showing each medic, with its name, adress, and treating pacients with their respective names, i have the following code:

NodeList medicNList = doc.getElementsByTagName("medic");

for (int temp = 0; temp < medicNList.getLength(); temp++) {

        Node medicNode = medicNList.item(temp);
        Element eElement = (Element) medicNode;

        area.append("\n");
        area.append("Medic Name : " + getTagValue("medic_name", eElement) + "\n");
        area.append("Medic Address : " + getTagValue("medic_address", eElement) + "\n");
        area.append("\n");
        area.append("Pacients : \n");
        area.append("Pacient Name : " + getTagValue("pacient_name", eElement) + "\n");
        area.append("Pacient Name : " + getTagValue("pacient_address", eElement) + "\n");

        }

My question is, if i want to have more than 1 disease per pacient, how do I display all of the diseases for each pacient? I don't know how to "walk" to the diagnostic node for each pacient and showing the relevant data inside


Solution

  • Your code looks incorrect as it is. You currently have multiple pacient (patients) per medic so you should be iterating the list of patients for each medic.

    Then iterate diseases for each patient. You need to use the getElementsByTagName method for each nesting in the XML. Plus you need to skip over the pluralised elements such as <pacients>.

    I would suggest you use an XPath library instead as it can make the code a lot easier to read. There are plenty of good ones out there. I would recommend jaxen