Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python将XML转换为MYSQL_Python_Xml - Fatal编程技术网

使用Python将XML转换为MYSQL

使用Python将XML转换为MYSQL,python,xml,Python,Xml,我有下面的test.xml <root> <parent> <ID>1</ID> <child1>Value1</child1> <child2>value11</child2> <child3> <subchild>value111</subchild> </child3> </parent&

我有下面的test.xml

<root>
<parent>
    <ID>1</ID>
    <child1>Value1</child1>
    <child2>value11</child2>
    <child3>
       <subchild>value111</subchild>
    </child3>
</parent>
<parent>
    <ID>2</ID>
    <child1>value2</child1>
    <child2>value22</child2>
    <child2>value333</child2>
</parent>
<parent>
    <ID>3</ID>
    <child1>value3</child1>
    <child2>value33</child2>
</parent>
<parent>
    <ID>4</ID>
    <child1>value4</child1>
    <child2>value44</child2>
</parent>
</root>
在SQL查询中使用它,然后移动到下一个父级,重置字典并执行相同的操作。 并非每个父母都有相同数量的子女,有些子女有子子女

我试过:

    value = []
    tag = []

    from elementtree import ElementTree as ET
    for parent in tree.getiterator():
        for child in parent:
             value.append(child.text)
             tag.append(child.tag)
但是我不知道如何得到我想要的结果。为了使文章尽可能简单,我省略了检索和打开xml。这是我尝试使用的方法,但我认为这不是正确的方法,因为我无法在父标记末尾停止迭代以插入

任何帮助都将不胜感激!谢谢

使用库尝试以下操作:

从lxml导入etree
来源=”“
1.
价值1
价值11
价值111
2.
价值2
价值22
价值333
3.
价值3
价值33
4.
价值4
价值44
"""
document=etree.fromstring(源)
插入=[]
id_编号=3
对于document.findall(“父项”)中的父项:
插入={}
cont=0
对于父级.iterDescents()中的元素:
如果element.tag==“ID”:
如果element.text==str(id\u编号):
cont=1
if元素.getchildren()=[]:
插入[element.tag]=element.text
如果继续:
insert.append(insert)
打印插页

python附带了一个etree API(它没有很好的打印功能和lxml所具有的一些其他功能):

这正是我所需要的。非常感谢。假设我只想要ID为3的父节点。我将如何做到这一点?当我说只想要父节点时,我的意思是上面代码提取的所有信息都包含在其中。但前提是ID为3
    value = []
    tag = []

    from elementtree import ElementTree as ET
    for parent in tree.getiterator():
        for child in parent:
             value.append(child.text)
             tag.append(child.tag)
from lxml import etree

source = """
<root>
<parent>
    <ID>1</ID>
    <child1>Value1</child1>
    <child2>value11</child2>
    <child3>
       <subchild>value111</subchild>
    </child3>
</parent>
<parent>
    <ID>2</ID>
    <child1>value2</child1>
    <child2>value22</child2>
    <child2>value333</child2>
</parent>
<parent>
    <ID>3</ID>
    <child1>value3</child1>
    <child2>value33</child2>
</parent>
<parent>
    <ID>4</ID>
    <child1>value4</child1>
    <child2>value44</child2>
</parent>
</root>
"""

document = etree.fromstring(source)
inserts = []

id_number = 3

for parent in document.findall('parent'):
    insert = {}
    cont = 0
    for element in parent.iterdescendants():
        if element.tag == 'ID':
            if element.text == str(id_number):
                cont = 1
        if element.getchildren() == []:
            insert[element.tag] = element.text
    if cont:
        inserts.append(insert)

print inserts