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字符串,如下所示: <?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:geonode="http://www.geonode.org/" xmlns:gml="http://www.opengis.net/gml" elementFormDefault="qualified" targetNamespace="h

我有一个XML字符串,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:geonode="http://www.geonode.org/" xmlns:gml="http://www.opengis.net/gml" elementFormDefault="qualified" targetNamespace="http://www.geonode.org/">
  <xsd:import namespace="http://www.opengis.net/gml" schemaLocation="http://localhost:8080/geoserver/schemas/gml/3.1.1/base/gml.xsd"/>
  <xsd:complexType name="test_24Type">
    <xsd:complexContent>
      <xsd:extension base="gml:AbstractFeatureType">
        <xsd:sequence>
          <xsd:element maxOccurs="1" minOccurs="0" name="attribute_1" nillable="true" type="xsd:string"/>
          <xsd:element maxOccurs="1" minOccurs="0" name="the_geom" nillable="true" type="gml:MultiLineStringPropertyType"/>
        </xsd:sequence>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>
  <xsd:element name="test_24" substitutionGroup="gml:_Feature" type="geonode:test_24Type"/>
</xsd:schema>
我知道有这样一个库:从xml.etree导入cElementTree作为ET
但我不知道如何正确使用它来提取此元素上的信息。

以下是使用xml.etree库和python 2.7从xml文件提取数据的方法之一。 替换代码中相应位置的xml文件名和标记名

import xml.etree.ElementTree as XT
dataTree = XT.parse('your_xml_file_name.xml')
dataRoot = dataTree.getroot()
geoNode = dataRoot.find('your_tag_name').text
print geoNode

谢谢我没有文件,但有一个存储XML字符串的变量。在这种情况下,可以使用变量名代替文件名。请参阅这些答案以获得更详细的解决方案:问题是关于提取
http://www.geonode.org/
来自
xmlns:geonode=”http://www.geonode.org/“
命名空间声明。这段代码不能做到这一点。
import xml.etree.ElementTree as XT
dataTree = XT.parse('your_xml_file_name.xml')
dataRoot = dataTree.getroot()
geoNode = dataRoot.find('your_tag_name').text
print geoNode