2016-04-25 72 views
0

我是用PHP與XML一起工作的新手。我有一個相當複雜的XML結構,並且在laravel中使用simplexml,並且無法訪問我需要獲取的所有元素。我能夠遍歷大型的XML文件,但simpleXML每個記錄返回兩個對象,我似乎只能訪問'標題'中的元素,第一個對象返回...在Simplexml中訪問複雜結構中的元素

這裏是部分XML對象

SimpleXMLElement Object 
(
    [identifier] => RCM0635 
    [datestamp] => 2015-06-09 
) 

SimpleXMLElement Object 
(
    [lidoWrap] => SimpleXMLElement Object 
    (
     [lido] => SimpleXMLElement Object 
      (
       [lidoRecID] => RCM:1748 
       [descriptiveMetadata] => SimpleXMLElement Object 
        (
         [objectClassificationWrap] => SimpleXMLElement Object 
          (
           [objectWorkTypeWrap] => SimpleXMLElement Object 
            (
             [objectWorkType] => SimpleXMLElement Object 
              (
               [term] => musical instruments 
              ) 

            ) 

           [classificationWrap] => SimpleXMLElement Object 
            (
             [classification] => Array 
              (
               [0] => SimpleXMLElement Object 
                (
                 [term] => Cornet 
                ) 

               [1] => SimpleXMLElement Object 
                (
                 [conceptID] => SimpleXMLElement Object 
                  (
                   [@attributes] => Array 
                    (
                     [type] => SH_Class 
                    ) 

                  ) 

                ) 

              ) 

            ) 

          ) 

有了下面的代碼,我可以得到頭中的元素,但我無法弄清楚如何獲得其他元素?

$streamer = \Prewk\XmlStringStreamer::createStringWalkerParser(public_path().'/xml/many_mimo_records.xml'); 


       while ($node = $streamer->getNode()) { 

        $simpleXmlNode = simplexml_load_string($node); 

        echo (string)$simpleXmlNode->identifier; 
        echo (string)$simpleXmlNode->datestamp; 

       } 

我會爲任何建議非常感謝......

回答

0

我不知道如果我理解你,但在重大:

  1. 你的行爲對while ($node = $streamer->getNode())環是什麼意思是第一次迭代後,您會得到這個對象:

SimpleXMLElement Object ( [identifier] => RCM0635 [datestamp] => 2015-06-09 )

所以第一次可以這樣讀取它:

` $ simpleXmlNode = simplexml_load_string($ node); echo(string)$ simpleXmlNode-> identifier; echo(string)$ simpleXmlNode-> datestamp;

`

,但在第二次迭代您有:

`

SimpleXMLElement Object 
(
    [lidoWrap] => SimpleXMLElement Object 
    (
     [lido] => SimpleXMLElement Object 
      (
       [lidoRecID] => RCM:1748 
       [descriptiveMetadata] => SimpleXMLElement Object 
        (
         [objectClassificationWrap] => SimpleXMLElement Object 
          (
           [objectWorkTypeWrap] => SimpleXMLElement Object 
            (
             [objectWorkType] => SimpleXMLElement Object 
              (
               [term] => musical instruments 
              ) 

            ) 

           [classificationWrap] => SimpleXMLElement Object 
            (
             [classification] => Array 
              (
               [0] => SimpleXMLElement Object 
                (
                 [term] => Cornet 
                ) 

               [1] => SimpleXMLElement Object 
                (
                 [conceptID] => SimpleXMLElement Object 
                  (
                   [@attributes] => Array 
                    (
                     [type] => SH_Class 
                    ) 

                  ) 

                ) 

              ) 

            ) 

          ) 

`

因此,雖然裏面的代碼是錯誤的。

我sugest嘗試這樣的事:

`

while ($node = $streamer->getNode()) { 

    $simpleXmlNode = simplexml_load_string($node); 

    if (!empty($simpleXmlNode->identifier)) 
     echo (string)$simpleXmlNode->identifier; 
    if (!empty($simpleXmlNode->datestamp)) 
     echo (string)$simpleXmlNode->datestamp; 
    if (!empty($simpleXmlNode->lidoWrap)) { 
     $lido = $simpleXmlNode->lidoWrap->lido; 
     echo (string)$lido->lidoRecID; 
     // and so on as the recursive XML node objects 
    } 
} 

`