2

Pythonを使用してXMLファイルの末尾に追加したいと思います。

ファイルは次のような構造になっています。

<?xml version="1.0" ?>
<dic>
  <word-data>
    ...
  </word-data>
</dic>

And I would like to add another element in so that the file will look something like this:

<?xml version="1.0" ?>
<dic>
  <word-data>
    ...
  </word-data>
  <word-data>
    ...
  </word-data>
</dic>

Now for the programming Part!

What I am currently doing is "encoding" a list into xml using this function:

def make_xml(List):
    doc = Document();

    main = doc.createElement('dic')
    doc.appendChild(main)
    for l in List:
        parent = doc.createElement('word-data')
        main.appendChild(parent)

        for i in l:
            node = doc.createElement(i[0])
            node.appendChild(doc.createTextNode(str(i[1])))
            parent.appendChild(node)
    return doc

One of the lists looks like this:

List = [[['parent', 'node'],['parent', 'node'],['parent', 'node']]]

Now my question is how can I add this to the doc node in an existing XML file. I thought about turning the file into a list Then turning the doc into a new list, but I did not know how to do that, and I thought it may have been inefficient.

Anyways, any help is appreciated

4

1 に答える 1