gpt4 book ai didi

java - 使用 NodeList 和 DocumentBuilder 解析 Xml

转载 作者:搜寻专家 更新时间:2023-10-30 21:37:34 24 4
gpt4 key购买 nike

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

这是我正在使用的 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>

我想做的是捕获每个 LabTest元素,并在每个元素中捕获 value元素(并获取 type 的值)并且在 value 中元素,捕获所有 condition元素。

最后,我想要类似 Map<String, HashMap<String, ArrayList<String>> 的东西, 其中StringLabTest名称和 HashMap使用 type (例如 decreasedincreased 等)作为键,然后用 value 的条件填充 ArrayList类型。

够糊涂了吧?

基本上,我认为我只需要一个示例,说明如何循环并获取每个 LabTest 及其“值”元素,以及这些“值”元素下的每个“条件”元素。

最佳答案

这应该像您描述的那样工作:

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

关于java - 使用 NodeList 和 DocumentBuilder 解析 Xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6604876/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com