【问题标题】:Cannot add node to xml via php and simplexml无法通过 php 和 simplexml 将节点添加到 xml
【发布时间】:2017-12-11 08:27:03
【问题描述】:

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

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

有什么想法吗?

谢谢!

【问题讨论】:

  • 它不会更新远程站点版本,它按预期工作 - 3v4l.org/5JnEv
  • 嗨劳伦斯,为什么这适用于其他 xml 文件而不适用于这个?
  • 因为您从 http://test.com/xml/customer.xml 获取并保存到 test.xml :/ 该过程只是重复。
  • @LawrenceCherone 我明白你的意思......听起来很愚蠢,但你能通过从远程源加载 xml 将代码发给我吗?
  • 添加了答案,如果您需要澄清,请告诉我。

标签: php xml simplexml


【解决方案1】:

您的代码可以正常工作,但它不会仅更新远程源您的本地版本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

【讨论】:

  • 劳伦斯您好,感谢您的宝贵时间!当然,我有兴趣保存远程 xml 的本地编辑版本。它仍然不会添加新项目。这是实际的远程文件 url 供您测试,看看它是否适合您:rihardos.gr/…
  • 由于&lt;store&gt; 包装器而无法正常工作,请改用$products-&gt;xpath("*/product")
  • 劳伦斯,你简直就是救命恩人!!!这就是为什么它适用于其他 xml 文件而不适用于这个的原因!不能感谢你!保重!
猜你喜欢
  • 2013-03-27
  • 1970-01-01
  • 2021-03-25
  • 1970-01-01
  • 2016-12-12
  • 2014-12-11
  • 2013-02-18
  • 2018-06-01
  • 1970-01-01
相关资源
最近更新 更多