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

发布于 2024-10-05 01:42:10 字数 1929 浏览 2 评论 0原文

如果可能的话,使用 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" />

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

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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫卆 2024-10-12 01:42:10

text 元素上的数据应使用 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 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>

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>
梦境 2024-10-12 01:42:10

使用 asXML 方法。它会给你你所询问的节点的xml,如下所示:(没有测试)

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文