使用字符串将XML注入节点

时间:2009-11-11 18:49:41

标签: 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;

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

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

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

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

3 个答案:

答案 0 :(得分: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>
 */

链接:

答案 1 :(得分:4)

我发现了一个我修改过的功能,并且能够正常工作:

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

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

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

答案 2 :(得分:-1)

你应该可以这样做:

$child = new SimpleXMLElement($newNode);

$yourxmlobject->addChild($child);