繁体   English   中英

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

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

将我自己的Elements(标签)添加到从文件解析的XML树中时遇到问题。

如果我试图将一个元素添加到现有树中,则该文件中的那个元素将无法正确添加,无论我使用new_tag = etree.Subelement(self.root, "new_tag")还是
new_tag = Element("new_tag") self.root.append(new_tag)

self.root.tostring()的返回看起来很棒,添加的元素没有以正确的方式缩进,例如len(self.root)仍返回与添加元素之前相同的值。

我与lxml.de示例有所不同的唯一一件事是,我将所有这些东西都放入了一个类中……但是那为什么不起作用呢?

希望您能对我有所帮助,因为我搜索该错误大约3个小时,但是没有找到。

里昂

编辑:

这是代码:

#!/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())

返回的是:

[<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']

和原始的xml文件:

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

您必须直接找到根,然后插入。 您正在看的教程不会解析示例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 = '...'

我自己发现了错误。 如果我将自己的解析器添加到parse方法中,则将获得格式正确的XML输出,并且内部也可以正常工作。

暂无
暂无

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

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