如何将自定义数据添加到我的整个XML数据中?
我正在尝试在所有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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只想将子对象添加到行元素中,则以下代码将起作用。
If you only want to add a child object to the row elements, the following code would work.
感谢您的解决方案:@efe
这就像我的魅力一样
我的代码:
Thanks for the solution : @Efe
This worked like a charm for me
My code :