Parsing Xml with NodeList and DocumentBuilder

You have a bit of trouble parsing xml with dom and DocumentBuilder. I can get it to work, but I think I am a little confused with all child nodes, etc.

Here's the XML I'm working with:

<?xml version="1.0" encoding="utf-8"?>
<LabTests>
    <LabTest type="specialty" name="Anti-FXa activity" id="antiFXa" order="16">
        <values unit="U/mL" default="N/A">
            <value type="increased" val="0">
                <conditions>
                    <condition>Heparin effect</condition>
                </conditions>
            </value>
            <value type="normal" val="">
                <conditions></conditions>
            </value>
            <value type="decreased" val="">
                <conditions></conditions>
            </value>
        </values>
    </LabTest>
    <LabTest type="general" name="aPTT" id="aPTT" order="">
        <values unit="secs" default="N/A">
            <value type="increased" val="">
                <conditions>
                    <condition>Acquired hemophilia</condition>
                    <condition>Acquired vWD</condition>
                    <condition>DIC</condition>
                    <condition>Dysfibrinogenemia</condition>
                    <condition>FI deficiency</condition>
                    <condition>FII deficiency</condition>
                    <condition>FII/IIa inhibitors</condition>
                    <condition>FIX deficiency</condition>
                    <condition>FIX inhibitors</condition>
                    <condition>FV deficiency</condition>
                    <condition>FV inhibitors</condition>
                    <condition>FVIII deficiency</condition>
                    <condition>FX deficiency</condition>
                    <condition>FX inhibitors</condition>
                    <condition>FXI deficiency</condition>
                    <condition>FXI inhibitors</condition>
                    <condition>FXII deficiency</condition>
                    <condition>FXII inhibitors</condition>
                    <condition>Heparin effect</condition>
                    <condition>Liver disease effect</condition>
                    <condition>Lupus anticoagulant</condition>
                    <condition>Monoclonal gammopathy</condition>
                    <condition>Vitamin K deficiency</condition>
                    <condition>vWD type 1</condition>
                    <condition>vWD type 2</condition>
                    <condition>vWD type 3</condition>
                    <condition>Warfarin effect</condition>
                </conditions>
            </value>
            <value type="normal" val="">
                <conditions>
                    <condition>DIC</condition>
                    <condition>Dysfibrinogenemia</condition>
                    <condition>FVII deficiency</condition>
                    <condition>FXIII deficiency</condition>
                    <condition>FVII inhibitors</condition>
                    <condition>Liver disease effect</condition>
                    <condition>Lupus anticoagulant</condition>
                    <condition>Monoclonal gammopathy</condition>
                    <condition>Vitamin K deficiency</condition>
                    <condition>vWD type 1</condition>
                    <condition>vWD type 2</condition>
                    <condition>vWD type 3</condition>
                    <condition>Warfarin effect</condition>
                </conditions>
            </value>
            <value type="decreased" val="">
                <conditions>
                    <condition>DIC</condition>
                </conditions>
            </value>
        </values>
    </LabTest>
</LabTests>

      

what I'm trying to do is grab each element LabTest

and, within each of those elements, grab the elements value

(and grab the value type

) and within value

, grab all the elements condition

.

In the end, I need something like Map<String, HashMap<String, ArrayList<String>>

where String

- is the name LabTest

, and HashMap

uses type

(eg decreased

, increased

etc.) for the key, and then fills the ArrayList conditions for this type value

.

Confusion enough?

Basically, I just need an example of how I will quote and capture each LabTest with its "value" elements and each "condition" element under those "significant" elements.

+9


source to share


2 answers


This should work as you described:



DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();

Document doc = builder.parse("input.xml");

NodeList labTestList = doc.getElementsByTagName("LabTest");
for (int i = 0; i < labTestList.getLength(); ++i)
{
    Element labTest = (Element) labTestList.item(i);
    String labTestType = labTest.getAttribute("type");

    NodeList valueList = labTest.getElementsByTagName("value");
    for (int j = 0; j < valueList.getLength(); ++j)
    {
        Element value = (Element) valueList.item(j);
        String valueType = value.getAttribute("type");

        NodeList conditionList = value.getElementsByTagName("condition");
        for (int k = 0; k < conditionList.getLength(); ++k)
        {
            Element condition = (Element) conditionList.item(k);
            String conditionText = condition.getFirstChild().getNodeValue();
        }
    }
}

      

+32


source


Also note that there is a nice notion of unescaping XML node values ​​in a nodelist when parsing.



0


source







All Articles