dousheyan0375 2014-08-23 03:45
浏览 59
已采纳

使用simplexml_load_file解析xml并插入MySQL

I am so close to having this work. I have an xml that is not well formed but its the standard among Android Launchers so I have to figure out how to work with it.

I have it loading in my MySQL table but its loading ALL items for ALL categories. Let me show you what i mean.

<resources>
<version>1</version>
<category title="System" />
<item drawable="bluetooth" />
<item drawable="bluetooth_audio" />
<item drawable="browser" />
<item drawable="calculator" />
<item drawable="calendar" />
<item drawable="call_history" />
<item drawable="camera" />
<item drawable="clock" />
<item drawable="cmas" />
<item drawable="contacts" />
<item drawable="downloads" />
<item drawable="email" />
<item drawable="gallery" />
<item drawable="help" />
<item drawable="mailbox" />
<item drawable="messaging" />
<item drawable="phone" />
<item drawable="settings" />
<item drawable="supersu" />
<item drawable="superuser" />
<item drawable="superuser_koush" />
<item drawable="voice_commands" />
<item drawable="voice_search" />
<category title="Misc" />
<item drawable="apps" />
<item drawable="apps2" />
<item drawable="bigdx_clean" />
<item drawable="iconback" />
<item drawable="iconfront" />
<item drawable="text_apps" />
<item drawable="text_market" />
<item drawable="text_phone" />
<item drawable="text_sms" />
<item drawable="text_web" />
</resources>

Here is the PHP code that is currently parsing it

$apps =  simplexml_load_file($ptmpName);

    //loop through parsed xmlfeed and print output
    foreach ($apps->category as $category) {
        printf($category['title']);
        $current_category = $category['title'];
        foreach ($apps->item as $item) {
            printf($item['drawable']);
            $current_drawable = $item['drawable'];
            //insert into databse
            mysql_query("INSERT INTO user_drawable (user_ip, category,name)VALUES ('$ip',\"$current_category\",\"$current_drawable\")")
            or die(mysql_error());
            //show updated records
            printf ("Records inserted: %d
", mysql_affected_rows());
        }
    }

So im ending up with all the item drawable entires duplicated for all category titles.

I need item drawable to match to category above it. everytime the category changes down the file the item drawable's row should change category

  • 写回答

1条回答 默认 最新

  • 普通网友 2014-08-23 05:05
    关注

    The problem is inside second foreach you are reading all items then again the above foreach will run and will give same item which makes them twice. Use this:EDITED

     $file="filename.xml";
        $apps =  simplexml_load_file($file);
    
    foreach($apps->children() as $child) {
        $xobj=$apps->getName();
    //  echo  $xobj. "  =>".$child."<br>";
        foreach($child->attributes() as $attrc => $attrValc) {
            $childc=$child->getName();
            if($childc == 'category'){
        echo  $childc ."  ".$attrc." =>".$attrValc."<br>";
            }else{
                echo  $attrc." =>".$attrValc."<br>";
            }
        }
    }
    

    hope it helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿