简体   繁体   English

Python Lxml:将自己的标签添加到XML文件

[英]Python Lxml: Add own tags to a XML File

I've got a problem while adding my own Elements (tags) to an XML tree I've parsed from a file. 将我自己的Elements(标签)添加到从文件解析的XML树中时遇到问题。

If I'm trying to add a Element to the existing tree, that one from the file, it doesn't add it correctly, no matter if I use new_tag = etree.Subelement(self.root, "new_tag") or 如果我试图将一个元素添加到现有树中,则该文件中的那个元素将无法正确添加,无论我使用new_tag = etree.Subelement(self.root, "new_tag")还是
new_tag = Element("new_tag") self.root.append(new_tag)

The returning of self.root.tostring() is looking awesome, the added elements where not indented in the right way and for example len(self.root) still returns the same value as it did before I added the Element. self.root.tostring()的返回看起来很棒,添加的元素没有以正确的方式缩进,例如len(self.root)仍返回与添加元素之前相同的值。

The only thing I did diffrent from the examples at lxml.de is that I put all these thing into a class ... but why shouldn't that work? 我与lxml.de示例有所不同的唯一一件事是,我将所有这些东西都放入了一个类中……但是那为什么不起作用呢?

I hope you could help me, because I was searching for that bug about 3 hours and I didn't find it. 希望您能对我有所帮助,因为我搜索该错误大约3个小时,但是没有找到。

Leon 里昂

edit: 编辑:

here is the code: 这是代码:

#!/usr/bin/env python3

from lxml import etree

class MyClass(object):
    def __init__(self, xml_filepath = "data.xml"):
        '''
        Constructor
        '''
        self._xml_path = xml_filepath

        with open(self._xml_path) as input_file:
            self.tree = etree.parse(input_file)
        self.xml_tags = self.tree.getroot()[0]

    def read_something(self):


        return [tag.tag for tag in self.tree.iter("child")]

    def change_something(self):
        self.xml_tags[0].tag = "test"

    def add_something(self):
        for elements in self.tree.iter('Home'):        
            child = etree.Element("child")
            child2 = etree.Element("child2")
            elements.insert(1,child)
            child.insert(0, child2)
            child2.text = "23123"

    def to_string(self):
        list_ = list(self.tree.getroot()[0])
        print(list_)
        return(etree.tostring(self.tree, pretty_print=True, xml_declaration=True).decode())

if __name__ == '__main__':
    testClass = MyClass()
    testClass.change_something()
    testClass.add_something()
    print(testClass.to_string())
    print(testClass.read_something())

the returning is: 返回的是:

[<Element test at 0x765e7e68>, <Element child at 0x765e7e90>]
<?xml version='1.0' encoding='ASCII'?>
<root>
  <Home>
    <test>
      <name>Hello world</name>
    </test>
  <child><child2>23123</child2></child></Home>
</root>

['child']

and the original xml file: 和原始的xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <Home>
    <test>      
      <name>Hello world</name>
    </test>    
  </Home>
</root>

You have to directly find the root and then insert. 您必须直接找到根,然后插入。 The tutorial you are looking at does not parse an example xml file but actually creates root and child elements: 您正在看的教程不会解析示例xml文件,而是实际创建根元素和子元素:

import lxml.etree as ET

dom = ET.parse('C:\Path\To\XMLFile.xml')

for elements in dom.iter('root'):        
    child = ET.Element("child")
    elements.insert(1,child)
    child.text = '...'

I've got found the Error by myself. 我自己发现了错误。 If I add my own parser to the parse method, I'll get a well formated XML output, also the interna are working well now. 如果我将自己的解析器添加到parse方法中,则将获得格式正确的XML输出,并且内部也可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM