Python Lxml (objectify):检查标签是否存在

发布于 2024-10-25 12:20:40 字数 1246 浏览 4 评论 0原文

我需要检查 xml 文件中是否存在某个标记。

例如,我想查看此代码片段中是否存在该标签:

 <main>
       <elem1/>
       <elem2>Hi</elem2>
       <elem3/>
       ...
 </main>

目前,我正在使用带有错误检查的丑陋黑客,如下所示:

try:
   if root.elem1.tag:
      foo = elem1
except AttributeError:
   foo = "error finding elem1"

如果无法找到节点(即“无法查找-标记名-”)。

我必须检查一长串变量,而且我不想重复代码 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>

我想检查标签是否存在,如果存在,则获取内容。

编辑编辑: 好的,我将合并两个答案,但我只能投票给一个。抱歉。

编辑3:有关XPath的相关问题在这里:Python lxml (objectify): Xpath烦恼

I need to check whether a certain tag exists in an xml file.

For example, I want to see if the tag exists in this snippet:

 <main>
       <elem1/>
       <elem2>Hi</elem2>
       <elem3/>
       ...
 </main>

Currently, I am using an ugly hack with error checking, like this:

try:
   if root.elem1.tag:
      foo = elem1
except AttributeError:
   foo = "error finding elem1"

I also want to customize the string if it is unable to find the node (i.e. "unable to find -tagname-").

I have to check a long list of variables, and I don't want to repeat the code 100 times.

Any suggestions?

Edit:

Here is a snip of the actual xml file:

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

I want to check whether the tag exists, and, if so, to get the contents.

Edit edit:
Ok, I am going to combine two of the answers, but I can only vote for one. Sorry.

Edit 3: Related question about XPath here: Python lxml (objectify): Xpath troubles

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

荆棘i 2024-11-01 12:20:40

hasattr() 适用于此:

if hasattr(root, 'elem1'):
    foo = root.elem1

hasattr() works for this:

if hasattr(root, 'elem1'):
    foo = root.elem1
意中人 2024-11-01 12:20:40

编辑:更新示例文件的答案。

我假设您想在每个资产中搜索某些标签。如果是这样,以下内容对我有用:

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

输出是:

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

Edit: updated answer for sample file.

I'm assuming you want to search each asset for certain tags. If so, the following worked for me:

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

The output was:

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
大姐,你呐 2024-11-01 12:20:40

假设你想获取elem2的值,你可以使用xpath来查找它。

tree = etree.parse(StringIO(htmlString), etree.HTMLParser()).getroot()
youWantValue = tree.xpath('/main/elem2')[0].text

Assume you want to get elem2's value, you can use xpath to find it.

tree = etree.parse(StringIO(htmlString), etree.HTMLParser()).getroot()
youWantValue = tree.xpath('/main/elem2')[0].text
孤独陪着我 2024-11-01 12:20:40

如果您的文档相对较短,您可以迭代

的所有子级,查找与您的一组变量名称匹配的标签:

tree = lxml.etree.fromstring(DATA)
NAMES = set(['elem1', 'elem3'])
for node in tree.iterchildren():
    if node.tag in NAMES:
        print 'found', node.tag

或者您可以一次搜索每个变量名称:

for tag in ('elem1', 'elem3'):
    if tree.find(tag) is not None:
        print 'found', tag

If your document tends to be relatively short you can iterate over all children of <main> looking for tags matching your set of variable names:

tree = lxml.etree.fromstring(DATA)
NAMES = set(['elem1', 'elem3'])
for node in tree.iterchildren():
    if node.tag in NAMES:
        print 'found', node.tag

Or you can search for each variable name one at a time:

for tag in ('elem1', 'elem3'):
    if tree.find(tag) is not None:
        print 'found', tag
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文