1

So here is my code:

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

}

As you can see i need to add Object to every $this->simpleXMLobject->path->toRecord->myRecord record.

And its not working!

In a print_r() of the final result i'm getting this:

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

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



            )

    )

I know that i have the XML in a $result.

Any ideas?

UPDATE:

The XMLs:

<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 returns this kind of data:

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

The idea is to get data from 'https://urltocasedetailes.com/blha/nlha' as xml object and add to the original xml <person> records

UPDATE

If i replace this:

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

With this:

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

It works! But that's not really what i need.

hakre
  • 193,403
  • 52
  • 435
  • 836
rinchik
  • 2,642
  • 8
  • 29
  • 46
  • 1
    Have you looked at the [SimpleXMLElement::addChild](http://php.net/manual/en/simplexmlelement.addchild.php) function? – Rolando Isidoro Apr 26 '13 at 16:29
  • tried it. same result. – rinchik Apr 26 '13 at 16:35
  • There's not enough information in your question, you'll have to show an example of the XML you're working with and what you want to do with it. – Rolando Isidoro Apr 26 '13 at 16:37
  • Your code seems to have an issue, the function you are looking for is: newChildData not newChaldData as show in your snippet. – Reid Johnson Apr 26 '13 at 18:24
  • @ReidJohnson thats a typo. it's not a real vars. – rinchik Apr 26 '13 at 19:31
  • @RolandoIsidoro you can see the simpleXMLobject structure from the code. XML is, obviously, the same. – rinchik Apr 26 '13 at 19:33
  • @rinchik You'll have to do better than that, paste the result of **echo $record->asXML()** and **echo $xml->asXML()**. – Rolando Isidoro Apr 26 '13 at 20:20
  • @RolandoIsidoro sorry i can't. really. XML contains private sensitive info. unfortunately i dont have the scheme. Will try to draw a mockup. – rinchik Apr 26 '13 at 20:59
  • SimpleXML does not support to add another SimpleXMLelement as a new children out of the box. If you try that you should also see an error message because PHP tells you. If you search for that error message you should find existing Q&A like [Is there a way to add a PHP SimpleXMLElement to another SimpleXMLElement?](http://stackoverflow.com/q/1157104/367456) or [Adding a block of XML as child of a SimpleXMLElement object](http://stackoverflow.com/q/1824568/367456) that have this solved. – hakre Apr 27 '13 at 10:31

1 Answers1

0

I'll take your mockup XMLs and try to exemplify how to add specific data to the original XML, hope this will help you:

<?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();

Output:

<?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>
Rolando Isidoro
  • 4,983
  • 2
  • 31
  • 43