Reputation: 13
I am working with the xml labels from dailymed.nlm.nih.gov. I am having a problem when reading the Contraindications associated with a drug. I want all of the content inside of the tag, but it cuts off once I hit the inner tag. I have tried iterating over all the subelements but the best I could do was get the "Warning" to display. "; anuria; hypersensitivity to ..." was lost. If anyone know of a way to get this data using the parser it would help a lot. Thank you!
<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>
Upvotes: 0
Views: 445
Reputation: 4903
Presuming you're using something like the following you need to use ET.tostring
which will get all the text of child elements.
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 "<<"
Gives
>>
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.
<<
Upvotes: 1