我使用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 & 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会更有效率。
答案 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 & 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 & 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();