문제

다음 XML 구조가 있습니다.

<?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를 사용합니다.

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

이것은 실제로 배열을 다음의 형태로 반환합니다.

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

그러나 실제 정보 (항목 태그 사이에 저장)는 어디에 있습니까? 나는 뭔가 잘못하고 있어야하지만, 그것이 무엇인지 알 수는 없습니다 ... 아마도 정말 간단한 것 ... 도움이 감사 할 것입니다.

도움이 되었습니까?

해결책

XML 파일을로드하면 cdata를 처리해야합니다.이 예제는 작동합니다.

<?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"
  }
}

다른 팁

나는 객체의 __toString () 메소드와 동일하다고 생각합니다.

echo $description[0];

표시하거나 캐스트 할 수 있습니다.

$str = (string) $description[0];

"SimplexMlelement"에 대한 php.net 문서를 살펴보십시오 (http://uk.php.net/manual/en/function.simplexml-element-children.php) 노드를 문자열로 변환하는 것처럼 보입니다. "(string) $ value;" 트릭을 수행합니다.

실패하면, 그 페이지에는 올바른 방향을 가리킬 수있는 많은 예가 있습니다!

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

while(list( , $node) = each($description)) {

echo($node);

}

Dreamwerx의 솔루션이 더 좋습니다

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top