【发布时间】:2015-04-27 02:28:48
【问题描述】:
使用 elementtree,有没有一种简单的方法来解析整个 xml 文档,除了那些在具有某些特定属性值的节点上的文本。作为一个例子,我想解析文件,除了属性name="Liechtenstein"和属性month="08"
<data>
<country name="Liechtenstein">
<rank updated="yes">2</rank>
<language>english</language>
<currency>1.21$/kg</currency>
<gdppc month="06">141100</gdppc>
<gdpnp month="10">2.304e+0150</gdpnp>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<rank updated="yes">5</rank>
<language>english</language>
<currency>4.1$/kg</currency>
<gdppc month="05">59900</gdppc>
<gdpnp month="08">5.2e-015</gdpnp>
<neighbor name="Malaysia" direction="N"/>
</country>
<country name="Lahore">
<rank updated="yes">8</rank>
<language>Pertr</language>
<currency>7.3$/kg</currency>
<gdppc month="010">34000</gdppc>
<gdpnp month="099">3.4e+015</gdpnp>
<neighbor name="Peru" direction="N"/>
</country>
</data>
基于以上,我想返回以下5、english、4.1$/kg、59900、8、Pertr、7.3$/kg、34000、3.4e+015 .我觉得可以使用iterparse,但我不知道该怎么做。
感谢您的建议
【问题讨论】:
-
你是在使用
import xml.etree.ElementTree解析?? -
是的 vivek,问题是我需要动态地执行此操作,并且 xml 嵌套很深。不需要的名称和属性可以位于任何层次结构中,而不是此处显示的特定顺序。有没有办法像在 xpath 中使用 elementtree 那样做
//*[not(@08 or 09 or 10)]? -
是的,只需在列表中添加忽略值,即 `["08", "09", "10"]`。但是你能剪切有效的测试用例,即stackoverflow上的输入和相应的输出,或者给我发电子邮件vivekbsable@gmail.com ??
标签: python xml xml-parsing elementtree