如何从xml对象中获取XML的字符串值,包括其他标记

时间:2010-11-26 02:29:14

标签: php simplexml

如果可能的话,使用PHP和simplexml,我想解析下面的示例文档,以提取文本标记中包含的内容的全部值,包括段落,内容和其他任何标记。

<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&#160;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>

当我在php / simplexml中引用它时,它什么都不返回。

$message .= $xml->section->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&#160;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" />

非常感谢,因为我确信有一个简单的解决方案,我忽略了。

2 个答案:

答案 0 :(得分:1)

文字元素的数据应该用 CDATA 打包。

应该是这样的:

<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&#160;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>

答案 1 :(得分:0)

使用asXML方法。它将为您提供您询问的节点的xml,如下所示:(未测试)

$message = $xml->section->text->asXML();