从另一个孩子更新子节点xml php

时间:2016-05-27 22:07:07

标签: php xml

请帮忙。 我有没有正确填写的XML文件(错过了一些值)

XML的错误示例

<?xml version="1.0"?>
<csv_data>
    <row>
        <brand>Maxxis</brand>
        <code>1126668/02</code>
        <articul_brand></articul_brand>
        <tiporazmer></tiporazmer>
        <name>(26/9.00 R12) 6PR TL</name>
        <model>M-966 Mudzilla</model>
        <sklad_msk></sklad_msk>
        <sklad_nahabino></sklad_nahabino>
        <sklad_chelny>8</sklad_chelny>
        <price_big_opt>6,786р.</price_big_opt>
        <price_small_opt>7,100р.</price_small_opt>
        <price_retail>8,170р.</price_retail>
    </row>
</csv_data>

XML的好例子

 <?xml version="1.0"?>
    <csv_data>
    <row>
        <brand>Metzeler</brand>
        <code>ЦО04257/21</code>
        <articul_brand>2491500</articul_brand>
        <tiporazmer>110/80R18</tiporazmer>
        <name>(110/80 R18) 58W TL (M) Front</name>
        <model>Roadtec Z8</model>
        <sklad_msk></sklad_msk>
        <sklad_nahabino>2</sklad_nahabino>
        <sklad_chelny></sklad_chelny>
        <price_big_opt>7,720р.</price_big_opt>
        <price_small_opt>8,106р.</price_small_opt>
        <price_retail>9,270р.</price_retail>
    </row>
</csv_data>

重点是错误示例中错过了<tiporazmer></tiporazmer>中的值,但可以在<name>(26/9.00 R12) 6PR TL</name>

中找到它们

问题是如何使用名称字段更新字段<tiporazmer></tiporazmer>?复制没有括号?怎么样?

我做了什么,但我收到了错误Fatal error: Call to a member function getElementsByTagName() on a non-object in

代码:

<?php
    $xml = new DOMDocument('1.0', 'utf-8');
    $xml->formatOutput = true; 
    $xml->preserveWhiteSpace = false;
    $xml->load('test2.xml');
    $new_tiporazmer="26/9.00 R12";

    //Get item Element
    $element = $xml->getElementsByTagName('rows')->item(0);  
    //Load child elements
    $tiporazmer = $element->getElementsByTagName('tiporazmer')->item(0);

    //Replace old elements with new
    $element->replaceChild($tiporazmer, $new_tiporazmer);
    ?>

1 个答案:

答案 0 :(得分:0)

错误是因为您正在调用getElementsByTagName('rows'),但XML节点的名称是row,因此s中不应该有rows

然后,为了使用new替换旧元素,除非已经准备好构造的DOMDocument对象,否则需要使用nodeValue而不是replaceChild

但实现目标的更好方法是直接按名称查找元素,替换其文本,然后另存为新文件。我保存到“/ tmp”文件夹,因为权限通常可以在unix系统上写入,但是你可以在任何地方写你的php解释器。

<?php
    $xml = new DOMDocument('1.0', 'utf-8');
    $xml->formatOutput = true; 
    $xml->preserveWhiteSpace = false;
    $xml->load('test2.xml');
    $new_tiporazmer="26/9.00 R12";

    //Get item Element and Replace old elements with new
    $xml->getElementsByTagName('tiporazmer')->item(0)->nodeValue = $new_tiporazmer;

    //Save as new file
    $xml->save("/tmp/good.xml");

这导致了good.xml:

<?xml version="1.0"?>
<csv_data>
  <row>
    <brand>Maxxis</brand>
    <code>1126668/02</code>
    <articul_brand/>
    <tiporazmer>26/9.00 R12</tiporazmer>
    <name>(26/9.00 R12) 6PR TL</name>
    <model>M-966 Mudzilla</model>
    <sklad_msk/>
    <sklad_nahabino/>
    <sklad_chelny>8</sklad_chelny>
    <price_big_opt>6,786&#x440;.</price_big_opt>
    <price_small_opt>7,100&#x440;.</price_small_opt>
    <price_retail>8,170&#x440;.</price_retail>
  </row>
</csv_data>
相关问题