所以这是我的代码:
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;
}
}
如您所见,我需要将Object添加到每个$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。
有什么想法吗?
更新
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>
记录中
更新
如果我替换它:
if($xml !== FALSE)
{
$record->newChild = $xml->newChaldData;
}
有了这个:
if($xml !== FALSE)
{
$record->newChild = $xml->newChaldData->child1;
}
有效!但那不是我真正需要的。
答案 0 :(得分: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>