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

时间:2017-12-11 08:27:04

标签: php xml 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文件中使用它没有任何问题......

有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

您的代码可以正常运行,但只会更新本地版本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