2013-02-10 70 views
0

我正在使用dailymed.nlm.nih.gov的xml標籤。閱讀與藥物相關的禁忌時,我遇到了問題。我想要標籤內的所有內容,但是一旦我點擊內部標籤,它就會切斷。我試過遍歷所有的子元素,但我能做的最好的是獲得「警告」顯示。 「無尿;對...過敏」失去了。如果有人知道使用解析器獲取這些數據的方法,那將會有很大的幫助。 謝謝!Python元素樹處理標籤中的標籤XML

<component> 
     <section ID="LINK_8e9e0719-efa5-451c-bea3-d547298ad0a1"> 
      <id root="8e9e0719-efa5-451c-bea3-d547298ad0a1"/> 
      <code code="34070-3" codeSystem="2.16.840.1.113883.6.1" displayName="CONTRAINDICATIONS SECTION"/> 
      <title>CONTRAINDICATIONS</title> 
      <text> 
       <paragraph>Atenolol and chlorthalidone tablets are contraindicated in patients with: sinus bradycardia; heart block greater than first degree; cardiogenic shock; overt cardiac failure (see<content styleCode="bold"> 
        <linkHtml href="#LINK_0df2629f-13c7-4b14-8664-475c32377c68">WARNINGS</linkHtml> 
       </content>); anuria; hypersensitivity to this product or to sulfonamide-derived drugs.</paragraph> 
      </text> 
      <effectiveTime value="20101001"/> 
     </section> 
    </component> 

回答

1

假設你正在使用像你需要使用ET.tostring這將讓子元素的所有文字以下。

import xml.etree.ElementTree as ET 
txt = """ 
<component> 
<section ID="LINK_8e9e0719-efa5-451c-bea3-d547298ad0a1"> 
    <id root="8e9e0719-efa5-451c-bea3-d547298ad0a1"/> 
    <code code="34070-3" codeSystem="2.16.840.1.113883.6.1" displayName="CONTRAINDICATIONS SECTION"/ 
    <title>CONTRAINDICATIONS</title> 
    <text> 
    <paragraph>Atenolol and chlorthalidone tablets are contraindicated in patients with: sinus brady 
     <linkHtml href="#LINK_0df2629f-13c7-4b14-8664-475c32377c68">WARNINGS</linkHtml> 
      </content>); anuria; hypersensitivity to this product or to sulfonamide-derived drugs.</ 
    </text> 
    <effectiveTime value="20101001"/> 
</section> 
</component>""" 

root = ET.fromstring(txt) 

for e in root.iter('text'): 
    print ">>" 
    print ET.tostring(e, method="text") 
    print "<<" 

給人

>> 

    Atenolol and chlorthalidone tablets are contraindicated in patients with: sinus bradycardia; heart block greater than first degree; cardiogenic shock; overt cardiac failure (see 
     WARNINGS 
      ); anuria; hypersensitivity to this product or to sulfonamide-derived drugs. 


<<