使用XPath和Python从XML中获取行

0 投票
3 回答
1977 浏览
提问于 2025-04-15 13:02

我想从XML文件中获取一些行(z:row 行),使用的是:

<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>

我在使用(Python)的时候遇到了一些问题:

ElementTree.parse('myxmlfile.xml').getroot().findall('//z:row')

我觉得在这种情况下,有两个地方是不对的。

有没有人知道我该怎么做呢?

3 个回答

1

“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);
1

如果你不想花时间去弄清楚怎么正确设置命名空间,你可以像这样忽略它们:

XPathGet("//*[local-name() = 'row']")

这样做会选择所有名字是 row 的节点(不考虑命名空间)。

1

如果我这样定义命名空间:

<?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的ElementTree API:

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>]

另请参见 http://effbot.org/zone/element.htm#xml-namespaces

撰写回答