Обновите значения элементов, используя xml.dom.minidom - PullRequest
3 голосов
/ 23 марта 2010

У меня есть структура 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>

Используя только xml.dom.minidom (ограничения) я бы хотел

1) пройти через файл XML

2) Поиск / получение определенного элемента в зависимости от его родителя

Пример: элемент проверки для author1, isbn для author2

3) Изменить / установить значение этого элемента

4) Записать новую структуру XML в файл

Может кто-нибудь помочь здесь?

Спасибо!

UPDATE

Это то, что я делал до сих пор

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()

1 Ответ

5 голосов
/ 24 марта 2010

Я не совсем уверен, что вы подразумеваете под "оформить заказ".Этот скрипт найдет элемент и изменит значение этого элемента.Возможно, вы сможете адаптировать его к вашим конкретным потребностям.

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()

Общая идея заключается в том, что вы сопоставляете имя автора с tagNames дочерних элементов элемента "Store", а затем просматриваете через дочерние элементыавтор, ищущий совпадение с целевым элементом tagName.В этом решении сделано много предположений, но оно может помочь вам начать.Больно пытаться работать с иерархическими структурами, такими как XML, без использования рекурсии.

Cheers, Phil


В ретроспективе произошла ошибка в функции "alterElement".Я исправил это (обратите внимание на переменную "found")

...