0

Yesterday i have asked similar question like this about output xml by using python. But today i want to use the python code to display the XML content of the restaurant name and capacity for rate larger than 4. Here is xml my code

<record>
   <restaurant name="La Pasteria" rate="3">
      <cuisine id="-">Italian</cuisine>
      <address>8, Jalan Mata Kuching, 89200 Selangor</address>
      <capacity>300</capacity>
      <phone>06-2899808</phone>
      <phone>06-2829818</phone>
         <general>-</general>
         <shop1>-</shop1>
         <shop2>-</shop2>
   </restaurant>
   <restaurant name="Nyonya Baba" rate="5">
      <cuisine id="112">Malaysian</cuisine>
      <address>21, Jalan Taming Sari, 75350 Melaka</address>
      <address>25, Jalan Bukit Beruang, 75450 Melaka</address>
      <capacity>80</capacity>
      <phone>
      <general>012-2677498</general>
         <shop1>06-2855413</shop1>
         <shop2>06-2856418</shop2>
      </phone>
</record>

Here is what i am using in python

import xml.etree.ElementTree as ET
 
# We're at the root node (<page>)
root_node = ET.parse('record.xml').getroot()

# We need to go one level below to get <items>
# and then one more level from that to go to <item>
for tag in root_node.findall('restaurant'):
    value = tag.attrib['name']
    print("Restaurant name:")
    print(value)
    for capacity in tag.findall('capacity'):
        print(capacity.text)

What i need to add inside this code to output if the restaurant rate is larger than 4?

1 Answer 1

1

All you had to do was to check if the rate is larger than 4

import xml.etree.ElementTree as ET
 
# We're at the root node (<page>)
root_node = ET.parse('record.xml').getroot()

# We need to go one level below to get <items>
# and then one more level from that to go to <item>
for tag in root_node.findall('restaurant'):
    if int(tag.attrib.get("rate", 0)) > 4:
        value = tag.attrib['name']
        print("Restaurant name:")
        print(value)
        for capacity in tag.findall('capacity'):
            print(capacity.text)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.