简体   繁体   English

Python XML iterparse()名称空间

[英]Python XML iterparse() namespacing

According to this post , I successfully can parse my XML file, and reading it's content. 根据这篇文章 ,我可以成功解析XML文件并读取其内容。 However, if I add namespace to it, the whole thing goes wrong. 但是,如果我向其中添加名称空间,则整个过程都会出错。

Let's consider the following XML: 让我们考虑以下XML:

<root xmlns="MyNamespace">
   <A1>
     <B1></B1>
     <C>1<D1></D1></C>
     <E1></E1>
   </A1>
   <A2>
      <B2></B2>
       <C>2<D></D></C>
       <E2></E2>
   </A2>
</root>

My iterparse looks like this: 我的iterparse看起来像这样:

context = ET.iterparse('../in/process/teszt.xml', events=('end', ), tag='B1')

I found several examples, but to be honest I don't really understand them, and have no idead how to solve this problem. 我找到了几个示例,但老实说,我并不真正理解它们,也不知道如何解决此问题。

如果XML具有默认名称空间 ,则需要在tag使用名称空间URI以及元素的本地名称:

context = ET.iterparse('../in/process/teszt.xml', events=('end', ), tag='{MyNamespace}B1')

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

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