我解析了一些XML配置以使用其中的設置。我不打算再寫出XML,所以我的興趣只在於提取。如何合併兩個lxml.objectify元素?
我有兩個lxml.objectify元素:一方面包含默認全局設置的元素,另一方面包含特定於實例的設置。每個元素都具有相似的結構(例如root.global_settings.lights與root.instance_settings.lights具有相同的設置),但可以有交集和設置差異。元素包含一些兒童文本節點,但也包含其他節點的節點。
我想要什麼:一個元素包含來自兩個元素的所有設置。特定於實例的設置將覆蓋全局設置。
我現在最好的解決方案是循環實例子項並覆蓋/添加到全局項目(在有文本節點的所有級別)。我在想也許會有更像dict.update的東西?
編輯:只是舉一個例子
<global>
<birthday>Unknown</birthday>
<wishes>
<cake>Chocolate</cake>
<gift>Book</gift>
</wishes>
</global>
<instance>
<name>Mike</name>
<birthday>06-06-1974</birthday>
<wishes>
<notes>Hates flowers</notes>
</wishes>
<instance>
將產生相同的,如果我在
<global>
<name>Mike</name>
<birthday>06-06-1974</birthday>
<wishes>
<cake>Chocolate</cake>
<gift>Book</gift>
<notes>Hates flowers</notes>
</wishes>
</global>