如何在XML文件中查找特定标签并使用Python和minidom访问其父标签

2024-05-16 11:28:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试编写一些代码,这些代码将在XML文章文件中搜索标记中包含的特定DOI。当它找到正确的DOI后,我希望它访问与该DOI相关的文章的<title>和{}文本。在

我的XML文件格式如下:

<root>
 <article>
  <number>
   0 
  </number>
  <DOI>
   10.1016/B978-0-12-381015-1.00004-6 
  </DOI>
  <title>
   The patagonian toothfish biology, ecology and fishery. 
  </title>
  <abstract>
   lots of abstract text
  </abstract>
 </article>
 <article>
  ...All the article tags as shown above...
 </article>
</root>

我希望脚本找到doi10.1016/B978-0-12-381015-1.00004-6的文章(例如),然后让我能够访问相应的<title>和{}标记中的<article>。在

{我一直在努力调整代码:

^{pr2}$

但我不太清楚我在做什么!在

谢谢你的帮助。在


Tags: 文件the代码标记文本abstractnumbertitle
2条回答

迷你身份是必要条件吗?用lxml和XPath解析它会非常容易。在

from lxml import etree
datasource = open('/Users/philgw/Dropbox/PW-Honours-Project/Code/processed.xml').read()
tree = etree.fromstring(datasource)
path = tree.xpath("//article[DOI="10.1016/B978-0-12-381015-1.00004-6") 

这将得到指定DOI的文章。在

另外,标记之间似乎有空格。我不知道这是否是因为Stackoverflow格式。这可能就是为什么你不能将它与minidom相匹配。在

imho-只要在python文档中查找就行了! 试试这个(未测试):

from xml.dom import minidom

xmldoc = minidom.parse(datasource)   

def get_xmltext(parent, subnode_name):
    node = parent.getElementsByTagName(subnode_name)[0]
    return "".join([ch.toxml() for ch in node.childNodes])

matchingNodes = [node for node in xmldoc.getElementsByTagName("article")
           if get_xmltext(node, "DOI") == '10.1016/B978-0-12-381015-1.00004-6']

for node in matchingNodes:
    print "title:", get_xmltext(node, "title")
    print "abstract:", get_xmltext(node, "abstract")

相关问题 更多 >