使用XPath,etree和python提取值
我尝试使用XPath,Python和etree提取值。我对我收到的.xml文件没有任何影响,我认为它似乎有些无效。使用XPath,etree和python提取值
我的方法已经提取了我想要检查的文本节点对象。
# This is the tag.
textTag = lastExportTree.xpath("//TEXT_NODE[@PROPERTY = '%s']/TEXT[@ID = '%s']" % (key, id[1]))
# This is a part of the xml. I already have the text node I want to examine.
<TEXT ID="1001" STATE="5" LOCKED="false"><SYSTEMMESSAGE>CALBUY</SYSTEMMESSAGE>Hiho</TEXT>
<TEXT ID="1002" STATE="1" LOCKED="false"/>
<TEXT ID="1003" STATE="5" LOCKED="false">Stack</TEXT>
<TEXT ID="1004" STATE="1" LOCKED="false">Overflow</TEXT>
如果我想访问ID的内容= “1003” 我只需要键入:
print(textTag.text); # Will print 'Stack'
但随着ID标签= “1001” 还包括SYSTEMMESSAGE标签。 如何访问内容'HiHo'? (textTag.text不会工作!)这是无效的XML我收到什么?
非常感谢您的回答!
我以前也遇到过这个问题,这就是我们最后的结果。在我们的例子中,我们感兴趣的是在一个元素的所有非脚本和非风格的子元素中查找文本。
# Just to pre-compile our XPath. This will get all the text from this element from
# each of the child elements that aren't 'script' or 'style'
textXpath = etree.XPath(
'(.|.//*[not(name()="script")][not(name()="style")])/text()')
# If instead you don't want to include the current element:
# textXpath = etree.XPath(
# './/*[not(name()="script")][not(name()="style")]/text()')
results = ''.join(textXpath(textTag))
它可能不是最漂亮的代码块,但这是我们所采取的。
谢谢帮助! – 2013-03-25 21:16:14
@ dan.lecocq - xpath与评论不完全匹配。它包括当前元素和所有后代(不只是儿童)。 – tdelaney 2013-03-25 21:31:12
啊,公平点。我会编辑澄清 – 2013-03-25 21:34:49
假设你向我们展示lastExportTree下的节点,这应该这样做:
lastExportTree.xpath('TEXT[@STATE="5" and @LOCKED="false" and SYSTEMMESSAGE]/text()')[0]
,说找到具有给定的状态,被锁定的特性和SYSTEMMESSAGE子元素的所有子节点命名为文本。
如果xml文件无效,当您尝试加载文档时,您的xml解析器将引发错误,所以这不是问题。您显示的xpath无效,不会选择您声明的文本节点...更准确的代码会有帮助! – tdelaney 2013-03-25 20:21:16
您是否看过etree文档中的混合内容部分?http://effbot.org/zone/element-infoset.htm#mixed-content – theodox 2013-03-25 20:28:22