1

所以这是我的代码:

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] => 



            )

    )

我知道我在 .xml 文件中有 XML $result

有任何想法吗?

更新:

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;
}

有用!但这并不是我真正需要的。

4

1 回答 1

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>
于 2013-04-26T23:56:01.267 回答