2017-02-23 12 views
2

私はPHPで書かれたWebサービスに取り組んでいます。 IEEE Explore APIを使用してデータを表示する必要があります。これはREST APIです。以下のようにXMLを出力します。PHPを使ってCDATA XMLを解析するには?

foreachループを使用してこのデータを解析します。データを取得しようとしました。しかし問題は、すべてのタグ値がCDATAラッパーの内側にあることです。

どうすればこの問題を解決できますか?

<?xml version="1.0" encoding="UTF-8"?> 
<document> 
    <rank>1</rank> 
    <title><![CDATA[Gait Rhythm Fluctuation Analysis for Neurodegenerative Diseases by Empirical Mode Decomposition]]></title> 
    <authors><![CDATA[Peng Ren; Shanjiang Tang; Fang Fang; Lizhu Luo; Lei Xu; Maria L. Bringas-Vega; Dezhong Yao; Keith M. Kendrick; Pedro A. Valdes-Sosa]]></authors> 
    <controlledterms> 
     <term><![CDATA[diseases]]></term> 
     <term><![CDATA[feature extraction]]></term> 
     <term><![CDATA[gait analysis]]></term> 
    </controlledterms> 
    <pubtitle><![CDATA[IEEE Transactions on Biomedical Engineering]]></pubtitle> 
    <punumber><![CDATA[10]]></punumber> 
    <pubtype><![CDATA[Journals &amp; Magazines]]></pubtype> 
    <publisher><![CDATA[IEEE]]></publisher> 
    <abstract><![CDATA[Previous studies have indicated that gait rhythm of gait rhythms.]]></abstract> 
    <issn><![CDATA[0018-9294]]></issn> 
    <arnumber><![CDATA[7422732]]></arnumber> 
    <doi><![CDATA[10.1109/TBME.2016.2536438]]></doi> 
    <publicationId><![CDATA[7422732]]></publicationId> 
    <partnum><![CDATA[7422732]]></partnum> 
</document> 

答えて

-1

コードを確認してください:

<?php 
//require_once 'connection.php'; 
function xmlToArray($xml,$ns=null){ 
$a = array(); 
for($xml->rewind(); $xml->valid(); $xml->next()) { 
    $key = $xml->key(); 
    if(!isset($a[$key])) { $a[$key] = array(); $i=0; } 
    else $i = count($a[$key]); 
    $simple = true; 
    foreach($xml->current()->attributes() as $k=>$v) { 
    $a[$key][$i][$k]=(string)$v; 
    $simple = false; 
    } 
    if($ns) foreach($ns as $nid=>$name) { 
    foreach($xml->current()->attributes($name) as $k=>$v) { 
    $a[$key][$i][$nid.':'.$k]=(string)$v; 
    $simple = false; 
    } 
    } 
    if($xml->hasChildren()) { 
    if($simple) $a[$key][$i] = xmlToArray($xml->current(), $ns); 
    else $a[$key][$i]['content'] = xmlToArray($xml->current(), $ns); 
    } else { 
    if($simple) $a[$key][$i] = strval($xml->current()); 
    else $a[$key][$i]['content'] = strval($xml->current()); 
    } 
    $i++; 
} 
return $a; 
} 

$xml = new SimpleXmlIterator('test.xml', null, true); 
$namespaces = $xml->getNamespaces(true); 
// echo '<pre>'; print_r($namespaces); die; 
$arr = xmlToArray($xml,$namespaces); 
echo '<pre>'; print_r($arr); die; 
?> 

私は、フィールドの値はCDATAに常駐取得することができています。 XMLデータをtest.xmlファイルに配置します。

+0

はい動作します...ありがとう、プラカシです。 – muraliniceguy97

+0

あなたはコードを少し複雑にして単純化してください。 – muraliniceguy97

-1

解決策を提供するphp.netのドキュメントを読むときに私の問題の別の簡単な解決策が見つかりました。私は単にこのような私のURLを渡す$xml = simplexml_load_file($mainurl, 'SimpleXMLElement',LIBXML_NOCDATA);。私は私の完全なデータを取得します。

関連する問題