0

外部データソースに基づいてXMLファイルを最新の状態に保つ自動化されたプロセスがあります。このXMLファイルはユーザーが変更することもでき、手動での変更を維持する必要があります。

<Nodes>
    <!-- User added data goes here -->
    <Node name="1">Data Data Data</Node>
    <Node name="2">Data Data Data</Node>
    <Node name="3">Data Data Data</Node>

    <!-- AUTOMATEDSTUFF -->
    <!-- User, do not modify nodes below this line. -->
    <Node name="4">Data Data Data</Node>
    <Node name="5">Data Data Data</Node>
    <Node name="6">Data Data Data</Node>

</Nodes>

ファイルを更新するたびに、自動化によって以前に追加されたすべてのノードを吹き飛ばしたいと思います。これは以下のすべてです:

<!-- AUTOMATEDSTUFF --> 

今、私は次のようにPythonですべてのノードを読んでいます:

xmldoc = minidom.parse(filename)
nodesSection = xmldoc.getElementsByTagName('Nodes')[0]
for child in nodesSection.childNodes:
    .....

コメントに遭遇した後でのみノードの検索を開始するにはどうすればよいですか?

4

1 に答える 1

3

In XML, comments are comments, and can be rightfully stripped out of the document in any stage of processing. You should adapt your program to add a special attribute, like

<Nodes>
    <!-- User added data goes here -->
    <Node name="1">Data Data Data</Node>
    <Node name="2">Data Data Data</Node>
    <Node name="3">Data Data Data</Node>

    <!-- User, do not modify nodes below this line. -->
    <Node name="4" from="autogenerated">Data Data Data</Node>
    <Node name="5" from="autogenerated">Data Data Data</Node>
    <Node name="6" from="autogenerated">Data Data Data</Node>
</Nodes>

Then, you can simply filter out all nodes with the property from="autogenerated".


However, if you really want to detect comments (and as mentioned above, that's a bad idea), simply check all children of <Node>:

xmldoc = minidom.parse(filename)
nodes = xmldoc.documentElement.childNodes
commentIdx = next(i for i,n in enumerate(nodes) if
                n.nodeType == n.COMMENT_ELEMENT and n.data == ' AUTOMATEDSTUFF ')
automatedNodes = nodes[commentIdx+1:]
print(automatedNodes) # or do something else with them
于 2012-01-04T18:51:20.947 に答える