2010-03-23 62 views
3

更新元件值I有一个XML结构,其看起来类似于:使用xml.dom.minidom

<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)搜索/获取特定元件,这取决于它的父

示例:作者1结帐元件,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() 
+0

你有什么,到目前为止,你在哪里遇到了问题? – 2010-03-23 19:33:53

+0

是标签作者1,author2实际上是或作者的名字?你能它们定义为只是名称=“POE”的属性 – corn3lius 2010-03-23 19:34:43

+0

@Tim?作者:我在这里添加的代码,我相信我不会写入文件的正确方法 @ corn3lius:我已编辑的作者名到“富”和“酒吧” 谢谢! – user291784 2010-03-23 21:17:14

回答

5

我不完全相信你的 “结账” 的意思。该脚本将查找该元素并更改该元素的值。也许你可以适应你的具体需求。

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

总的想法是,你对阵的“商店”元素的子元素的标记名作者的名字,然后通过笔者的孩子递归,找对目标元素的标记名比赛。在这个解决方案中有很多假设,但它可能会让你开始。在不使用递归的情况下尝试处理XML等分层结构是很痛苦的。

干杯, 菲尔


回想起来有在 “alterElement” 功能的错误。我已经解决了这个问题(注意是“发现”可变“)

+0

非常感谢菲尔,这真的帮助! – user291784 2010-03-24 00:39:28

相关问题