用NodeList和DocumentBuilder解析Xml [英] parsing Xml with NodeList and DocumentBuilder
问题描述
这是我正在使用的XML: / p>
<?xml version =1.0encoding =utf-8?>
< LabTests>
< LabTest type =specialtyname =Anti-FXa activityid =antiFXaorder =16>
< values unit =U / mLdefault =N / A>
< value type =increaseval =0>
<条件>
< condition>肝素效应< / condition>
< / conditions>
< / value>
< value type =normalval =>
< conditions>< / conditions>
< / value>
< value type =reducedval =>
< conditions>< / conditions>
< / value>
< / values>
< / LabTest>
< LabTest type =generalname =aPTTid =aPTTorder =>
< values unit =secsdefault =N / A>
< value type =increaseval =>
<条件>
< condition>获得性血友病< / condition>
< condition>获取vWD< / condition>
< condition> DIC< / condition>
<条件>血液纤维蛋白原血症< /条件>
< condition> FI deficiency< / condition>
< condition> FII缺陷< / condition>
<条件> FII / IIa抑制剂< / condition>
< condition> FIX缺陷< / condition>
<条件> FIX抑制剂< / condition>
<条件> FV缺陷< / condition>
<条件> FV抑制剂< / condition>
<条件> FVIII缺陷< / condition>
< condition> FX缺陷< / condition>
< condition> FX抑制剂< / condition>
< condition> FXI缺陷< / condition>
< condition> FXI抑制剂< / condition>
<条件> FXII缺陷< / condition>
<条件> FXII抑制剂< / condition>
< condition>肝素效应< / condition>
< condition>肝病效应< / condition>
<条件>狼疮抗凝剂< / condition>
<条件>单克隆丙种球蛋白病< /条件>
< condition>维生素K缺乏< / condition>
< condition> vWD type 1< / condition>
< condition> vWD type 2< / condition>
< condition> vWD类型3< / condition>
< condition>华法林效果< / condition>
< / conditions>
< / value>
< value type =normalval =>
<条件>
< condition> DIC< / condition>
<条件>血液纤维蛋白原血症< /条件>
<条件> FVII缺陷< / condition>
<条件> FXIII缺陷< / condition>
<条件> FVII抑制剂< / condition>
< condition>肝病效应< / condition>
<条件>狼疮抗凝剂< / condition>
<条件>单克隆丙种球蛋白病< /条件>
< condition>维生素K缺乏< / condition>
< condition> vWD type 1< / condition>
< condition> vWD type 2< / condition>
< condition> vWD类型3< / condition>
< condition>华法林效果< / condition>
< / conditions>
< / value>
< value type =reducedval =>
<条件>
< condition> DIC< / condition>
< / conditions>
< / value>
< / values>
< / LabTest>
< / LabTests>
我想要做的是抓住每个 LabTest
元素,并且在每个元素中,抓住值
元素(并获取类型的值
),并且在值
元素内,抓住所有条件
元素。
最后,我想要一个类似于 Map< String,HashMap< String,ArrayList< String>>
,其中 String
是 LabTest
名称和 HashMap
使用类型
(例如减少
,增加
等),然后填充ArrayList与值
类型的条件。
令人困惑?
基本上,我只需要一个例子,我想,如何循环遍历并使用其值元素抓取每个LabTest,并且每个条件元素在这些价值元素之下。
应该按照你所描述的方式工作:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
文档doc = builder.parse(input.xml);
NodeList labTestList = doc.getElementsByTagName(LabTest); (int i = 0; i< labTestList.getLength(); ++ i)
{
元素labTest =(Element)labTestList.item(i);
String labTestType = labTest.getAttribute(type);
NodeList valueList = labTest.getElementsByTagName(value); (int j = 0; j< valueList.getLength(); ++ j)
{
元素值=(Element)valueList.item(j);
String valueType = value.getAttribute(type);
NodeList conditionList = value.getElementsByTagName(condition); (int k = 0; k< conditionList.getLength(); ++ k)
{
元素条件=(元素)conditionList.item(k);
String conditionText = condition.getFirstChild()。getNodeValue();
}
}
}
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.
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 hold of each LabTest
element and, within each of those elements, grab hold of the value
elements (and grab the value of type
) and, within the value
element, grab hold of all of the condition
elements.
In the end, I want something like a Map<String, HashMap<String, ArrayList<String>>
, where the String
is the LabTest
name and the HashMap
uses the type
(e.g. decreased
, increased
, etc) for the key and then fills up the ArrayList with the conditions for that value
type.
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.
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();
}
}
}
这篇关于用NodeList和DocumentBuilder解析Xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!