Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.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

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

Python读取具有多个命名空间的xml文件

Python读取具有多个命名空间的xml文件,python,xml,Python,Xml,您的输入XML文档应如下所示才有效: It should eliminate the <region:tag1> contents that is coming from <Location:Region222>. 输出: import xml.etree.ElementTree as ET tree = ET.parse("yourfile.xml") root = tree.getroot() tag1 = root.find('.//{http://

您的输入XML文档应如下所示才有效:

      It should eliminate the <region:tag1> contents that is coming from <Location:Region222>.
输出:

import xml.etree.ElementTree as ET

tree = ET.parse("yourfile.xml")
root = tree.getroot()
tag1 = root.find('.//{http://some/www.hello.com}tag1')  # accessing tag with namespace

print(tag1.text)

显示您的努力不要在注释中发布输入xml内容-将其移动到问题显示最终输出的外观考虑到存在多个区域:tag1标记,并且它们具有子标记如何处理子内容,包括属性值1,2使用更多指定更新代码。感谢您宝贵的帮助支持。@sudarsan,欢迎,如果已经解决了问题,您可以接受答案。上面给出的解决方案只用于获取第一个元素,我们如何获取的所有内容。如果xml文件如下所示。一些内容。 . . . 其他一些内容请提供帮助。@sudarsan,更新您的xml并将最终结果发布到问题中
      It should eliminate the <region:tag1> contents that is coming from <Location:Region222>.
<?xml version="1.0" encoding="UTF-8"?>
<country:list version="3.0" xmlns:country="http://Some/www.home.com" xmlns:region="http://some/www.hello.com">
<country:Region111>
     <Some_child_tags>
      <region:tag1>some contents</region:tag1>
     </Some_child_tags>
</country:Region111>
</country:list>
import xml.etree.ElementTree as ET

tree = ET.parse("yourfile.xml")
root = tree.getroot()
tag1 = root.find('.//{http://some/www.hello.com}tag1')  # accessing tag with namespace

print(tag1.text)
some contents