xml python 中的 cElementTree 解析错误
xml parsing error with cElementTree in python
我一直在用 cElementTree 编写 XML 文件,当我开始使用 .parse(file)
时,我收到一条错误消息:
xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 15
XML 文件:
<material Date Created="1/23/2015 at 14:59:10 in Mountain Standard Time" Material Name="Material" Render Engine="CYCLES">
<main>
<node0 inputs="" label="" location="<Vector (-114.1876, 479.6438)>" name="Texture Coordinate" node_specific="['from_dupli', False]" outputs="" type="TEX_COORD" />
<node0 inputs="" label="" location="<Vector (87.1538, 383.3991)>" name="Attribute" node_specific="['attribute_name', '']" outputs="" type="ATTRIBUTE" />
<node0 inputs="" label="" location="<Vector (-38.2097, 246.6303)>" name="RGB" node_specific="" outputs="[0, (0.5, 0.5, 0.5, 1.0)]" type="RGB" />
</main>
</material>
我不明白为什么它不能解析它创建的文件。
您正在尝试解析 无效 有效 XML 的文档。属性名称中不能有空格,解析器需要 =
而不是更多属性名称:
<material Date Created="1/23/2015 at 14:59:10 in Mountain Standard Time"
<!-- ^ position 15 on line 1 -->
location
属性值中的 <
和 >
字符也应分别转义为 <
和 >
。
如果您替换了 material
标签上属性名称中的空格并转义了这些尖括号,则可以解析文档:
>>> from xml.etree import ElementTree
>>> sample = '''\
... <material Date_Created="1/23/2015 at 14:59:10 in Mountain Standard Time" Material_Name="Material" Render_Engine="CYCLES">
... <main>
... <node0 inputs="" label="" location="<Vector (-114.1876, 479.6438)>" name="Texture Coordinate" node_specific="['from_dupli', False]" outputs="" type="TEX_COORD" />
... <node0 inputs="" label="" location="<Vector (87.1538, 383.3991)>" name="Attribute" node_specific="['attribute_name', '']" outputs="" type="ATTRIBUTE" />
... <node0 inputs="" label="" location="<Vector (-38.2097, 246.6303)>" name="RGB" node_specific="" outputs="[0, (0.5, 0.5, 0.5, 1.0)]" type="RGB" />
... </main>
... </material>
... '''
>>> tree = ElementTree.fromstring(sample)
>>> tree
<Element 'material' at 0x1042d42d0>
我一直在用 cElementTree 编写 XML 文件,当我开始使用 .parse(file)
时,我收到一条错误消息:
xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 15
XML 文件:
<material Date Created="1/23/2015 at 14:59:10 in Mountain Standard Time" Material Name="Material" Render Engine="CYCLES">
<main>
<node0 inputs="" label="" location="<Vector (-114.1876, 479.6438)>" name="Texture Coordinate" node_specific="['from_dupli', False]" outputs="" type="TEX_COORD" />
<node0 inputs="" label="" location="<Vector (87.1538, 383.3991)>" name="Attribute" node_specific="['attribute_name', '']" outputs="" type="ATTRIBUTE" />
<node0 inputs="" label="" location="<Vector (-38.2097, 246.6303)>" name="RGB" node_specific="" outputs="[0, (0.5, 0.5, 0.5, 1.0)]" type="RGB" />
</main>
</material>
我不明白为什么它不能解析它创建的文件。
您正在尝试解析 无效 有效 XML 的文档。属性名称中不能有空格,解析器需要 =
而不是更多属性名称:
<material Date Created="1/23/2015 at 14:59:10 in Mountain Standard Time"
<!-- ^ position 15 on line 1 -->
location
属性值中的 <
和 >
字符也应分别转义为 <
和 >
。
如果您替换了 material
标签上属性名称中的空格并转义了这些尖括号,则可以解析文档:
>>> from xml.etree import ElementTree
>>> sample = '''\
... <material Date_Created="1/23/2015 at 14:59:10 in Mountain Standard Time" Material_Name="Material" Render_Engine="CYCLES">
... <main>
... <node0 inputs="" label="" location="<Vector (-114.1876, 479.6438)>" name="Texture Coordinate" node_specific="['from_dupli', False]" outputs="" type="TEX_COORD" />
... <node0 inputs="" label="" location="<Vector (87.1538, 383.3991)>" name="Attribute" node_specific="['attribute_name', '']" outputs="" type="ATTRIBUTE" />
... <node0 inputs="" label="" location="<Vector (-38.2097, 246.6303)>" name="RGB" node_specific="" outputs="[0, (0.5, 0.5, 0.5, 1.0)]" type="RGB" />
... </main>
... </material>
... '''
>>> tree = ElementTree.fromstring(sample)
>>> tree
<Element 'material' at 0x1042d42d0>