简体   繁体   中英

Get XML node attribute value Java

I have an XML Document which contains XML elements containing attributes from which I would like to get it's value and store it in a Hashmap.

Example:

<?xml version="1.0" encoding="UTF-8"?>
<Nodes>
    <Node name="test1">
        <mou>
            <line3>hello</line3>
        </mou>
    </Node>

    <Node name="test2">
        <mou>
            <line3>hello</line3>
        </mou>
    </Node>

    <InputNode name="Chance">
        <Test>
            <RoundTo>100</RoundTo>
        </Test>
    </InputNode>
    <InputNode name="total" />
</Nodes>

I'd like to parse this xml and retrieve the values attributes from all the elements named 'Node' and store it in a map object. So from the example above I would get

[name=test1,name=test2]

The problem is with maps the keys must be unique. How can I accomplish my goal using Java?

Sorry, I got your question wrong. You can parse the xml already. You want to store it in a map. You can create objects out of all the nodes. Node { nodeName, attribute1, attribute2} then you can save it as a map by {key,value} = {name, nodeObj}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM