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

问题描述 投票:1回答:1

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

我的目标是创建一个新节点(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文件中使用它没有任何问题...

有任何想法吗?

谢谢!

php xml simplexml
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.