Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用php在xml中添加/追加字符串数据_Php_Xml - Fatal编程技术网

如何使用php在xml中添加/追加字符串数据

如何使用php在xml中添加/追加字符串数据,php,xml,Php,Xml,下面是我的示例xml数据 <?xml version="1.0"?> <catalog> <book> <book NAME="ci_id"> <PVAL><![CDATA[55190]]></PVAL> </book> <author>Gambardella, Matthew</author> <t

下面是我的示例xml数据

 <?xml version="1.0"?>
<catalog>
   <book>
      <book NAME="ci_id">
        <PVAL><![CDATA[55190]]></PVAL>
      </book>
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
   </book>
   <book>
      <book NAME="ci_id">
        <PVAL><![CDATA[55191]]></PVAL>
      </book>
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
   </book>
   <book>
      <book NAME="ci_id">
        <PVAL><![CDATA[55192]]></PVAL>
      </book>
      <author>Corets, Eva</author>
      <title>Maeve Ascendant</title>
   </book>

   <book>
      <book NAME="ci_id">
        <PVAL><![CDATA[55194]]></PVAL>
      </book>
      <author>Corets, Eva</author>
      <title>The Sundered Grail</title>
   </book> 
</catalog>

马修·甘巴德拉
XML开发人员指南
拉尔斯,金
夜雨
科雷茨,伊娃
梅夫上升
科雷茨,伊娃
破碎的圣杯
我的要求是,我必须根据pval id条件(例如:这里我要删除pval=55192)删除父节点(即,),并且我需要更新新节点

在下面的代码中,我已基于pval成功删除了父节点,但无法添加新节点(即,它是一个字符串值)。请引导我

$xmlFileToLoad   =  'client_temp/endeca_xml_search_feed/product/testing.xml';
      $xmlFileToSave   =  'client_temp/endeca_xml_search_feed/product/testing-modified.xml';

      $cdata_text = '
      <book>
         <book NAME="ci_id">
            <PVAL><![CDATA[78965]]></PVAL>
         </book>
         <author>testing</author>
         <title>Md kaif khan</title>
         <genre>kkkk</genre>
         <price>5.95</price>
         <publish_date>2016-09-10</publish_date>
         <description>Just i am testing.</description>
      </book>';

      $dom = new DOMDocument();
      $dom->load($xmlFileToLoad);
      $xpath = new DOMXPath($dom);

    function findStopPointByName($xml, $query) {
      $upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZAZSCZCÓL";
      $lower = "abcdefghijklmnopqrstuvwxyzazscznól";
      $arg_query    = "translate(text(), '$upper', '$lower')";
      $q = "//book[book/PVAL[contains($arg_query, '$query')]]" ."\n";
      return $xml->query($q);
    }

    foreach(findStopPointByName($xpath,'55192') as $node)
    {
      $node->parentNode->removeChild($node);
       $node->appendChild( 
     $doc->createTextNode( $cdata_text ) );  
    }

    $dom->save($xmlFileToSave);
$xmlFileToLoad='client_temp/endeca_xml_search_feed/product/testing.xml';
$xmlFileToSave='client_temp/endeca_xml_search_feed/product/testing modified.xml';
$cdata\u text='1
测试
Md kaif khan
KKK
5.95
2016-09-10
只是我在测试。
';
$dom=新的DOMDocument();
$dom->load($xmlFileToLoad);
$xpath=newdomxpath($dom);
函数findStopPointByName($xml,$query){
$upper=“abcdefghijklmnopqrstuvxyzczcÓL”;
$lower=“abcdefghijklmnopqrstuvxyzcznnól”;
$arg_query=“translate(text(),'$upper','$lower')”;
$q=“//book[book/PVAL[包含($arg_query,$query')]]”“\n”;
返回$xml->query($q);
}
foreach(findStopPointByName($xpath,'55192')作为$node)
{
$node->parentNode->removeChild($node);
$node->appendChild(
$doc->createTextNode($cdata\U text));
}
$dom->save($xmlFileToSave);

请告诉我如何添加节点(即,字符串数据。此处为$cdata\U text)。

很高兴提供帮助。祝你好运嗨@splash,我需要你的帮助。我正在尝试将新节点添加到根节点(即,…)。使用以下代码<代码>$newNode=newDOMDocument()$newNode->loadXML($trimData)$newNode->formatOutput=true;//将所有新XML导入到我们的XML中,并附加到保存的父项$ndata=$dom->importNode($newNode->documentElement,true)$dom->documentElement->appendChild($nda)$dom->appendChild($nda);您好@Slash,代码运行良好,但性能非常差。示例:完成10个节点的测试需要55秒,而服务器的负载太高,几乎需要99%。现在我不使用findStopPointByName函数。相反,我使用$dom->getElementById(这里是记录id,即…);你能给我一个建议吗,那就是如何提高绩效。
   foreach(findStopPointByName($xpath,'55192') as $node)
    {
      // Save parent - after removing node we will use it
      $parent = $node->parentNode;
      $parent->removeChild($node);

      // Load XML from text. You cann't append text, only node
      $data = new DOMDocument();
      $data->loadXML($cdata_text);

      // Import all new XML to our one and append to saved parent
      $ndata =  $dom->importNode($data->documentElement, true);   
      $parent->appendChild($ndata); 
    }