我想在xml线索中添加一个元素

时间:2019-03-05 02:58:06

标签: python xml xml-declaration

sport.xml是

import h5py
import argparse
import sys
from anytree import Node, RenderTree

class HDFTree:
    def __init__(self,filename):
        self._file = h5py.File(filename,'r')
        self._root = Node(filename)
        self._node_map = {filename:self._root}
        self._create_tree()

    def _add_node(self,name,item):
        #TODO: Figure out way to get parent of fnode
        parent_node = self._node_map[item.parent] # I don't think item.parent is a thing so this wont work
        self._node_map[name] = Node(name,parent=parent_node)

    def _create_tree(self):
        self._file.visititems(self._add_node)

    def print_tree(self):
        print(RenderTree(self._root))

    def __del__(self):
        self._file.close()

我要附加一个元素

<sports>
  <sport order="001">
    <name>football</name>
    <orgin>England</orgin>
  </sport>
  <sport order="002">
    <name>baseball</name>
    <orgin>America</orgin>
  </sport>
</sports>

到xml的线索

因此,我想制作一个xml

<?xml version="1.0" encoding="UTF-8" ?>

我写了代码,

<?xml version="1.0" encoding="UTF-8" ?>
<sports>
  <sport order="001">
    <name>football</name>
    <orgin>England</orgin>
  </sport>
  <sport order="002">
    <name>baseball</name>
    <orgin>America</orgin>
  </sport>
</sports>

但是我做不到。 我应该如何编写代码以将该元素附加到xml的线索中?

0 个答案:

没有答案