How to take string value of XML including other tags from within an xml object
Using PHP and simplexml if possible, I'd like to parse the following example document to extract the whole value of what is contained within the开发者_JAVA百科 text tag, including the paragraph, content, and whatever else tags are there.
<section>
<id root="71EB365C-A4F5-D758-0C51-B8DA375805CD" />
<code code="34066-1" codeSystem="2.16.840.1.113883.6.1" displayName="BOXED WARNING SECTION" />
<title mediaType="text/x-hl7-title+xml">USE IN PREGNANCY</title>
<text><paragraph><content styleCode="bold">When used in pregnancy during the second and third trimesters, ACE inhibitors can cause injury and even death to the developing fetus. </content>When pregnancy is detected, quinapril tablets USP should be discontinued as soon as possible. See <content styleCode="bold">WARNINGS</content>, <content styleCode="bold">Fetal/Neonatal Morbidity and Mortality</content>.<linkHtml href="#W_fetal" /></paragraph><content styleCode="bold" /><content styleCode="bold" /><content styleCode="bold" /></text>
<effectiveTime value="20070112" />
</section>
When I reference it in php/simplexml as such, it returns nothing.
$message .= $xml->section->text;
This is a simple example for a huge project where the contents of the text tag vary greatly, so I cannot just solve this one example specifically.
I'd like the output to be:
<paragraph><content styleCode="bold">When used in pregnancy during the second and third trimesters, ACE inhibitors can cause injury and even death to the developing fetus. </content>When pregnancy is detected, quinapril tablets USP should be discontinued as soon as possible. See <content styleCode="bold">WARNINGS</content>, <content styleCode="bold">Fetal/Neonatal Morbidity and Mortality</content>.<linkHtml href="#W_fetal" /></paragraph><content styleCode="bold" /><content styleCode="bold" /><content styleCode="bold" />
Many thanks, as I'm sure there is a simple solution that I'm overlooking.
The data on the text element should have been wrapped with CDATA.
It should have been like this:
<section>
<id root="71EB365C-A4F5-D758-0C51-B8DA375805CD" />
<code code="34066-1" codeSystem="2.16.840.1.113883.6.1" displayName="BOXED WARNING SECTION" />
<title mediaType="text/x-hl7-title+xml">USE IN PREGNANCY</title>
<text><![CDATA[<paragraph><content styleCode="bold">When used in pregnancy during the second and third trimesters, ACE inhibitors can cause injury and even death to the developing fetus. </content>When pregnancy is detected, quinapril tablets USP should be discontinued as soon as possible. See <content styleCode="bold">WARNINGS</content>, <content styleCode="bold">Fetal/Neonatal Morbidity and Mortality</content>.<linkHtml href="#W_fetal" /></paragraph><content styleCode="bold" /><content styleCode="bold" /><content styleCode="bold" />]]></text>
<effectiveTime value="20070112" />
</section>
Use the asXML method. It will give you the xml of the node you ask, like this: (didn't test)
$message = $xml->section->text->asXML();
精彩评论