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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2502758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:47:33  来源:igfitidea点击:

Update element values using xml.dom.minidom

pythonxmlminidom

提问by user291784

I have an XML structure which looks similar to:

我有一个类似于以下内容的 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>

Using xml.dom.minidom only (restrictions) i would like to

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

1)traverse through the XML file

1)遍历XML文件

2)Search/Get for particular element, depending on its parent

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

Example: checkout element for author1, isbn for author2

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

3)Change/Set that element's value

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

4)Write the new XML structure to a file

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

Can anyone help here?

有人可以帮忙吗?

Thank you!

谢谢!

UPDATE:

更新

This is what i have done till now

这是我迄今为止所做的

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 None) or (len(elem) != 1)):
    return None
  leaf = elem[0].firstChild
  if (leaf is None):
    return None
  return leaf.data


def setcheckout(node, tagname):

  assert ((node is not None) and (tagname is not None))
  child = getLoneChild(node, 'foo')
  Check = getLoneLeaf(child[0],'checkout')
  Check = tagname
  return Check

doc = xml.dom.minidom.parse('test.xml') 
root = doc.getElementsByTagName('Store')[0]
output = setcheckout(root, checkout)

tmp_config = '/tmp/tmp_config.xml' 
fw = open(tmp_config, 'w')
fw.write(doc.toxml())
fw.close()

回答by Phil Boltt

I'm not entirely sure what you mean by "checkout". This script will find the element and alter the value of that element. Perhaps you can adapt it to your specific needs.

我不完全确定您所说的“结帐”是什么意思。此脚本将查找元素并更改该元素的值。也许您可以根据自己的特定需求进行调整。

import xml.dom.minidom as DOM

# find the author as a child of the "Store"
def getAuthor(parent, author):
  # by looking at the children
  for child in [child for child  in parent.childNodes 
                if child.nodeType != DOM.Element.TEXT_NODE]:
    if child.tagName == author:
      return child
  return None

def alterElement(parent, attribute, newValue):
  found = False;
  # look through the child elements, skipping Text_Nodes 
  #(in your example these hold the "values"
  for child in [child for child  in parent.childNodes 
                if child.nodeType != DOM.Element.TEXT_NODE]:

    # if the child element tagName matches target element name
    if child.tagName == attribute:
      # alter the data, i.e. the Text_Node value, 
      # which is the firstChild of the "isbn" element
      child.firstChild.data = newValue
      return True

    else:
      # otherwise look at all the children of this node.
      found = alterElement(child, attribute, newValue)

    if found:
      break 

  # return found status
  return found

doc = DOM.parse("test.xml")
# This assumes that there is only one "Store" in the file
root = doc.getElementsByTagName("Store")[0]

# find the author
# this assumes that there are no duplicate author names in the file
author = getAuthor(root, "foo")
if not author:
  print "Author not found!"
else:
  # alter an element
  if not alterElement(author, "isbn", "987654321"):
    print "isbn not found"
  else:
    # output the xml
    tmp_config = '/tmp/tmp_config.xml'
    f = open(tmp_config, 'w')
    doc.writexml( f )
    f.close()

The general idea is that you match the name of the author against the tagNames of the children of the "Store" element, then recurse through the children of the author, looking for a match against a target element tagName. There are a lot of assumptions made in this solution, but it may get you started. It's painful to try and deal with hierarchical structures like XML without using recursion.

一般的想法是将作者的姓名与“Store”元素的子元素的 tagNames 进行匹配,然后遍历作者的子元素,寻找与目标元素 tagName 的匹配项。在这个解决方案中有很多假设,但它可能会让你开始。在不使用递归的情况下尝试处理像 XML 这样的分层结构是很痛苦的。

Cheers, Phil

干杯,菲尔



In retrospect there was an error in the "alterElement" function. I've fixed this (note the "found" variable")

回想起来,“alterElement”函数中存在错误。我已经解决了这个问题(注意“找到”变量”)