PHP的SimpleXMLElement解析问题

时间:2013-03-20 05:06:01

标签: php xml simplexml

我正在尝试解析以下回复:

SimpleXMLElement Object ( 
    [responseType] => SimpleXMLElement Object ( 
        [inputConceptName] => aspirin [inputKindName] => % ) 
    [groupConcepts] => SimpleXMLElement Object ( 
        [concept] => Array ( 
            [0] => SimpleXMLElement Object ( 
                [conceptName] => ASPIRIN 
                [conceptNui] => N0000145918 
                [conceptKind] => DRUG_KIND ) 
            [1] => SimpleXMLElement Object ( #
                [conceptName] => Aspirin 
                [conceptNui] => N0000006582 
                [conceptKind] => INGREDIENT_KIND ) 
) ) ) 
PHP中的

。我将它存储为卷曲字符串:

$xml = simplexml_load_string($data);
print_r($xml);

如何将第一个对象的conceptNui作为PHP变量?

2 个答案:

答案 0 :(得分:1)

看一下文档,它提供了一个简单的例子来理解提取数据的友好语法:

http://www.php.net/manual/en/simplexml.examples-basic.php

在您的情况下,可能是:

$xml->groupConcepts->concept[0]->conceptNui

正如您的结构

SimpleXMLElement Object ( 
  [responseType] => SimpleXMLElement Object ( 
    [inputConceptName] => aspirin 
    [inputKindName] => % ) 
  [groupConcepts] => SimpleXMLElement Object ( 
    [concept] => Array ([0] => SimpleXMLElement Object ( 
                    [conceptName] => ASPIRIN 
                    [conceptNui] => N0000145918 
                    [conceptKind] => DRUG_KIND ) 
              [1] => SimpleXMLElement Object ( 
                    [conceptName] => Aspirin 
                    [conceptNui] => N0000006582 
                    [conceptKind] => INGREDIENT_KIND ) 
              )
          )
  )

答案 1 :(得分:1)

   $conceptNui = $mainObj->groupConcepts->concept[0]->conceptNui;