使用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>>
之类的内容,其中String
是LabTest
名称而HashMap
使用type
(例如{键的{1}},decreased
等)然后使用increased
类型的条件填充ArrayList。
足够困惑?
基本上,我只需要一个例子,我想,如何循环并抓住每个LabTest及其“value”元素,以及这些“value”元素下的每个“condition”元素。
答案 0 :(得分:34)
这应该像你描述的那样工作:
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();
}
}
}