读取XML到Java地图容器

时间:2015-12-06 16:00:56

标签: java xml hashmap

Tneed帮助以下列格式将XML内容读取到Java Map容器​​中。 (Java初学者问题)

<Tests xmlns="http://...managerxml">      
<Test id="10500510001">
        <StaticData>
        <systemID>sysid</systemID>
        <failureSeverity>0</failureSeverity>
        <destination>5</destination>
        <ServiceMessage>
                <classA_message>CLASSA_ID_10500510001</classA_message>
                <classM_message>CLASSM_ID_10500510001</classM_message>
                <classC_message></classC_message>
         </ServiceMessage>
         <userMessage></userMessage>
        </StaticData>
    </Test>

我了解如何从列表中访问所有测试的测试ID,但是不了解如何访问每个测试节点的静态数据子节点和属性。 需要将属性与test id一起构造到结构映射: 地图。在此先感谢您的帮助!!

这是我到目前为止所做的:

File fXmlFile = new File("TestManager.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);        
doc.getDocumentElement().normalize();

System.out.println("Root element :" +doc.getDocumentElement().getNodeName());

NodeList nList = doc.getElementsByTagName("Test");

System.out.println("----------------------------");

for (int temp = 0; temp < nList.getLength(); temp++) {

    Node nNode = nList.item(temp);
    System.out.println("\nCurrent Element :" + nNode.getNodeName());

    if (nNode.getNodeType() == Node.ELEMENT_NODE) {

        Element eElement = (Element) nNode;
        System.out.println("Indicator id : " + eElement.getAttribute("id"));
    }
}

0 个答案:

没有答案