php - 使用 DOM 插入新行

标签 php xml dom simplexml

我正在尝试将新节点插入到 XML 文档中。我使用 simpleXML 进行大部分解析,但对于这篇文章,我需要使用 DOM。这是我用来添加的函数

function simplexml_insert_after(SimpleXMLElement $sxe, SimpleXMLElement $insert, SimpleXMLElement $target)   
 {
    $target_dom = dom_import_simplexml($target);
    $target_dom->formatOutput = true;
    $target_dom->preserveWhiteSpace = false;
    $insert_dom = $target_dom->ownerDocument->importNode(dom_import_simplexml($insert), true);
    if ($target_dom->nextSibling) {
        return $target_dom->parentNode->insertBefore($insert_dom, $target_dom->nextSibling);
    } else {
        return $target_dom->parentNode->appendChild($insert_dom);
    }
}

然后这样调用它:

$meta2 = new SimpleXMLElement("<meta/>");
$meta2->addAttribute('name', 'subject');
$meta2->addAttribute('value', $doc_id); 
$target = current($my_xml->xpath('//meta[last()]'));
simplexml_insert_after($my_xml, $meta2, $target);

我的问题是它在目标之后而不是在新行上立即插入新节点,因此生成的 XML 如下所示:

<meta content="a" name="ap-category"/>
<meta content="bx" name="ap-format"/><meta name="subject" value="urn:blah"/>

当我希望它看起来像这样时:

<meta content="a" name="ap-category"/>
<meta content="bx" name="ap-format"/>
<meta name="subject" value="urn:blah"/>

我尝试将 preserveWhiteSpace 更改为 true,但没有帮助。如何在插入该节点之前添加新行?

编辑 这是有效的修复方法:

 function simplexml_insert_after(SimpleXMLElement $sxe, SimpleXMLElement $insert,SimpleXMLElement $target)   
 {
     $target_dom = dom_import_simplexml($target);
     $target_dom->formatOutput = true;
     $target_dom->preserveWhiteSpace = false;
     $insert_dom = $target_dom->ownerDocument->importNode(dom_import_simplexml($insert), true);
     if ($target_dom->nextSibling) {
          $result = $target_dom->parentNode->insertBefore($insert_dom, $target_dom->nextSibling);
          $target_dom->parentNode->insertBefore($target_dom->ownerDocument->createTextNode("\n"), $result);
          return $result
     } else {
          return $target_dom->parentNode->appendChild($insert_dom);
     }
}

最佳答案

尝试在要插入的元素之前插入一个文本节点,如下所示:

function simplexml_insert_after(SimpleXMLElement $sxe, 
                                SimpleXMLElement $insert, 
                                SimpleXMLElement $target)   
{
    $target_dom = dom_import_simplexml($target);
    $target_dom->formatOutput = true;
    $target_dom->preserveWhiteSpace = false;

    $document = $target_dom->ownerDocument;
    $insert_dom = $document->importNode(dom_import_simplexml($insert), true);

    $parentNode = $target_dom->parentNode;
    if ($target_dom->nextSibling) {
        $result = $parentNode->insertBefore($insert_dom, $target_dom->nextSibling);
        $parentNode->insertBefore($document->createTextNode("\n"), $result);
        return $result
    } else {
        return $parentNode->appendChild($insert_dom);
    }
}

关于php - 使用 DOM 插入新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7440425/

相关文章:

php - 如何在 yii 中从数据库中获取 CSS 的变量值?

php - Curl - 获取 JSESSIONID

c# - 遍历 XML 架构失败。 (使用 System.Xml.Schema 和 C#)

javascript - 让加载屏幕与 Polymer 配合使用

javascript - 如何通过引用将属性传递给 javascript 函数?

php - 在 Quickbook 中添加客户日记条目时出现问题 - 消息 : Passed array has no key for 'Value' when contructing an ReferenceType

php - $mysqli->准备 SQL 事务

c# - 如何解决: "You need to add XmlChoiceIdentifierAttribute to the member." when using XmlAttributeOverrides?

php - 如何将XML文件复制到MySQL字段中进行分析?

javascript - 用 JS/jQuery 中的文本替换 DOM 树中的非图像元素