ElementTree-有条件更改子儿童价值

发布于 2025-02-09 21:17:04 字数 1172 浏览 3 评论 0原文

我只是在当前项目中使用ElementTree与XML合作。我的任务是根据同一孩子中的另一个子儿童值更改子孩子的价值。

我为此创建了一个代码,但某种程度上认为可能有一种方法可以改善这种可读性和明智的性能。

这是我的代码,

import xml.etree.ElementTree as ET

tree = ET.ElementTree(ET.fromstring("<Properties><Property><Name>KENT</Name><Value>99</Value></Property><Property><Name>JOHN</Name><Value>fifthy</Value></Property></Properties>"))
root = tree.getroot()

change_found = False

for item in root:

    for subItem in item:

        if change_found and subItem.tag == "Value":

            subItem.text = "50"
            change_found = False

        if subItem.tag == "Name" and subItem.text == "JOHN":

            change_found = True


print(ET.tostring(root, encoding='utf8', method='xml'))

正如您可以从代码中看到的那样,当子儿童文本为“ john”并且标签为“名称”时,它将change_found设置为true。由于下一个子儿童具有价值标签,因此将其更改为文本(从五十到50)。

该代码正常运行,但我相信可以有所改进。

您可以假设属性的结构始终处于此顺序中。

<Property> 
    <Name> Some name </Name>
    <Value> Some value </Value>
<Property>

您还可以假设只有1个带有子孙的“名称”,带有文字“ John”

I have just work with XML using ElementTree in my current project. I have a task to change a subchild value based on another subchild value in the same child.

I have created a code for that but somehow feel that there might be a way to improve on this readability wise and performance wise.

Here is my code,

import xml.etree.ElementTree as ET

tree = ET.ElementTree(ET.fromstring("<Properties><Property><Name>KENT</Name><Value>99</Value></Property><Property><Name>JOHN</Name><Value>fifthy</Value></Property></Properties>"))
root = tree.getroot()

change_found = False

for item in root:

    for subItem in item:

        if change_found and subItem.tag == "Value":

            subItem.text = "50"
            change_found = False

        if subItem.tag == "Name" and subItem.text == "JOHN":

            change_found = True


print(ET.tostring(root, encoding='utf8', method='xml'))

As you can see from the code, when the subchild text is "JOHN" and the tag is "Name", it sets the change_found to True. Since the next subchild has a tag of Value, it made the change to the text (from fifty to 50).

The code works fine, but I believe there can be some improvement.

You can assume that the structure of the property is always in this order.

<Property> 
    <Name> Some name </Name>
    <Value> Some value </Value>
<Property>

You can also assume that there are only 1 with has a subchild "NAME" with a text "JOHN"

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

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

发布评论

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

评论(1

允世 2025-02-16 21:17:04

如果我正确理解您,可以使用XPath:

root = ET.fromstring([your string above])
for p in root.findall('.//Property'):
    if p.find('.//Name').text.strip()=="JOHN":
        p.find('.//Value').text="50"

print(ET.tostring(root).decode())

输出:输出:

<Properties>
    <Property>
      <Name>KENT
      </Name>
      <Value>99
      </Value>
    </Property>
    <Property>
      <Name>JOHN
      </Name>
      <Value>50</Value>
    </Property>
  </Properties>

If I understand you correctly, you can get there more simply, using xpath:

root = ET.fromstring([your string above])
for p in root.findall('.//Property'):
    if p.find('.//Name').text.strip()=="JOHN":
        p.find('.//Value').text="50"

print(ET.tostring(root).decode())

Output:

<Properties>
    <Property>
      <Name>KENT
      </Name>
      <Value>99
      </Value>
    </Property>
    <Property>
      <Name>JOHN
      </Name>
      <Value>50</Value>
    </Property>
  </Properties>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文