如何将自定义数据添加到我的整个XML数据中?

发布于 2025-02-10 19:12:27 字数 2033 浏览 2 评论 0原文

我正在尝试在所有XML

数据中添加自定义数据:

<?xml version='1.0' encoding='utf-8'?>
<data>
  <row>
    <index>0</index>
    <price>$5.95</price>
    <name>Belgian Waffles</name>
    <desc>Two of our famous Belgian Waffles with plenty of real maple syrup</desc>
    <calories>650</calories>
  </row>
  <row>
    <index>1</index>
    <price>$7.95</price>
    <name>Strawberry Belgian Waffles</name>
    <desc>Light Belgian waffles covered with strawberries and whipped cream</desc>
    <calories>900</calories>
  </row>
  <row>
    <index>2</index>
    <price>$8.95</price>
    <name>Berry-Berry Belgian Waffles</name>
    <desc>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</desc>
    <calories>900</calories>
  </row>
  <row>
    <index>3</index>
    <price>$4.50</price>
    <name>French Toast</name>
    <desc>Thick slices made from our homemade sourdough bread</desc>
    <calories>600</calories>
  </row>
  <row>
    <index>4</index>
    <price>$6.95</price>
    <name>Homestyle Breakfast</name>
    <desc>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</desc>
    <calories>950</calories>
  </row>
</data>

我的代码:

import xml.etree.ElementTree as ET
parse_xml = ET.parse('/content/sample_data/xyz.xml')
get_root_element = parse_xml.getroot()
ET.SubElement(get_root_element[0],'FID')

for data in get_root_element:
  customtext = 'ABC'
  data.text = str(customtext)
parse_xml.write('abc.xml')

我已经写了:et.Subelement(get_root_element [0],'fid') - &gt;它仅将数据添加到首先索引中。

我希望将同样的内容添加到所有索引中?

I am trying add the custom data across all my xml

Data:

<?xml version='1.0' encoding='utf-8'?>
<data>
  <row>
    <index>0</index>
    <price>$5.95</price>
    <name>Belgian Waffles</name>
    <desc>Two of our famous Belgian Waffles with plenty of real maple syrup</desc>
    <calories>650</calories>
  </row>
  <row>
    <index>1</index>
    <price>$7.95</price>
    <name>Strawberry Belgian Waffles</name>
    <desc>Light Belgian waffles covered with strawberries and whipped cream</desc>
    <calories>900</calories>
  </row>
  <row>
    <index>2</index>
    <price>$8.95</price>
    <name>Berry-Berry Belgian Waffles</name>
    <desc>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</desc>
    <calories>900</calories>
  </row>
  <row>
    <index>3</index>
    <price>$4.50</price>
    <name>French Toast</name>
    <desc>Thick slices made from our homemade sourdough bread</desc>
    <calories>600</calories>
  </row>
  <row>
    <index>4</index>
    <price>$6.95</price>
    <name>Homestyle Breakfast</name>
    <desc>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</desc>
    <calories>950</calories>
  </row>
</data>

My code:

import xml.etree.ElementTree as ET
parse_xml = ET.parse('/content/sample_data/xyz.xml')
get_root_element = parse_xml.getroot()
ET.SubElement(get_root_element[0],'FID')

for data in get_root_element:
  customtext = 'ABC'
  data.text = str(customtext)
parse_xml.write('abc.xml')

I have written : ET.SubElement(get_root_element[0],'FID') -> It add data to only first indexing.

I want the same to be added to all the indexing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

短暂陪伴 2025-02-17 19:12:27

如果您只想将子对象添加到行元素中,则以下代码将起作用。

import xml.etree.ElementTree as ET
parse_xml = ET.parse('xyz.xml')
get_root_element = parse_xml.getroot()
i = 0
while True:
    try:
        ET.SubElement(get_root_element[i],'FID')
        i += 1
    except:
        break
ET.dump(parse_xml)

If you only want to add a child object to the row elements, the following code would work.

import xml.etree.ElementTree as ET
parse_xml = ET.parse('xyz.xml')
get_root_element = parse_xml.getroot()
i = 0
while True:
    try:
        ET.SubElement(get_root_element[i],'FID')
        i += 1
    except:
        break
ET.dump(parse_xml)
雨落星ぅ辰 2025-02-17 19:12:27

感谢您的解决方案:@efe

这就像我的魅力一样

我的代码:

import xml.etree.ElementTree as ET
parse_xml = ET.parse('/content/sample_data/xyz.xml')
get_root_element = parse_xml.getroot()

for idx,record in enumerate(get_root_element):
  ET.SubElement(get_root_element[idx],'FID')

for data in get_root_element.iter('FID'):
  customtext = 'ABC'
  data.text = str(customtext)
parse_xml.write('/content/sample_data/abc.xml')

Thanks for the solution : @Efe

This worked like a charm for me

My code :

import xml.etree.ElementTree as ET
parse_xml = ET.parse('/content/sample_data/xyz.xml')
get_root_element = parse_xml.getroot()

for idx,record in enumerate(get_root_element):
  ET.SubElement(get_root_element[idx],'FID')

for data in get_root_element.iter('FID'):
  customtext = 'ABC'
  data.text = str(customtext)
parse_xml.write('/content/sample_data/abc.xml')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文