-->

Java DOM XML Parsing How to walk through multiple

2019-08-29 20:11发布

问题:

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

回答1:

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



回答2:

I would give htmlcleaner a try.

HTMLCleaner is Java library used to safely parse and transform any HTML found on web to well-formed XML. It is designed to be small, fast, flexible and independant. HtmlCleaner may be used in java code, as command line tool or as Ant task. Result of parsing is lightweight document object model which can easily be transformed to standards like DOM or JDom, or serialized to XML output in various ways (compact, pretty printed and so on).

You can use XPath with htmlcleaner to get contents within xml tags.Here is a nice
example Xpath Example