douyangquan2474 2014-05-20 13:55
浏览 37
已采纳

来自孙子的XMLSimpleElement

I'm trying to loop threw the following xml string in PHP but having trouble getting the childelements of actor and ebook when using xmlsimpleelement parser.

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:ff="some uri">
   <title>Fulfillments</title>
   <link rel="self" type="application/atom+xml" href="some uri" title="...." />
   <updated>2014-05-20T10:10:05Z</updated>

   <entry>
       <title>e90e7ea4e7ea4-8b6a-4355-a9e7-a448c8435126</title>
       <id>urn:uuid:e90e7ea4-8b6a-4355355-a9e7-a448c8435126</id>
       <updated>2014-05-20T10:10:05Z</updated>
       <summary>sometext</summary>
       <ff:created>2014-05-07T06:43:32Z</ff:created>
       <ff:modified>2014-05-07T06:43:32Z</ff:modified>
       <ff:price>50</ff:price>
       <ff:orderid>randomorderhash</ff:orderid>
       <ff:drmType>4</ff:drmType>
       <ff:actor>
           <ff:id>12345</ff:id>
           <ff:name>Actor Name</ff:name>
       </ff:actor>
       <ff:ebook>
           <ff:id>somerandomidhash</ff:id>
           <ff:ean>12345678</ff:ean>
           <ff:publisher>905555</ff:publisher>
           <ff:version>1</ff:version>
           <ff:drmType>4</ff:drmType>
           <ff:format>1</ff:format>
           <ff:size>750319</ff:size>
       </ff:ebook>
   </entry>

  <entry>
      ....

My phpcode looks like this:

$data = simplexml_load_string($xmlstr);
$result = array();
foreach ( $data->children() as $entries ) {
    $item = array();
    foreach($entries->children() as $key => $entry) {
        $item[$key] = $entry;
    }

   foreach($entries->children('ff', true) as $key => $entry) {
        switch ($key) {
            case 'actor':
                // what do i write here to get the children?
            break;
            case 'ebook':
                // what do i write here to get the children?
            break;
            default:
                $item[$key] = $entry;
            break;
        }
    }
    $result[] = $item;
}

I've tried with both children() and attributes() but still having a hard time getting the child elements of both actor and ebook.

Any help is much appreciated!

  • 写回答

1条回答 默认 最新

  • dsvcqvp139098 2014-05-20 14:36
    关注

    works here: http://ideone.com/GfmqQh

    foreach($entries->children('ff', true) as $key => $entry) {
        switch ($key) {
            case 'actor':
                // what do i write here to get the children?
                var_dump($entry->children('ff', true)->name);
                //if case you like get attributes... $entry->attributes(), return array of attributes
                echo "actor
    ";
            break;
            case 'ebook':
                // what do i write here to get the children?
                echo "ebook
    ";
            break;
            default:
                echo "$key default
    ";
                $item[$key] = $entry;
            break;
        }
    }
    $result[] = $item;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?