我非常接近这项工作。我有一个不完善的xml,但它是Android Launchers的标准,所以我必须弄清楚如何使用它。
我将它加载到我的MySQL表中,但是它为所有类别加载所有项目。让我告诉你我的意思。
<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>
这是当前解析它的PHP代码
$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\n", mysql_affected_rows());
}
}
所以我最终得到了所有类别标题重复的所有项目可绘制的entires。
我需要项目drawable以匹配它上面的类别。每当类别更改文件时,项目可绘制的行应更改类别
答案 0 :(得分:1)
问题出在第二个foreach你正在阅读所有项目然后上面的foreach将再次运行并将给出相同的项目,使他们两次。
使用此方法: 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>";
}
}
}
希望它有所帮助。