在php中使用SimpleXml将其他数据添加到元素中并保存

时间:2012-04-27 19:38:07

标签: php xml regex simplexml

我使用simplexml通过php以XML的形式加载API URL。在ProductDescription元素中,它包含我想要在保修部分附加的html。 我想在最后<li>Valid in US</li>代码之前的ProductDescription元素中添加</ul>

    <xmldata>
      <Products>
        <ProductCode>ACODE</ProductCode>
        <ProductID>1234</ProductID>
        <ProductName>PRODUCTTITLE</ProductName>
        <ProductDescription>
           <h1>Stuff</h1><p>description</p><hr /><strong>Features &amp; Benefits</strong>
           <ul><li>feat</li><li>feat</li><li>feat</li></ul><hr /><strong>Specifications</strong><ul><li>spec</li><li>spec</li><li>spec</li></ul> <hr /><strong>Warranty Information for a certain product</strong>
           <ul><li>3 Years Parts</li><li>3 Years Labor</li></ul><div> <a href="/ahref" target="_blank">See more products from MFG </a></div>
        </ProductDescription>
        <ProductManufacturer>MFG</ProductManufacturer>
      </Products>
    </xmldata>

现在我所能做的就是从ProductDescription中获取裸代码,我需要知道在发布或显示之前将list标记添加到最后ul标记的末尾的方法。这是我的部分php:

     foreach( $xml as $NEW_XML ) 
        {          
            $code = $NEW_XML->ProductCode;
            $name = $NEW_XML->ProductName;
            $desc = $NEW_XML->ProductDescription; 
            $mfg = $NEW_XML->ProductManufacturer;
echo "<textarea rows='13' cols='64' name='DESC' />" . $desc. "</textarea>"; }

我希望我不必使用REGex,因为它可能是一种痛苦(除非有人知道某种方式)。我的初衷是将它放入<textarea>并简单地将其发布回xml,但如果有办法直接保存为.xml会更有效率。

1 个答案:

答案 0 :(得分:1)

如果知道ul的数量,您可以添加如下信息:

$NEW_XML->ProductDescription->ul[2]->addChild('li', 'Valid in US');

这假定$NEW_XML指向Products - 元素。要始终获取最后ul,您必须先计算它们:

$desc = $NEW_XML->ProductDescription;
$count = count($desc->ul);
$desc->ul[$count - 1]->addChild('li', 'Valid in US');

然后您可以输出或保存$xml->asXML()

<强>更新

这是我使用的完整代码,根据我上次评论中的假设更新,也许这会有所帮助:

<?php
$xmldata = <<<ENDXML
<xmldata>
      <Products>
        <Product>
            <ProductCode>ACODE</ProductCode>
            <ProductID>1234</ProductID>
            <ProductName>PRODUCTTITLE</ProductName>
            <ProductDescription>
               <h1>Stuff</h1><p>description</p><hr /><strong>Features &amp; Benefits</strong>
               <ul><li>feat</li><li>feat</li><li>feat</li></ul><hr />    <strong>Specifications</strong><ul><li>spec</li><li>spec</li><li>spec</li></ul> <hr /><strong>Warranty Information for a certain product</strong>
               <ul><li>3 Years Parts</li><li>3 Years Labor</li></ul><div> <a href="/ahref" target="_blank">See more products from MFG </a></div>
            </ProductDescription>
            <ProductManufacturer>MFG</ProductManufacturer>
        </Product>
        <Product>
            <ProductCode>ANOTHERCODE</ProductCode>
            <ProductID>98765</ProductID>
            <ProductName>PRODUCTTITLE 2</ProductName>
            <ProductDescription>
               <h1>Stuff</h1><p>description</p><hr /><strong>Features &amp; Benefits</strong>
               <ul><li>feat</li><li>feat</li><li>feat</li></ul><hr /> <strong>Specifications</strong><ul><li>spec</li><li>spec</li><li>spec</li></ul> <hr /> <strong>Warranty Information for a certain product</strong>
               <ul><li>3 Years Parts</li><li>3 Years Labor</li></ul><div> <a href="/ahref" target="_blank">See more products from MFG </a></div>
            </ProductDescription>
            <ProductManufacturer>MFG</ProductManufacturer>
        </Product>
    </Products>
</xmldata>
ENDXML;

$xml = simplexml_load_string($xmldata);

foreach ($xml->Products->Product as $product) {
    $description = $product->ProductDescription;
    $count = count($description->ul);
    $description->ul[$count-1]->addChild('li', 'Valid in US');
}

echo $xml->asXML();