dongmingxiang0312 2012-11-12 14:45
浏览 55
已采纳

PHP simplexml子项为小写

I've got this as XML:

...
<product>
<id>1</id>
<defaultImage>test.jpg</defaultImage>
</product>
...

I've got this as php:

$testcase = 'defaultimage';
$xml = simplexml_load_file('./temp/'.$i.'.xml');
foreach ($xml->children() as $child) {
    $child->$testcase;
}

Now the problem is this, I'm forced to have $testcase in a lowercase form (defaultimage) BUT in the XML file the name of the child is: defaultImage (note the uppercase I)

Question: How can I handle all the children as lowercases?

  • 写回答

1条回答 默认 最新

  • dqef7931 2012-11-12 15:11
    关注

    You can use the ->getName() method to find out the name of a node while looping through an inner ->children() array.

    For clarity, I've used a more explicit loop over all product nodes for the outer loop

    $testcase = 'defaultimage';
    $xml = simplexml_load_file('./temp/'.$i.'.xml');
    foreach ( $xml->product as $product )
    {
       foreach ( $product->children as $child )
       {
          if ( strtolower($child->getName()) == $testcase )
          {
             // Do whatever it is you need to do with the <defaultImage> node
          }
       }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部