使用XPath和Python从XML获取行
我想使用以下方法从XML中获取一些行(z:rowrows):使用XPath和Python从XML获取行,python,xml,xpath,Python,Xml,Xpath,我想使用以下方法从XML中获取一些行(z:rowrows): <rs:data> <z:row Attribute1="1" Attribute2="1" /> <z:row Attribute1="2" Attribute2="2" /> <z:row Attribute1="3" Attribute2="3" /> <z:row Attribute1="4" Attribute2="4" />
<rs:data>
<z:row Attribute1="1" Attribute2="1" />
<z:row Attribute1="2" Attribute2="2" />
<z:row Attribute1="3" Attribute2="3" />
<z:row Attribute1="4" Attribute2="4" />
<z:row Attribute1="5" Attribute2="5" />
<z:row Attribute1="6" Attribute2="6" />
</rs:data>
我认为这两点在那种情况下是无效的
有人知道我该怎么做吗?如果您不想正确设置名称空间,可以这样忽略它们:
XPathGet("//*[local-name() = 'row']")
ElementTree.parse("r.xml").getroot().findall('{http://example.com/z}row')
# => [<Element {http://example.com/z}row at 551ee0>, <Element {http://example.com/z}row at 551c60>, <Element {http://example.com/z}row at 551f08>, <Element {http://example.com/z}row at 551be8>, <Element {http://example.com/z}row at 551eb8>, <Element {http://example.com/z}row at 551f30>]
它选择名称(不带命名空间)为
行的每个节点
如果不想正确设置命名空间,可以这样忽略它们:
XPathGet("//*[local-name() = 'row']")
ElementTree.parse("r.xml").getroot().findall('{http://example.com/z}row')
# => [<Element {http://example.com/z}row at 551ee0>, <Element {http://example.com/z}row at 551c60>, <Element {http://example.com/z}row at 551f08>, <Element {http://example.com/z}row at 551be8>, <Element {http://example.com/z}row at 551eb8>, <Element {http://example.com/z}row at 551f30>]
它选择名称(不带名称空间)为行的每个节点,“z:”前缀表示一个XML名称空间。您需要找出该名称空间是什么,并执行以下操作:
XmlDocument doc = new XmlDocument();
doc.Load(@"File.xml");
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("z", @"http://thenamespace.com");
XmlNodeList nodes = doc.SelectNodes(@"//z:row", ns);
“z:”前缀表示一个XML名称空间。您需要找出该名称空间是什么,并执行以下操作:
XmlDocument doc = new XmlDocument();
doc.Load(@"File.xml");
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("z", @"http://thenamespace.com");
XmlNodeList nodes = doc.SelectNodes(@"//z:row", ns);
如果我这样定义名称空间:
<?xml version="1.0"?>
<rs:data xmlns="http://example.com" xmlns:rs="http://example.com/rs" xmlns:z="http://example.com/z">
<z:row Attribute1="1" Attribute2="1" />
<z:row Attribute1="2" Attribute2="2" />
<z:row Attribute1="3" Attribute2="3" />
<z:row Attribute1="4" Attribute2="4" />
<z:row Attribute1="5" Attribute2="5" />
<z:row Attribute1="6" Attribute2="6" />
</rs:data>
Python-API可以这样使用:
XPathGet("//*[local-name() = 'row']")
ElementTree.parse("r.xml").getroot().findall('{http://example.com/z}row')
# => [<Element {http://example.com/z}row at 551ee0>, <Element {http://example.com/z}row at 551c60>, <Element {http://example.com/z}row at 551f08>, <Element {http://example.com/z}row at 551be8>, <Element {http://example.com/z}row at 551eb8>, <Element {http://example.com/z}row at 551f30>]
ElementTree.parse(“r.xml”).getroot().findall('{http://example.com/z}行')
# => [, , , ]
如果我这样定义名称空间,请参见:
<?xml version="1.0"?>
<rs:data xmlns="http://example.com" xmlns:rs="http://example.com/rs" xmlns:z="http://example.com/z">
<z:row Attribute1="1" Attribute2="1" />
<z:row Attribute1="2" Attribute2="2" />
<z:row Attribute1="3" Attribute2="3" />
<z:row Attribute1="4" Attribute2="4" />
<z:row Attribute1="5" Attribute2="5" />
<z:row Attribute1="6" Attribute2="6" />
</rs:data>
Python-API可以这样使用:
XPathGet("//*[local-name() = 'row']")
ElementTree.parse("r.xml").getroot().findall('{http://example.com/z}row')
# => [<Element {http://example.com/z}row at 551ee0>, <Element {http://example.com/z}row at 551c60>, <Element {http://example.com/z}row at 551f08>, <Element {http://example.com/z}row at 551be8>, <Element {http://example.com/z}row at 551eb8>, <Element {http://example.com/z}row at 551f30>]
ElementTree.parse(“r.xml”).getroot().findall('{http://example.com/z}行')
# => [, , , ]
另请参见XPathGet是函数吗?从什么编程环境考虑XPATGET作为返回所有节点的函数?XPATGET是一个函数吗?从什么编程环境考虑XPATGET作为返回所有节点的函数。LXML教程可能更有用:只需记住“导入LXML .ETRE”=“导入XML.EtEr.EntErrestEtEt作为EtRE”,除了少数例外。LXML教程可能更有帮助:只需记住“导入LXML .ETRE”==“将xml.etree.ElementTree作为etree导入”,但有几个例外。