L
L
Lordao2018-03-25 20:59:35
Java
Lordao, 2018-03-25 20:59:35

How to parse certain elements from an XML file?

There is a specific xml file with the following structure:

XML file
<ValCurs>
<Valute ID="R01010">
<NumCode>036</NumCode>
<CharCode>AUD</CharCode>
<Nominal>1</Nominal>
<Name>Австралийский доллар</Name>
<Value>44,1039</Value>
</Valute>
<Valute ID="R01020A">
<NumCode>944</NumCode>
<CharCode>AZN</CharCode>
<Nominal>1</Nominal>
<Name>Азербайджанский манат</Name>
<Value>33,5431</Value>
</Valute>
<Valute ID="R01035">
<NumCode>826</NumCode>
<CharCode>GBP</CharCode>
<Nominal>1</Nominal>
<Name>Фунт стерлингов Соединенного королевства</Name>
<Value>80,6068</Value>
</Valute>
</ValCurs>

You need to pull certain elements using XMLPullParser for a specific attribute.
private void pullDataInArticles(XmlPullParser xmlPullParser) throws XmlPullParserException, IOException {
        int eventType = xmlPullParser.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG && xmlPullParser.getName().equalsIgnoreCase("Valuet")) {
                if (xmlPullParser.getName().equalsIgnoreCase("Value") && xmlPullParser.getAttributeValue(0).equals("R01235")) {
                    String title = xmlPullParser.getText();
                    currentArticle.setTitle_usd(title);
                    Log.d("valueXML",title);
                } else if (xmlPullParser.getName().equalsIgnoreCase("Value") && xmlPullParser.getAttributeValue(0).equals("R01239")){
                    String title = xmlPullParser.getText();
                    currentArticle.setTitle_euro(title);
                    Log.d("valueXML",title);
            } else if (eventType == XmlPullParser.END_TAG && xmlPullParser.getName().equalsIgnoreCase("Valuet")) {
                articles.add(currentArticle);
                currentArticle = new Article();
            }
            eventType = xmlPullParser.next();
        }
        triggerObserver();
        }
    }

But there are certain problems with specifying attributes.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
piatachki, 2018-03-26
@piatachki

File from cbr.ru, apparently - I recognize the unique style of naming nodes ) I
recommend that you turn to JAX* or RestTemplate. You will certainly be surprised how simple and elegant everything is right out of the box.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question