在python中解析XML。获取所选节点内的所有子节点值

时间:2018-02-01 10:37:43

标签: python xml python-2.7 xpath xml-parsing

我有以下XML,我试图在python中解析:

<abstract>
    <title>Abstract</title>
    <p>Amphinomids, more commonly known as fireworms, are a basal lineage of marine annelids characterized by the presence of defensive dorsal calcareous chaetae, which break off upon contact. It has long been hypothesized that amphinomids are venomous and use the chaetae to inject a toxic substance. However, studies investigating fireworm venom from a morphological or molecular perspective are scarce and no venom gland has been identified to date, nor any toxin characterized at the molecular level. To investigate this question, we analyzed the transcriptomes of three species of fireworms—
        <italic>Eurythoe complanata</italic>
        , 
        <italic>Hermodice carunculata</italic>
        , and 
        <italic>Paramphinome jeffreysii</italic>
        —following a venomics approach to identify putative venom compounds. Our venomics pipeline involved de novo transcriptome assembly, open reading frame, and signal sequence prediction, followed by three different homology search strategies: BLAST, HMMER sequence, and HMMER domain. Following this pipeline, we identified 34 clusters of orthologous genes, representing 13 known toxin classes that have been repeatedly recruited into animal venoms. Specifically, the three species share a similar toxin profile with C-type lectins, peptidases, metalloproteinases, spider toxins, and CAP proteins found among the most highly expressed toxin homologs. Despite their great diversity, the putative toxins identified are predominantly involved in three major biological processes: hemostasis, inflammatory response, and allergic reactions, all of which are commonly disrupted after fireworm stings. Although the putative fireworm toxins identified here need to be further validated, our results strongly suggest that fireworms are venomous animals that use a complex mixture of toxins for defense against predators.
    </p>
</abstract>

我正在尝试检索<abstract>节点(包括子节点)之间的所有文本。我可以迭代到节点并获取文本,但迭代停在'最深的节点':

import xml.etree.ElementTree as ET

resXML = ET.fromstring(response)
abstract = resXML.find(".//abstract").iter()
for section in abstract:
    print section.text

> Abstract 
> Amphinomids, more commonly known as fireworms, are a basal
> lineage of marine annelids characterized by the presence of defensive
> dorsal calcareous chaetae, which break off upon contact. It has long
> been hypothesized that amphinomids are venomous and use the chaetae to
> inject a toxic substance. However, studies investigating fireworm
> venom from a morphological or molecular perspective are scarce and no
> venom gland has been identified to date, nor any toxin characterized
> at the molecular level. To investigate this question, we analyzed the
> transcriptomes of three species of fireworms— 
> Eurythoe complanata
> Hermodice carunculata 
> Paramphinome jeffreysii

显然我的做法并不完全实现。斜体物种或段落的其余部分之间没有逗号:'-following a venomics...'

如何遍历所选节点下的所有节点?

1 个答案:

答案 0 :(得分:1)

ElementTree模型中,元素之后(跟随兄弟之后)的文本节点存储为该元素的尾部,而不是父元素的text。因此除了section.text之外,您还需要查看section.tail

>>> section in abstract:
...     print section.text.strip()
...     if section.tail:
...         print section.tail.strip()
... 

Abstract

Amphinomids, more commonly known as fireworms, are a basal lineage of marine annelids characterized by the presence of defensive dorsal calcareous chaetae, which break off upon contact. It has long been hypothesized that amphinomids are venomous and use the chaetae to inject a toxic substance. However, studies investigating fireworm venom from a morphological or molecular perspective are scarce and no venom gland has been identified to date, nor any toxin characterized at the molecular level. To investigate this question, we analyzed the transcriptomes of three species of fireworms—

Eurythoe complanata
,
Hermodice carunculata
, and
Paramphinome jeffreysii
—following a venomics approach to identify putative venom compounds. Our venomics pipeline involved de novo transcriptome assembly, open reading frame, and signal sequence prediction, followed by three different homology search strategies: BLAST, HMMER sequence, and HMMER domain. Following this pipeline, we identified 34 clusters of orthologous genes, representing 13 known toxin classes that have been repeatedly recruited into animal venoms. Specifically, the three species share a similar toxin profile with C-type lectins, peptidases, metalloproteinases, spider toxins, and CAP proteins found among the most highly expressed toxin homologs. Despite their great diversity, the putative toxins identified are predominantly involved in three major biological processes: hemostasis, inflammatory response, and allergic reactions, all of which are commonly disrupted after fireworm stings. Although the putative fireworm toxins identified here need to be further validated, our results strongly suggest that fireworms are venomous animals that use a complex mixture of toxins for defense against predators.