Bio*_*i3c 19 python xml lxml objectify
我需要检查xml文件中是否存在某个标记.
例如,我想查看此代码段中是否存在该标记:
<main>
<elem1/>
<elem2>Hi</elem2>
<elem3/>
...
</main>
Run Code Online (Sandbox Code Playgroud)
目前,我正在使用一个带有错误检查的丑陋黑客,如下所示:
try:
if root.elem1.tag:
foo = elem1
except AttributeError:
foo = "error finding elem1"
Run Code Online (Sandbox Code Playgroud)
如果无法找到节点,我也想自定义字符串(即"无法找到-tagname-").
我必须检查一长串变量,我不想重复代码100次.
有什么建议?
编辑:
以下是实际xml文件的片段:
<main>
<asset name="Virtual Dvaered Unpresence">
<virtual/>
<presence>
<faction>Dvaered</faction>
<value>-1000.000000</value>
<range>0</range>
</presence>
</asset>
<asset name="Virtual Empire Small">
<virtual/>
<presence>
<faction>Empire</faction>
<value>100.000000</value>
<range>2</range>
</presence>
</asset>
</main>
Run Code Online (Sandbox Code Playgroud)
我想检查标签是否存在,如果是,则获取内容.
编辑编辑:好的,我将结合两个答案,但我只能投一票.抱歉.
编辑3:关于XPath的相关问题:Python lxml(objectify):Xpath麻烦
小智 30
hasattr() 适用于此:
if hasattr(root, 'elem1'):
foo = root.elem1
Run Code Online (Sandbox Code Playgroud)
编辑:样本文件的更新答案.
我假设你想搜索每个资产的某些标签.如果是这样,以下内容对我有用:
import lxml.objectify
# Parse the file.
tree = lxml.objectify.parse('sample.xml')
root = tree.getroot()
# Which elements to find.
to_find = set(['presence/faction', 'presence/value', 'fake'])
# Go through each asset in the document.
for asset in root.findall('asset'):
# Check for each element.
for name in to_find:
node = asset.find(name)
if node is not None:
print 'Found %s, its value is %s' % (name, node)
else:
print 'Unable to find %s' % name
Run Code Online (Sandbox Code Playgroud)
输出是:
Found presence/value, its value is -1000.0
Found presence/faction, its value is Dvaered
Unable to find fake
Found presence/value, its value is 100.0
Found presence/faction, its value is Empire
Unable to find fake
Run Code Online (Sandbox Code Playgroud)
假设您想获得elem2的值,您可以使用xpath来查找它.
tree = etree.parse(StringIO(htmlString), etree.HTMLParser()).getroot()
youWantValue = tree.xpath('/main/elem2')[0].text
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17164 次 |
| 最近记录: |