0

This is the xml file type , I am trying modify elements in

Assume xml file name is "t.xml"

I used element tree to modify it via python and get the elements where I can modify using the logic and do it in bulk

I tried to print the names to check whether its working or not , but I am not able to get any output even though the python file is getting executed.

How can I modify c3 value in custom ???

<breakfast_menu>
    <food>
        <name itemid="11">Belgian Waffles</name>
        <price>5.95</price>
        <description>Two of our famous Belgian Waffles
with plenty of real maple syrup</description>
        <calories>650</calories>
        <city></city>
        <state></state>
        <custom>
              <c1>11</c1>
              <c2>ee</c2>
              <c3>king</c3>
        </custom>


    </food>
    <food>
        <name itemid="21">Strawberry Belgian Waffles</name>
        <price>7.95</price>
        <description>Light Belgian waffles covered
with strawberries and whipped cream</description>
        <calories>900</calories>
        <city></city>
        <state></state>
        <custom>
              <c1>12</c1>
              <c2>ff</c2>
              <c3>bye</c3>
        </custom>
    </food>
    <food>
        <name itemid="31">Berry-Berry Belgian Waffles</name>
        <price>8.95</price>
        <description>Light Belgian waffles covered with
an assortment of fresh berries and whipped cream</description>
        <calories>900</calories>
         <city></city>
        <state></state>
        <custom>
              <c1>13</c1>
              <c2>gg</c2>
              <c3>getin</c3>
        </custom>
    </food>
    <food>
        <name itemid="41">French Toast</name>
        <price>4.50</price>
        <description>Thick slices made from our
homemade sourdough bread</description>
        <calories>600</calories>
        <city></city>
        <state></state>
        <custom>
              <c1>15</c1>
              <c2>hh</c2>
              <c3>python</c3>
        </custom>
    </food>
</breakfast_menu>

import xml.etree.ElementTree as ET

mytree = ET.parse('t.xml')
myroot = mytree.getroot()

for x in myroot.findall('food'):
      item = x.find('name').text
print(item)

Please provide guidance

Thanks

2
  • For me it prints "French Toast" (slightly modified code to read from string). You may try to use an absolute path to the XML file to ensure that the right file is read. Commented Dec 5, 2022 at 3:11
  • @MichaelButscher tried but it didnt make any difference Commented Dec 5, 2022 at 5:14

1 Answer 1

1

To print the "name" (as what your code wanted to do):

from xml.etree import ElementTree as ET

elem_tree = ET.parse('t.xml')
root = elem_tree.getroot()

for food in root.findall('food'):
    for name in food.findall('name'):
        print("name: {}, id: {}".format(name.text, name.get('itemid')))

To modify "c3":

from xml.etree import ElementTree as ET

elem_tree = ET.parse('t.xml')
root = elem_tree.getroot()

for food in root.findall('food'):
    for custom in food.findall('custom'):
        for c3 in custom.findall('c3'):
            c3.text = 'Modified value'

print(ET.tostring(root, encoding='unicode', method='xml'))  # Print to console
# elem_tree.write('t_modified.xml')  # Write to file
Sign up to request clarification or add additional context in comments.

3 Comments

I am trying execute the file , its showing no errors but cant get an output
@Learn-code I've modified the answer. Your question title said "modifying xml file", I thought you want to write the result to an XML file, so the example code writes the result to file "t_modified.xml". But it seems that you want to "get an output", now I've commented out the code line of writing file, and added a line of printing to console.
Thanks , I did the same, I am trying to print and write the file to verify the output

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.