D
D
dieillusion2016-10-13 13:46:21
Java
dieillusion, 2016-10-13 13:46:21

Collection selection when parsing XML?

I have this xml file:

<?xml version="1.0" encoding="utf-8" ?>
<list>
<file name="a" size="559393"/>
<file name="b" size="1766945"/>
</list>

With the help of a SAX parser, I need to get the attribute values ​​​​(in my case, the name and size values ​​\u200b\u200bof all file tags) and write them to the collection for further program work.
How can I best implement this? For each file tag, create an object with fields name and size and add it all to the ArrayList Object?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2016-10-13
@dieillusion

I would rake in a HashMap in which the values ​​of the name attribute would be the keys , and the values ​​of the size attribute would be .

Map<String, String> attrs = new HashMap<>();

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    String name = attributes.getValue("name");
    String value = attributes.getValue("size");
    attrs.put(name, value);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question