how to identify for the xml end tag while parsing using DOM?

◇◆丶佛笑我妖孽 提交于 2020-01-25 13:06:09

问题


I want to identify end tag of each element in the xml file.

Example :

<Bank>
    <Account>
        <Id1>1001</Id1>
        <Name1>Sony Corporation</Name1>
        <Amt1>1000000</Amt1>
        <age>23</age>
   </Account>
   <Account>
        <Id2>1002</Id2>
        <Name2>Sony Corporation</Name2>
        <Amt2>1000000</Amt2>
   </Account>
</Bank>

Now I want to identify the

</Account> 

tag is closing.

How to identify such a end tag while parsing xml file using DOM?


回答1:


DOM is at a higher level of abstraction than this. It doesn't expose start and end tags, it exposes nodes, presented as a tree structure. If you tell us what you are trying to achieve (the problem you are trying to solve, not the way you are trying to solve it) then we could suggest the right approach.




回答2:


NodeList nl = doc.getElementsByTagName(Account);

        // looping through all song nodes <song>
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            // adding each child node to HashMap key => value
            map.put(Id1, parser.getValue(e, Id1));
            map.put(Name1, parser.getValue(e, Name1));
            map.put(Amt1, parser.getValue(e, Amt1));
            map.put(age, parser.getValue(e, age));

            // adding HashList to ArrayList
            songsList.add(map);
        }


来源:https://stackoverflow.com/questions/37614271/how-to-identify-for-the-xml-end-tag-while-parsing-using-dom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!