使用字符串将XML注入节点

Jus*_*Lee 3 php xml string simplexml

我正在重建节点子节点,将它们作为字符串保存到数组中,将它们丢弃在XML中,将新的子节点作为字符串插入到数组中...现在我想循环遍历数组并将它们写回原始节点.问题是我找不到任何关于如何使用字符串添加子节点的内容.

请参阅下面的代码.谢谢!!!

$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;
Run Code Online (Sandbox Code Playgroud)

更新:我发现了一个我修改过的函数并且能够正常工作:

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);
    }
} 
Run Code Online (Sandbox Code Playgroud)

在我的foreach()循环中使用此用法:

foreach ($children as $element) {  //  loop thru array 
    append_simplexml($questionGroup, new SimpleXMLElement($element));
}
Run Code Online (Sandbox Code Playgroud)

Ste*_*rig 8

你不能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>
 */
Run Code Online (Sandbox Code Playgroud)

链接: