使用 PHP 和 simplexml 从 XML 中检索 CDATA 内容 [英] Retrieving CDATA contents from XML using PHP and simplexml

查看:31
本文介绍了使用 PHP 和 simplexml 从 XML 中检索 CDATA 内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 XML 结构:

I have the following XML structure:

<?xml version="1.0" ?>
<course xml:lang="nl">
  <body>
    <item id="787900813228567" view="12000" title="0x|Beschrijving" engtitle="0x|Description"><![CDATA[Dit college leert studenten hoe ze een onderzoek kunn$
    <item id="5453116633894965" view="12000" title="0x|Onderwijsvorm" engtitle="0x|Method of instruction"><![CDATA[instructiecollege]]></item>
    <item id="7433550075448316" view="12000" title="0x|Toetsing" engtitle="0x|Examination"><![CDATA[Opdrachten/werkstuk]]></item>
    <item id="015071401858970545" view="12000" title="0x|Literatuur" engtitle="0x|Required reading"><![CDATA[Wayne C. Booth, Gregory G. Colomb, Joseph M. Wi$
    <item id="5960589172957031" view="12000" title="0x|Uitbreiding" engtitle="0x|Expansion"><![CDATA[]]></item>
    <item id="3610066867901779" view="12000" title="0x|Aansluiting" engtitle="0x|Place in study program"><![CDATA[]]></item>
    <item id="19232369892482925" view="12000" title="0x|Toegangseisen" engtitle="0x|Course requirements"><![CDATA[]]></item>
    <item id="3332396346891524" view="12000" title="0x|Doelgroep" engtitle="0x|Target audience"><![CDATA[]]></item>
    <item id="6606851872934866" view="12000" title="0x|Aanmelden bij" engtitle="0x|Enrollment at"><![CDATA[]]></item>
    <item id="1478643580820973" view="12000" title="0x|Informatie bij" engtitle="0x|Information at"><![CDATA[Docent]]></item>
    <item id="9710608434763993" view="12000" title="0x|Rooster" engtitle="0x|Schedule"><![CDATA[1e semester, maandag 15.00-17.00, zaal 1175/030]]></item>
  </body>
</course>

我想从其中一个项目标签中获取数据.要访问此标签,我使用以下 xpath:

I want to get the data from one of the item tags. To get to this tag, I use the following xpath:

$description = $xml->xpath("//item[@title='0x|Beschrijving']");

这确实以以下形式返回一个数组:

This does indeed return an array in the form of:

Array
(
    [0] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [id] => 787900813228567
                    [view] => 12000
                    [title] => 0x|Beschrijving
                    [engtitle] => 0x|Description
                )
        )
)

但是实际信息(存储在项目标签之间)在哪里?我一定是做错了什么,但我无法弄清楚那可能是什么......可能真的很简单......帮助将不胜感激.

But where is the actual information (that is stored between the item tags) located? I must be doing something wrong, but I can't figure out what that might be... Probably something really simple... Help would be appreciated.

推荐答案

当你加载 XML 文件时,你需要处理 CDATA.. 这个例子有效:

When you load the XML file, you'll need to handle the CDATA.. This example works:

<?php
$xml = simplexml_load_file('file.xml', NULL, LIBXML_NOCDATA);
$description = $xml->xpath("//item[@title='0x|Beschrijving']");
var_dump($description);
?>

输出如下:

array(1) {
  [0]=>
  object(SimpleXMLElement)#2 (2) {
    ["@attributes"]=>
    array(4) {
      ["id"]=>
      string(15) "787900813228567"
      ["view"]=>
      string(5) "12000"
      ["title"]=>
      string(15) "0x|Beschrijving"
      ["engtitle"]=>
      string(14) "0x|Description"
    }
    [0]=>
    string(41) "Dit college leert studenten hoe ze een on"
  }
}

这篇关于使用 PHP 和 simplexml 从 XML 中检索 CDATA 内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆