如何使用PHP编辑和删除XML中的特定节点

发布于 2025-01-07 22:26:06 字数 2742 浏览 0 评论 0 原文

我浪费了很多时间尝试通过 php 编辑和删除 xml 文件中的特定节点。现在我成功删除了该节点,但是当我尝试编辑节点时,它只会更新最后一个节点。

我的 xml 文件如下:

<?xml version="1.0"?>
  <reviews>
   <product name="NokiaN9White">
      <user name="testuser" id="02232012062451">
         <username>[email protected]</username>
         <date_post>23-02-2012</date_post>
         <date_update>23-02-2012</date_update>
         <review>Hello dear.</review>
      </user>
      <user name="testuser" id="02232012062521">
         <username>[email protected]</username>
         <date_post>23-02-2012</date_post>
         <date_update>23-02-2012</date_update>
         <review>Mrr</review>
      </user>
      <user name="testuser" id="02232012062523">
         <username>[email protected]</username>
         <date_post>23-02-2012</date_post>
         <date_update>23-02-2012</date_update>
         <review>adfasdf</review>
      </user>
   </product>
</reviews>

我编辑此数据的代码是:

$xmlDoc = new DOMDocument(); 
$xmlDoc->load(dirname(__FILE__)."/reviews.xml");

$products = $xmlDoc->getElementsByTagName( "product" );
    foreach( $products as $product )
    {
        $prdoductN = $product->getAttribute( "name" );
        if( $prdoductN == $product_name ){

            $users = $product->getElementsByTagName( "user" );

            $userR = get_userName($_SESSION['userName']);
            foreach( $users as $user ){
                $userName = $user->getAttribute( "name" );
                $review_id = $user->getAttribute( "id" );

                if( $userName == $userR && $review_id == $r_id ){

                    $current_date = date('d-m-Y');

                    $eventN = $user->getElementsByTagName("date_update");
                    $eventN->item(0)->nodeValue = $current_date;

                    $eventC = $user->getElementsByTagName("review");
                    $eventC->item(0)->nodeValue = $comments;

                }
            }

            $xmlDoc->formatOutput = true;
            //$xmlDoc->saveXML(); // This will return the XML as a string
            $xmlDoc->save(dirname(__FILE__)."/reviews.xml");
        }
    }

I have wasted a lot of my time trying to edit and delete a specific node in an xml file by php. Now I successfully deleted that node, but when I try to edit a node it will update only the last node.

My xml file is given below:

<?xml version="1.0"?>
  <reviews>
   <product name="NokiaN9White">
      <user name="testuser" id="02232012062451">
         <username>[email protected]</username>
         <date_post>23-02-2012</date_post>
         <date_update>23-02-2012</date_update>
         <review>Hello dear.</review>
      </user>
      <user name="testuser" id="02232012062521">
         <username>[email protected]</username>
         <date_post>23-02-2012</date_post>
         <date_update>23-02-2012</date_update>
         <review>Mrr</review>
      </user>
      <user name="testuser" id="02232012062523">
         <username>[email protected]</username>
         <date_post>23-02-2012</date_post>
         <date_update>23-02-2012</date_update>
         <review>adfasdf</review>
      </user>
   </product>
</reviews>

my code for editing this data is:

$xmlDoc = new DOMDocument(); 
$xmlDoc->load(dirname(__FILE__)."/reviews.xml");

$products = $xmlDoc->getElementsByTagName( "product" );
    foreach( $products as $product )
    {
        $prdoductN = $product->getAttribute( "name" );
        if( $prdoductN == $product_name ){

            $users = $product->getElementsByTagName( "user" );

            $userR = get_userName($_SESSION['userName']);
            foreach( $users as $user ){
                $userName = $user->getAttribute( "name" );
                $review_id = $user->getAttribute( "id" );

                if( $userName == $userR && $review_id == $r_id ){

                    $current_date = date('d-m-Y');

                    $eventN = $user->getElementsByTagName("date_update");
                    $eventN->item(0)->nodeValue = $current_date;

                    $eventC = $user->getElementsByTagName("review");
                    $eventC->item(0)->nodeValue = $comments;

                }
            }

            $xmlDoc->formatOutput = true;
            //$xmlDoc->saveXML(); // This will return the XML as a string
            $xmlDoc->save(dirname(__FILE__)."/reviews.xml");
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可爱咩 2025-01-14 22:26:06

您可以使用 Zorba 提供的 PHP 扩展来实现您的示例:

declare namespace a = "http://www.zorba-xquery.com/annotations";

declare variable $doc := <reviews>...</reviews>;

declare %a:sequential function local:remove-user(
    $doc as element(reviews), $name as xs:string, $id as xs:string
) as element(reviews)
{
    delete node $doc//user[@name = $name and @id = $id];
    $doc
};

local:remove-user($doc, "testuser", "02232012062523")

您可以在 http://www.zorba-xquery.com/html/demo#2awjZFgWFxoIi7uE2J4HgwUQUuA=

有关如何使用 PHP 安装 XQuery 扩展的说明,请访问 http://www.zorba-xquery.com/html/entry/2011/12/27/PHP_Meets_XQuery

You can use the PHP extension provided by Zorba to implement your example:

declare namespace a = "http://www.zorba-xquery.com/annotations";

declare variable $doc := <reviews>...</reviews>;

declare %a:sequential function local:remove-user(
    $doc as element(reviews), $name as xs:string, $id as xs:string
) as element(reviews)
{
    delete node $doc//user[@name = $name and @id = $id];
    $doc
};

local:remove-user($doc, "testuser", "02232012062523")

You can try your example live at http://www.zorba-xquery.com/html/demo#2awjZFgWFxoIi7uE2J4HgwUQUuA=

Instruction on how to install the XQuery extension with PHP are available at http://www.zorba-xquery.com/html/entry/2011/12/27/PHP_Meets_XQuery

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文