Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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数据_Python_Xml_Xml Parsing - Fatal编程技术网

在python中访问非树结构xml数据

在python中访问非树结构xml数据,python,xml,xml-parsing,Python,Xml,Xml Parsing,我有几个xml文件要用python解析。我知道python中的ElementTree包,但是我的xml文件并不是以树状结构存储的。下面是一个例子 <tag1 attribute1="at1" attribute2="at2">My files are text that I annotated with a tool to create these xml files.</tag1> Some parts of the text are enclosed in an x

我有几个xml文件要用python解析。我知道python中的ElementTree包,但是我的xml文件并不是以树状结构存储的。下面是一个例子

<tag1 attribute1="at1" attribute2="at2">My files are text that I annotated with a tool 
to create these xml files.</tag1>
Some parts of the text are enclosed in an xml tag, whereas others are not. 
<tag1 attribute1="at1" attribute2="at2"><tag2 attribute3="at3" attribute4="at4">Some
are even enclosed in multiple tags.</tag1></tag2>
And some have overlapping tags:
<tag1 attribute1="at1" attribute2="at2">This is an example sentence
<tag3 attribute5="at5">containing a nested example sentence</tag3></tag1>
我的文件是我用工具注释的文本
创建这些xml文件。
文本的某些部分包含在xml标记中,而其他部分则不是。
一些
甚至包含在多个标记中。
有些有重叠的标签:
这是一个例句
包含嵌套示例语句的

每当我使用类似ElementTree的函数来解析文件时,我只能访问第一个标记。我正在寻找一种方法来解析所有的标签,而不是像树一样的结构。非常感谢您的帮助。

如果每行有一个XML片段,只需单独解析每行即可

for line in some_file:
    # parse using ET and getroot.

如果您的示例是正确的,那么这是无效的XML。在第二种情况下,打开tag1,打开tag2,关闭tag1!有些库试图猜测格式不正确的XML,但请首先确认您的示例是正确的。另外,发布您当前如何尝试访问元素。根据定义,XML格式正确。此标记不能在etree之类的兼容XML库中使用。现在,如果这一切包装在根标签,你没有张贴,那么它可能是有效的。