Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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/4/postgresql/10.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 ElementTree提取xml属性_Python_Xml_Xpath_Elementtree - Fatal编程技术网

如何使用Python ElementTree提取xml属性

如何使用Python ElementTree提取xml属性,python,xml,xpath,elementtree,Python,Xml,Xpath,Elementtree,用于: 抛出错误。这将找到名为bar的元素的第一个实例,并返回属性键的值 xml.findtext("./bar[@key]") 你的表情: In [52]: import xml.etree.ElementTree as ET In [53]: xml=ET.fromstring(contents) In [54]: xml.find('./bar').attrib['key'] Out[54]: 'value' /bar[@key] 它的意思是:bar具有key属性的子项 如果要选择

用于:


抛出错误。

这将找到名为
bar
的元素的第一个实例,并返回属性
键的值

xml.findtext("./bar[@key]")
你的表情:

In [52]: import xml.etree.ElementTree as ET

In [53]: xml=ET.fromstring(contents)

In [54]: xml.find('./bar').attrib['key']
Out[54]: 'value'
/bar[@key]

它的意思是:
bar
具有
key
属性的子项

如果要选择属性,请使用以下相对表达式:

In [52]: import xml.etree.ElementTree as ET

In [53]: xml=ET.fromstring(contents)

In [54]: xml.find('./bar').attrib['key']
Out[54]: 'value'
它的意思是:
子项的
属性


当然,您需要考虑使用一个完全兼容的XPath引擎,如./P>< P><强>使用EntErthEng/St>>/P>在XML中获取子标记的属性值。 解析XML文件并获取

标记,然后使用
[0]
将为我们提供第一个子标记。类似地,
[1],[2]
为我们提供了后续的子标记。获取子标记后,使用
.attrib[attribute\u name]
获取该属性的值

bar/@key

通过以下方法,您可以从xml(在字典中)获取所有属性

将xml.etree.ElementTree导入为etree
xmlString=“全球网络编程挑战2019-12-25T12:00:00”
xml=etree.fromstring(xmlString)
def get_attr(xml):
属性=[]
对于(xml)中的子项:
如果len(child.attrib)!=0:
attributes.append(child.attrib)
获取属性(子项)
返回属性
属性=获取属性(xml)
打印(属性)

dipenparmar12函数不会返回children的子属性。此功能将

import xml.etree.ElementTree as etree
xmlString= "<feed xml:lang='en'><title>World Wide Web</title><subtitle lang='en'>Programming challenges</subtitle><link rel='alternate' type='text/html' href='http://google.com/'/><updated>2019-12-25T12:00:00</updated></feed>"
xml= etree.fromstring(xmlString)  

def get_attr(xml):
    attributes = []
    for child in (xml):
        if len(child.attrib)!= 0:
            attributes.append(child.attrib)
        get_attr(child)
    return attributes
attributes = get_attr(xml)

print(attributes)

不确定是ElementTree还是Google App Engine,但使用“@”会引发语法错误(“不支持的路径语法(%s)”%op)语法错误:不支持的路径语法(@)将Merydith:请阅读我的最后一句话。基本
ElementTree
API它不是一个完整的XPath引擎…好的。我会看看是否能找到一个可以在GAE/Py2.5.5上工作的模块。python ElementTree似乎不支持像
bar/@key
这样的语法,您必须使用
xxx.attribute.get(“key”)
对于适当的
xxx
。我在说AttributeError时出错:“内置函数”或“方法”对象没有属性“fromstring”@Stevoisiak,我有类似的问题
Car
我想从属性中访问“Car”。
>>> tree = ET.parse('file.xml')
>>> root = tree.getroot()
import xml.etree.ElementTree as etree
xmlString= "<feed xml:lang='en'><title>World Wide Web</title><subtitle lang='en'>Programming challenges</subtitle><link rel='alternate' type='text/html' href='http://google.com/'/><updated>2019-12-25T12:00:00</updated></feed>"
xml= etree.fromstring(xmlString)  

def get_attr(xml):
    attributes = []
    for child in (xml):
        if len(child.attrib)!= 0:
            attributes.append(child.attrib)
        get_attr(child)
    return attributes
attributes = get_attr(xml)

print(attributes)
import xml.etree.ElementTree as etree
xml= etree.fromstring(xmlString) 


 def get_attr(xml, attributes):
     for child in (xml):
         if len(child.attrib)!= 0:
             attributes.append(child.attrib)
         get_attr(child,attributes)
     return attributes

  attributes = get_attr(xml,[])
  print(attributes)