Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
xml extract currency
#1
Hi! I have the xml below, how can I extract the currency who have this value multiplier="100"?

<?xml version="1.0" encoding="utf-8"?>
<DataSet xmlns="http://www.bnr.ro/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bnr.ro/xsd nbrfxrates.xsd">
	<Header>
		<Publisher>National Bank of Romania</Publisher>
		<PublishingDate>2022-04-29</PublishingDate>
		<MessageType>DR</MessageType>
	</Header>
	<Body>
		<Subject>Reference rates</Subject>
		<OrigCurrency>RON</OrigCurrency>
		<Cube date="2022-04-29">
			<Rate currency="AED">1.2734</Rate>
			<Rate currency="AUD">3.3490</Rate>
			<Rate currency="BGN">2.5298</Rate>
			<Rate currency="BRL">0.9470</Rate>
			<Rate currency="CAD">3.6702</Rate>
			<Rate currency="CHF">4.8290</Rate>
			<Rate currency="CNY">0.7096</Rate>
			<Rate currency="CZK">0.2016</Rate>
			<Rate currency="DKK">0.6649</Rate>
			<Rate currency="EGP">0.2530</Rate>
			<Rate currency="EUR">4.9480</Rate>
			<Rate currency="GBP">5.8793</Rate>
			<Rate currency="HRK">0.6538</Rate>
			<Rate currency="HUF" multiplier="100">1.3122</Rate>
			<Rate currency="INR">0.0612</Rate>
			<Rate currency="JPY" multiplier="100">3.5869</Rate>
			<Rate currency="KRW" multiplier="100">0.3716</Rate>
			<Rate currency="MDL">0.2543</Rate>
			<Rate currency="MXN">0.2308</Rate>
			<Rate currency="NOK">0.5034</Rate>
			<Rate currency="NZD">3.0529</Rate>
			<Rate currency="PLN">1.0602</Rate>
			<Rate currency="RSD">0.0420</Rate>
			<Rate currency="RUB">0.0657</Rate>
			<Rate currency="SEK">0.4790</Rate>
			<Rate currency="THB">0.1365</Rate>
			<Rate currency="TRY">0.3154</Rate>
			<Rate currency="UAH">0.1547</Rate>
			<Rate currency="USD">4.6774</Rate>
			<Rate currency="XAU">287.9962</Rate>
			<Rate currency="XDR">6.2922</Rate>
			<Rate currency="ZAR">0.2941</Rate>
		</Cube>
	</Body>
</DataSet>
Reply
#2
Have you looked at ElementTree? The standard way to navigate XML is XPath and it has support for that.
Reply
#3
(Apr-30-2022, 07:32 AM)ndc85430 Wrote: Have you looked at ElementTree? The standard way to navigate XML is XPath and it has support for that.

I looked but I couldn't find anything that could help me
Reply
#4
Really? I find that hard to believe. You just need to go through the Rate elements and find ones that have an attribute called multiplier. Can you do that? The docs show you examples of the kind of thing you can do, so you do need to do some reading and experimenting.

Having said that, XPath would still be the better way to do it - again, see the docs for examples.
Reply
#5
(Apr-30-2022, 07:54 AM)ndc85430 Wrote: Really? I find that hard to believe. You just need to go through the Rate elements and find ones that have an attribute called multiplier. Can you do that? The docs show you examples of the kind of thing you can do, so you do need to do some reading and experimenting.

Having said that, XPath would still be the better way to do it - again, see the docs for examples.

i resolved it

    for x in root[1][2]:
        for key in x.attrib.keys():
            if key == 'multiplier':
                print(x.attrib['currency'])
Output:
HUF JPY KRW None
Reply
#6
For me the standard way(for a long time) is to use BS(with lxml as parser for speed) or lxml alone.
Of course if new to this then may use stuff from standard library first,just to show that there other option for this.
from bs4 import BeautifulSoup

soup = BeautifulSoup(xml_data, 'lxml-xml')
multi = soup.find_all('Rate', {'multiplier': True})
for item in multi:
    print(item.attrs.get('currency'))
Output:
HUF JPY KRW
3lnyn0 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  currency converter using forex-python preethy12ka4 8 752 Mar-08-2024, 06:59 PM
Last Post: preethy12ka4
  Currency converter Scott 5 3,016 Jun-14-2020, 11:59 PM
Last Post: Scott
  Currency formatting the third element in a 2D list RedSkeleton007 3 3,224 Mar-09-2018, 12:53 PM
Last Post: j.crater
  rounding locale.currency birdieman 8 10,622 Dec-28-2016, 01:48 AM
Last Post: birdieman

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020