小编use*_*784的帖子

使用xml.dom.minidom更新元素值

我有一个XML结构,看起来类似于:

<Store>
   <foo>
      <book>
        <isbn>123456</isbn>
      </book>
      <title>XYZ</title>
      <checkout>no</checkout>
   </foo>

   <bar>
      <book>
        <isbn>7890</isbn>
      </book>
      <title>XYZ2</title>
      <checkout>yes</checkout>
   </bar>
</Store>
Run Code Online (Sandbox Code Playgroud)

仅使用xml.dom.minidom(限制)我想

1)遍历XML文件

2)搜索/获取特定元素,具体取决于其父元素

示例:author1的checkout元素,author2的isbn

3)更改/设置该元素的值

4)将新的XML结构写入文件

有人可以帮忙吗?

谢谢!

更新:

这是我到目前为止所做的

import xml.dom.minidom
checkout = "yes"

def getLoneChild(node, tagname):

  assert ((node is not None) and (tagname is not None))
  elem = node.getElementsByTagName(tagname)
  if ((elem is None) or (len(elem) != 1)):
    return None
  return elem

def getLoneLeaf(node, tagname):

  assert ((node is not None) and (tagname is not None))
  elem = node.getElementsByTagName(tagname)
  if ((elem is …
Run Code Online (Sandbox Code Playgroud)

python xml minidom

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

minidom ×1

python ×1

xml ×1