2011-04-14 93 views
1

我有一個xml文件中的節點,該文件尚不存在,並且想用它來生成有問題的節點。我已經開始研究一個函數來做到這一點,但想知道是否有一個現有的庫可以做到這一點,並節省了我一些時間?我目前正在使用pyxml,但正在考慮將其移植到ElementTree。因此,要澄清我想:如何在Python中使用xpath填充xml文件

root/foo/bar 

產生:

<root> 
    <foo> 
    <bar> 
    </bar> 
    </foo> 
</root> 

我懷疑的是,這樣一個函數的行爲是不是不夠好,一般情況下定義的人,打擾你了,但想到我爲了以防萬一,請將它扔出去。如果這有幫助,我還有一個文件的DTD。

+0

乍一看,這似乎是不可能的創建基於XPath的「_stricto sensu_」這樣的元素。例如,它會爲'// foo/bar'生成什麼? OTOH,它似乎有可能基於XPath的一個子集生成XML - 事實上它似乎是一個好主意。 – brandizzi 2011-04-14 15:22:04

回答

1

找不到任何東西準備好, 但它應該或多或少地直接使用ElementTree(或甚至另一個xml庫 - 它只是我更熟悉ElementTree)。

的片段波紋管似乎XPath的有限子集是需要爲它工作:

# -*- coding: utf-8 -*- 
from xml.etree import ElementTree as ET 

def build_xpath(node, path): 
    components = path.split("/") 
    if components[0] == node.tag: 
     components.pop(0) 
    while components: 
     # take in account positional indexes in the form /path/para[3] or /path/para[location()=3] 
     if "[" in components[0]: 
      component, trail = components[0].split("[",1) 
      target_index = int(trail.split("=")[-1].strip("]")) 
     else: 
      component = components[0] 
      target_index = 0 
     components.pop(0) 
     found_index = -1 
     for child in node.getchildren(): 
      if child.tag == component: 
       found_index += 1 
       if found_index == target_index: 
        node = child 
        break 
     else: 
      for i in range(target_index - found_index): 
       new_node = ET.Element(component) 
       node.append(new_node) 
      node = new_node 


if __name__ == "__main__": 
    #Example 
    root = ET.Element("root") 
    build_xpath(root, "root/foo/bar[position()=4]/snafu") 
    print ET.tostring(root) 
+1

如何在這裏處理屬性? – 2014-10-07 14:25:57