2013-04-26 108 views
1

因此,這裏是我的代碼:PHP simplexml_load_string() - 在一個循環子對象添加到對象

foreach($this->simpleXMLobject->path->toRecord->myRecord as $record) 
{ 
    $childXML = new \CurlClass\GetXML($record->link, $this->timeout); 
    $result = $childXML->get(); 
    $xml = simplexml_load_string($result, NULL, LIBXML_NOCDATA); 
    if($xml !== FALSE) 
    { 
     $record->newChild = $xml->newChaldData; 
    } 

} 

正如你可以看到我需要的對象添加到每個$this->simpleXMLobject->path->toRecord->myRecord記錄。

而且它不工作!

在最終結果的print_r()我得到這個:

[n] => SimpleXMLElement Object 
    (
     [firstname] => ANGELA 
     [lastname] => LEE 
     [dob] => SimpleXMLElement Object 
      (
      ) 

     [id] => 67404998 
     [newChild] => SimpleXMLElement Object 
      (
       [0] => 



      ) 

    ) 

我知道,我有一個$result的XML。

任何想法?

UPDATE:

的個XML:

<xml> 
<result> 
    <stats/> 
    <somadata/> 
    <somadata/> 
    <somadata/> 
    <crimeData> 
     <person> 
      <fName>Eric</fName> 
      <lName>Eric</lName> 
      <caseDetailed>data1</caseDetailes> 
     </person> 
     ... 
     <person> 
      <fName>Eric</fName> 
      <lName>Eric</lName> 
      <caseDetailes>https://urltocasedetailes.com/blha/nlha</caseDetailes> 
     </person> 
    </crimeData> 
</result> 

https://urltocasedetailes.com/blha/nlha返回此類型的數據:

<xml> 
<result> 
    <stats/> 
    <detailesPart1> 
     <data1>Eric</data1> 
     <data2>Eric</data2> 
    </detailesPart1> 
    <detailesPart2> 
     <data1>Eric</data1> 
     <data2>Eric</data2> 
    </person> 
    </crimeData> 
</result> 

的想法是擺脫 'https://urltocasedetailes.com/blha/nlha' 數據作爲XML對象,添加到原始xml <person>記錄

UPDATE

如果我替換此:

if($xml !== FALSE) 
{ 
    $record->newChild = $xml->newChaldData; 
} 

有了這個:

if($xml !== FALSE) 
{ 
    $record->newChild = $xml->newChaldData->child1; 
} 

它的工作原理!但那不是我真正需要的。

+1

有你看了[SimpleXMLElement :: addChild](http://php.net/manual/en/simplexmlelement.addchild.php)函數? – 2013-04-26 16:29:11

+0

嘗試過。相同的結果。 – rinchik 2013-04-26 16:35:37

+0

在你的問題中沒有足夠的信息,你必須展示你正在使用的XML的例子以及你想要做什麼。 – 2013-04-26 16:37:11

回答

0

我相信你的樣機XML,並嘗試舉例說明如何具體數據添加到原始的XML,希望這將幫助你:

<?php 
$originalXML = <<<XML 
<?xml version="1.0" encoding="UTF-8"?> 
<result> 
    <crimeData> 
     <person> 
      <fName>Eric</fName> 
      <lName>Eric</lName> 
      <caseDetailes>https://urltocasedetailes.com/blha/nlha</caseDetailes> 
     </person> 
    </crimeData> 
</result> 
XML; 

$childXML = <<<XML 
<?xml version="1.0" encoding="UTF-8"?> 
<result> 
    <crimeData> 
     <person> 
      <detailesPart1> 
       <data1>Eric</data1> 
       <data2>Eric</data2> 
      </detailesPart1> 
      <detailesPart2> 
       <data1>Eric</data1> 
       <data2>Eric</data2> 
      </detailesPart2> 
     </person> 
    </crimeData> 
</result> 
XML; 

$sxe  = new SimpleXMLElement($originalXML); 
$persons = $sxe->xpath('//person'); 

foreach ($persons as $person) { 
    $url = (string) $person->caseDetailes; 

    // Retrieve the XML from the external location from caseDetailes 
    // $childSxe = new SimpleXMLElement($url, 0, true); 

    // Using this just for the example purpose 
    $childSxe = new SimpleXMLElement($childXML); 

    // Retrieve detailesPart2 from your external XML 
    $detailes = $childSxe->xpath('//detailesPart2'); 

    // Add detailesPart2/data1 to the person record 
    foreach ($detailes as $detaile) { 
     $person->addChild($detaile->data1->getName(), (string) $detaile->data1); 
    } 
} 

echo $sxe->asXML(); 

輸出:

<?xml version="1.0" encoding="UTF-8"?> 
<result> 
    <crimeData> 
     <person> 
      <fName>Eric</fName> 
      <lName>Eric</lName> 
      <caseDetailes>https://urltocasedetailes.com/blha/nlha</caseDetailes> 
      <data1>Eric</data1> 
     </person> 
    </crimeData> 
</result>