Embed XML in node using string

I restore the children nodes by storing them in the array as strings, spoofing them in XML, inserting the new node child into the array as a string ... now I want to loop through the array and write them back to the original node. The problem is that I can not find anything about how to add a child of a node using a string.

See my code below. Thanks!!!

$xml = simplexml_load_file($url);

    $questionGroup = $xml->qa[intval($id)];

    $children = array(); //  create empty array

    foreach ($questionGroup->children() as $element) {  //  loop thru children
        array_push($children, $element->asXML()); // save XML into array
    }
    //unset($questionGroup->answer);
    //unset($questionGroup->question);

    //create new node
    $newNode = '<answer><title>'.$title.'</title><description>'.$description.'</description><subName>'.$subName.'</subName><date>'.$date.'</date><timestamp>'.$timestamp.'</timestamp></answer>';

    echo "children count: ".count($children);
    echo "<br /><br />";
    print_r($children);
    echo "<br /><br />";
    // insert new
    array_splice($children,intval($elementIndex),0,$newNode);
    echo "children count: ".count($children);
    echo "<br /><br />";
    print_r($children);
    echo "<br /><br />";
    echo $questionGroup->asXML();
    foreach ($children as $element) {  //  loop thru array 
        echo "<br /><br />";
        echo $element;
        //$questionGroup->addChild(simplexml_load_string($element));  //  add array element to the empty questionGroup

    } 
        echo "<br /><br />";
    echo "questionGroup: ".$questionGroup;

UPDATE: I found a function that I modified and was able to work:

function append_simplexml(&$simplexml_to, &$simplexml_from)
{
   $childNode = $simplexml_to->addChild($simplexml_from->getName(), "");
    foreach ($simplexml_from->children() as $simplexml_child)
    {
       $simplexml_temp = $childNode->addChild($simplexml_child->getName(), (string) $simplexml_child);
       foreach ($simplexml_child->attributes() as $attr_key => $attr_value)
       {
          $simplexml_temp->addAttribute($attr_key, $attr_value);
       }

      // append_simplexml($simplexml_temp, $simplexml_child);
    }
} 

With this use in my foreach () loop:

foreach ($children as $element) {  //  loop thru array 
    append_simplexml($questionGroup, new SimpleXMLElement($element));
}
+3
source share
4 answers

I found a function that I modified and was able to get work:

function append_simplexml(&$simplexml_to, &$simplexml_from)
{
   $childNode = $simplexml_to->addChild($simplexml_from->getName(), "");
    foreach ($simplexml_from->children() as $simplexml_child)
    {
           $simplexml_temp = $childNode->addChild($simplexml_child->getName(), (string) $simplexml_child);
           foreach ($simplexml_child->attributes() as $attr_key => $attr_value)
       {
              $simplexml_temp->addAttribute($attr_key, $attr_value);
       }

      // append_simplexml($simplexml_temp, $simplexml_child);
    }
}

With this use in my foreach () loop:

foreach ($children as $element) {  //  loop thru array 
    append_simplexml($questionGroup, new SimpleXMLElement($element));
}
+4
source

SimpleXML. DOM DOMDocumentFragment. ( , , ).

$xml = simplexml_load_string('<root><parent/></root>');
// get the parent node under which you want to insert your XML fragement
$parent = dom_import_simplexml($xml->parent);
// create the XML fragment
$fragment = $parent->ownerDocument->createDocumentFragment();
// append the XML literal to your fragment
$fragment->appendXML('<child id="1"/><child id="2"><grandchild/></child>');
// append the fragment to the parent node
$parent->appendChild($fragment);
echo $xml->asXML();
/*
 * <?xml version="1.0"?>
 * <root><parent><child id="1"/><child id="2"><grandchild/></child></parent></root>
 */

:

+8

Thanks Justin. I saved your append_simplexml () since I found it a useful source of my own version.

After reading the code, I think that it does not copy the attributes from the root of $ simplexml_to - only its descendants. Of course, this is a problem or not, it depends on the use case :).

0
source

You should do something like this:

$child = new SimpleXMLElement($newNode);

$yourxmlobject->addChild($child);
-1
source

Source: https://habr.com/ru/post/1722611/


All Articles