0

我正在使用 simplexml 通过 php 以 XML 的形式加载 API URL。在ProductDescription元素中,它包含我想在保修部分附加的 html。我想<li>Valid in US</li>ProductDescriptionLAST</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,因为它可能会很痛苦(除非有人知道一种方法)。我最初的想法是将它放入 a<textarea>并简单地将其发布回 xml,但如果有一种方法可以直接保存为 .xml 会更有效。

4

1 回答 1

1

如果uls 的数量已知,则可以添加如下信息:

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

这假设$NEW_XML指向Products-Element。要始终获得最后一个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();
于 2012-04-27T19:58:17.303 回答