我一直在用PHP做一些练习,我有一些关于如何使用PHP将变量作为属性的每个项目添加到XML的新问题,我将详细解释它。
首先,我加载一个XML文件,该结构看起来与下一个类似:
<?xml version="1.0" encoding="utf-8"?>
<products>
<item>
<reference>00001</reference>
<other_string>PRODUCT 1</other_string>
<brand>BRAND 1</brand>
<promo>YES</promo>
</item>
<item>
<reference>00002</reference>
<other_string>PRODUCT 2</other_string>
<brand>BRAND 2</brand>
<promo>YES</promo>
</item>
<item>
<reference>00003</reference>
<other_string>PRODUCT 3</other_string>
<brand>BRAND 3</brand>
<promo>NO</promo>
</item>
<item>
<reference>00004</reference>
<other_string>PRODUCT 4</other_string>
<brand>BRAND 4</brand>
<promo>NO</promo>
</item>
<item>
<reference>00005</reference>
<other_string>PRODUCT 5</other_string>
<brand>BRAND 5</brand>
<promo>YES</promo>
</item>
</products>
正如您所看到的,XML中有一个名为<promo></promo>
的节点,其中包含两个可能的变量“YES”和“NO”,因此如果存在“YES”或“0”,我需要创建一个包含“1”的新节点如果“NO”看起来与此类似:
<?xml version="1.0" encoding="utf-8"?>
<products>
<item>
<reference>00001</reference>
<other_string>PRODUCT 1</other_string>
<brand>BRAND 1</brand>
<promo>YES</promo>
<newnode>1</newnode>
</item>
<item>
<reference>00002</reference>
<other_string>PRODUCT 2</other_string>
<brand>BRAND 2</brand>
<promo>YES</promo>
<newnode>1</newnode>
</item>
<item>
<reference>00003</reference>
<other_string>PRODUCT 3</other_string>
<brand>BRAND 3</brand>
<promo>NO</promo>
<newnode>0</newnode>
</item>
<item>
<reference>00004</reference>
<other_string>PRODUCT 4</other_string>
<brand>BRAND 4</brand>
<promo>NO</promo>
<newnode>0</newnode>
</item>
<item>
<reference>00005</reference>
<other_string>PRODUCT 5</other_string>
<brand>BRAND 5</brand>
<promo>YES</promo>
<newnode>1</newnode>
</item>
</products>
我一直在玩这个代码,但它不能递归地工作,似乎不使用xPath检测值:
<?php
$sXML = simplexml_load_file('file.xml', null, LIBXML_NOBLANKS);
$promoyes = $sXML->xpath("//item[promo='YES']");
foreach ( $promoyes as $value ) {
$value = $sXML->item->addChild('newnode', '1');
}
unset($value);
$promono = $sXML->xpath("//item[promo='NO']");
foreach ( $promono as $value ) {
$value = $sXML->item->addChild('newnode', '0');
}
unset($value);
$domDocument = dom_import_simplexml($sXML)->ownerDocument;
$domDocument->formatOutput = true;
echo $domDocument->save('new.xml');
?>
任何帮助将不胜感激。
正如您所知,节点都被添加到第一个<item>
节点 - 这一直到行......
$value = $sXML->item->addChild('newnode', '1');
当您从根节点开始并且只使用->item->
时,这将始终假设您将新节点添加到第一个<item>
元素。
您只需要将节点添加到$value
节点。我还简化了它使用1循环,它选择任何<item>
元素与<promo>
元素(使用//item[promo]
)。然后,这会在<promo>
值上添加测试新节点...
$sXML = simplexml_load_file('file.xml', null, LIBXML_NOBLANKS);
$promo = $sXML->xpath("//item[promo]");
foreach ( $promo as $value ) {
$value->newnode = ($value->promo=='YES')? 1:0;
}
$domDocument = dom_import_simplexml($sXML)->ownerDocument;
$domDocument->formatOutput = true;
$domDocument->save('new.xml');
这最容易使用DOMDocument
自己完成。您可以使用getElementsByTagName
查找所有promo
元素,然后使用基于newnode
元素值的值添加promo
元素:
$doc = new DOMDocument();
$doc->loadXML($xml);
foreach ($doc->getElementsByTagName('promo') as $ele) {
$newnode = $doc->createElement('newnode');
$newnode->nodeValue = $ele->nodeValue == 'YES' ? 1 : 0;
$ele->parentNode->appendChild($newnode);
}
echo $doc->saveXML();