Java 使用 NodeList 和 DocumentBuilder 解析 XML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6604876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:08:30  来源:igfitidea点击:

parsing Xml with NodeList and DocumentBuilder

javaxmldom

提问by LuxuryMode

Having a bit of trouble parsing xml with dom and DocumentBuilder. I'm able to get it working, but I guess I get a bit confused with all the child nodes, etc.

使用 dom 和 DocumentBuilder 解析 xml 时遇到了一些麻烦。我能够让它工作,但我想我对所有子节点等有点困惑。

Here's the XML I'm working with:

这是我正在使用的 XML:

<?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 hold of each LabTestelement and, within each of those elements, grab hold of the valueelements (and grab the value of type) and, within the valueelement, grab hold of all of the conditionelements.

我想要做的是抓住每个LabTest元素,并在每个元素中抓住value元素(并抓住 的值type),并在value元素内抓住所有condition元素。

In the end, I want something like a Map<String, HashMap<String, ArrayList<String>>, where the Stringis the LabTestname and the HashMapuses the type(e.g. decreased, increased, etc) for the key and then fills up the ArrayList with the conditions for that valuetype.

最后,我想是这样一个Map<String, HashMap<String, ArrayList<String>>,其中StringLabTest名称和HashMap使用type(例如decreasedincreased等)为重点,然后填补了该条件的ArrayList的value类型。

Confusing enough?

够混乱吗?

Basically, I just need an example, I think, of how to loop through and grab each LabTest with its "value" elements, and each of the "condition" elements under those "value" elements.

基本上,我只需要一个例子,我想,如何循环并抓住每个 LabTest 及其“价值”元素,以及这些“价值”元素下的每个“条件”元素。

采纳答案by Grzegorz Szpetkowski

That 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();
        }
    }
}