无法通过 php 和简单 xml 将节点添加到 xml

Cannot add node to xml via php and simplexml

我对此感到困惑,有趣的是我已经多次成功使用此代码...

我的目标是创建一个新节点 (newsku),它将一个字符串与 xml 中的现有节点相结合。

这是xml:

<products>
    <product>
        <id>3</id>
        <name><![CDATA[ΜΙΚΡΟΦΩΝΟ SAMSON G-TRACK]]></name>
        <manufacturer><![CDATA[SAMSON]]></manufacturer>
        <sku><![CDATA[550.SAM.060]]></sku>
        <description_greek><![CDATA[Samson G-Track - large diaphragm USB studio 
condenser microphone (USB bus-powered), built-in audio interface and mixer, 
allows simultaneous input of vocals and guitar, bass, or keyboard while also 
providing monitoring through an on-board headphone output. Specifications: mic 
and instrument/line gain control with clip LED, stereo input jacks for (3.5mm 
stereo-jack) instrument or line level signal, stereo headphone jack for zero 
latency monitoring with level control, 3-position headphone switch for stereo, 
mono and computer monitoring. USB bus-powered. Includes desktop microphone 
stand, audio I/O cables, USB cables and Cakewalk Sonar LE software. Optional 
shockmount available.
]]></description_greek>
        <short_description_greek><![CDATA[Samson G-Track - large diaphragm USB studio 
condenser microphone (USB bus-powered)]]></short_description_greek>
        <price>155.00</price>
        <msrp>185.00</msrp>
        <instock>no</instock>
        <images total="2">
            <image_1>http://test.com/media/catalog/product/5/5/550.sam.060-mi-
01.jpg</image_1>
            <image_2>http://test.com/media/catalog/product/5/5/550.sam.060-mi-
02.jpg</image_2>
        </images>
    </product>
</products>

这是我的代码:

<?php
    header('Content-Type: text/html; charset=UTF-8');
    error_reporting(E_ALL);

    // Import test xml

    $products = simplexml_load_file("http://test.com/xml/customer.xml");

    foreach($products->xpath("product") as $p) {
        $p->addChild("newsku", "NEW" . $p->sku);
    }

    $products->asXML('test.xml');
    echo 'test XML files are updated';
?>

我得到的是原始 xml 而没有新节点...

可能我正在做一些非常愚蠢的事情,因为我已经在许多其他 xml 文件中毫无问题地使用了它...

有什么想法吗?

谢谢!

您的代码可以正常工作,但它不会仅更新​​本地版本的远程源test.xml。每次调用 php 代码时,它都会被相同的值覆盖。

所以下面的代码将从您的远程源获取,然后添加新元素 newsku然后输出到浏览器

$products = simplexml_load_file('http://test.com/xml/customer.xml');

foreach( $products->xpath("*/product") as $p ) {
    $p->addChild("newsku",  "NEW".$p->sku);
}

header('Content-Type: text/xml; charset=UTF-8');
// just outputs the xml - does not save
exit($products->asXML());

如果需要缓存xml,要写入多次则先保存文件。这将在每次刷新时添加一个新项目(很确定它不是你想要的)。

// the xml file name
$xml_file = 'test.xml';

// check if the file exists or download and save it
if (!file_exists($xml_file)) {
    file_put_contents($xml_file, file_get_contents('http://test.com/xml/customer.xml'));
}

// load xml file for changes
$products = simplexml_load_file($xml_file);

foreach( $products->xpath("*/product") as $p ) {
    $p->addChild("newsku",  "NEW".$p->sku);
}

header('Content-Type: text/xml; charset=UTF-8');

// save the xml to file
$products->asXML($xml_file);

// read and display the xml file
readfile($xml_file);

如果 /xml/customer.xml 实际上在您的服务器上是本地的,我对 远程 感到困惑。您可以在没有 fgc/fpc 的情况下使用上面的代码并喜欢(再次将在每次调用文件时更新)。

// load xml file for changes
$products = simplexml_load_file('/path/to/xml/customer.xml');

foreach( $products->xpath("*/product") as $p ) {
    $p->addChild("newsku",  "NEW".$p->sku);
}

header('Content-Type: text/xml; charset=UTF-8');

// save the xml to file 
$products->asXML($xml_file);
readfile($xml_file);

// or just outputs the xml - no saving
// exit($products->asXML());

希望对您有所帮助。

在线查看 - https://3v4l.org/U8EbE