向XML fi添加内联注释

2024-06-16 08:33:26 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有一些随机的XML文件,其节点如下所示:

    <entry>
      <name>John</name>
      <last_name>Smith</last_name>
    </entry>

我想添加一个在线评论如下:

    <entry>  <!--He is a nice guy-->
      <name>John</name>
      <last_name>Smith</last_name>
    </entry>

我认为以下几点可以做到:

all_nodes = doc.getElementsByTagName('entry')
for my_node in all_nodes:
  comment = flight_node.ownerDocument.createComment("He is a nice guy")
  my_node.appendChild(comment)

但我得到:

    <entry>
      <name>John</name> 
      <last_name>Smith</last_name>
    <!--He is a nice guy-->
    </entry>

相反。你知道吗

我怎样才能获得在线评论?你知道吗


Tags: namenodeismycomment评论alljohn
1条回答
网友
1楼 · 发布于 2024-06-16 08:33:26

appendChild,正如预期的那样,把它放在末尾。要插入它,需要使用insertBefore。这也许不是你想要的,但是

my_node.childNodes.insert(0, comment)

应该能让你靠近。你知道吗

相关问题 更多 >