在mysql字段中操作文件列表数组中的数据

时间:2015-03-30 17:44:05

标签: php mysql

我有一个mysql数据库,其中包含有关有声书籍和书籍文件位置的详细信息。文件位置可以是多文件上载。这是有效的,并且完全符合预期。

我遇到的问题是,现在用户希望将数据库的内容导出到XML以便在另一个应用程序中使用。这就是问题所在。

数据库示例:

FieldName: BookTitle<br />
Content: War of the Worlds<br />
FieldName: Author<br />
Content: H G Wells<br />
FieldName: FileLocation<br />
Content:[{"name":"files\/warofworlds_b2_ch04_wells_64kb_wqd92auj.mp3","usrName":"warofworlds_b2_ch04_wells_64kb.mp3","size":5270144,"type":"audio\/mp3","searchStr":"warofworlds_b2_ch04_wells_64kb.mp3,!warofworlds_b2_ch06_wells_64kb.mp3,!warofworlds_b2_ch05_wells_64kb.mp3,!warofworlds_b2_ch10_wells_64kb.mp3,!warofworlds_b2_ch09_wells_64kb.mp3,!warofworlds_b2_ch08_wells_64kb.mp3,!warofworlds_b1_ch01_wells_64kb.mp3,!warofworlds_b1_ch02_wells_64kb.mp3,!warofworlds_b1_ch03_wells_64kb.mp3,!warofworlds_b1_ch04_wells_64kb.mp3,!warofworlds_b1_ch06_wells_64kb.mp3,!warofworlds_b1_ch05_wells_64kb.mp3,!warofworlds_b1_ch08_wells_64kb.mp3,!warofworlds_b1_ch10_wells_64kb.mp3,!warofworlds_b1_ch07_wells_64kb.mp3,!warofworlds_b1_ch09_wells_64kb.mp3,!warofworlds_b2_ch07_wells_64kb.mp3,!warofworlds_b1_ch11_wells_64kb.mp3,!warofworlds_b1_ch13_wells_64kb.mp3,!warofworlds_b1_ch14_wells_64kb.mp3,!warofworlds_b1_ch12_wells_64kb.mp3,!warofworlds_b1_ch16_wells_64kb.mp3,!warofworlds_b1_ch15_wells_64kb.mp3,!warofworlds_b1_ch17_wells_64kb.mp3,!warofworlds_b2_ch01_wells_64kb.mp3,!warofworlds_b2_ch03_wells_64kb.mp3,!warofworlds_b2_ch02_wells_64kb.mp3,!:sStrEnd"},{"name":"files\/warofworlds_b2_ch06_wells_64kb_m7j4eow4.mp3","usrName":"warofworlds_b2_ch06_wells_64kb.mp3","size":4134464,"type":"audio\/mp3"},{"name":"files\/warofworlds_b2_ch05_wells_64kb_dqkhae74.mp3","usrName":"warofworlds_b2_ch05_wells_64kb.mp3","size":3274304,"type":"audio\/mp3"},{"name":"files\/warofworlds_b2_ch10_wells_64kb_39ois1bc.mp3","usrName":"warofworlds_b2_ch10_wells_64kb.mp3","size":4315904,"type":"audio\/mp3"},{"name":"files\/warofworlds_b2_ch09_wells_64kb_fjgjexg9.mp3","usrName":"warofworlds_b2_ch09_wells_64kb.mp3","size":5699840,"type":"audio\/mp3"},{"name":"files\/warofworlds_b2_ch08_wells_64kb_v5hckthv.mp3","usrName":"warofworlds_b2_ch08_wells_64kb.mp3","size":9816704,"type":"audio\/mp3"},{"name":"files\/warofworlds_b1_ch01_wells_64kb_h0nml30w.mp3","usrName":"warofworlds_b1_ch01_wells_64kb.mp3","size":7520384,"type":"audio\/mp3"},{"name":"files\/warofworlds_b1_ch02_wells_64kb_woll8288.mp3","usrName":"warofworlds_b1_ch02_wells_64kb.mp3","size":4536704,"type":"audio\/mp3"},{"name":"files\/warofworlds_b1_ch03_wells_64kb_vbzl27ng.mp3","usrName":"warofworlds_b1_ch03_wells_64kb.mp3","size":3336320,"type":"audio\/mp3"},{"name":"files\/warofworlds_b1_ch04_wells_64kb_fdybmn8a.mp3","usrName":"warofworlds_b1_ch04_wells_64kb.mp3","size":3833600,"type":"audio\/mp3"},{"name":"files\/warofworlds_b1_ch06_wells_64kb_iavsdbqx.mp3","usrName":"warofworlds_b1_ch06_wells_64kb.mp3","size":2855936,"type":"audio\/mp3"},{"name":"files\/warofworlds_b1_ch05_wells_64kb_l82lslue.mp3","usrName":"warofworlds_b1_ch05_wells_64kb.mp3","size":4925504,"type":"audio\/mp3"},{"name":"files\/warofworlds_b1_ch08_wells_64kb_82spzg6p.mp3","usrName":"warofworlds_b1_ch08_wells_64kb.mp3","size":3312128,"type":"audio\/mp3"},{"name":"files\/warofworlds_b1_ch10_wells_64kb_bgv3f8n0.mp3","usrName":"warofworlds_b1_ch10_wells_64kb.mp3","size":6806528,"type":"audio\/mp3"},{"name":"files\/warofworlds_b1_ch07_wells_64kb_n1offnbh.mp3","usrName":"warofworlds_b1_ch07_wells_64kb.mp3","size":4267136,"type":"audio\/mp3"},{"name":"files\/warofworlds_b1_ch09_wells_64kb_eohkknbi.mp3","usrName":"warofworlds_b1_ch09_wells_64kb.mp3","size":6283328,"type":"audio\/mp3"},{"name":"files\/warofworlds_b2_ch07_wells_64kb_1y6ndhqm.mp3","usrName":"warofworlds_b2_ch07_wells_64kb.mp3","size":16417280,"type":"audio\/mp3"},{"name":"files\/warofworlds_b1_ch11_wells_64kb_dautbgxj.mp3","usrName":"warofworlds_b1_ch11_wells_64kb.mp3","size":6151040,"type":"audio\/mp3"},{"name":"files\/warofworlds_b1_ch13_wells_64kb_aledo534.mp3","usrName":"warofworlds_b1_ch13_wells_64kb.mp3","size":6020288,"type":"audio\/mp3"},{"name":"files\/warofworlds_b1_ch14_wells_64kb_cd212pib.mp3","usrName":"warofworlds_b1_ch14_wells_64kb.mp3","size":11562368,"type":"audio\/mp3"},{"name":"files\/warofworlds_b1_ch12_wells_64kb_eibzjw6p.mp3","usrName":"warofworlds_b1_ch12_wells_64kb.mp3","size":11322752,"type":"audio\/mp3"},{"name":"files\/warofworlds_b1_ch16_wells_64kb_g51vzwxq.mp3","usrName":"warofworlds_b1_ch16_wells_64kb.mp3","size":13554752,"type":"audio\/mp3"},{"name":"files\/warofworlds_b1_ch15_wells_64kb_djp9o4ju.mp3","usrName":"warofworlds_b1_ch15_wells_64kb.mp3","size":9244928,"type":"audio\/mp3"},{"name":"files\/warofworlds_b1_ch17_wells_64kb_4otwu4rd.mp3","usrName":"warofworlds_b1_ch17_wells_64kb.mp3","size":10668800,"type":"audio\/mp3"},{"name":"files\/warofworlds_b2_ch01_wells_64kb_4pebkj4j.mp3","usrName":"warofworlds_b2_ch01_wells_64kb.mp3","size":8106944,"type":"audio\/mp3"},{"name":"files\/warofworlds_b2_ch03_wells_64kb_0lqsspkr.mp3","usrName":"warofworlds_b2_ch03_wells_64kb.mp3","size":6123392,"type":"audio\/mp3"},{"name":"files\/warofworlds_b2_ch02_wells_64kb_a84ehsss.mp3","usrName":"warofworlds_b2_ch02_wells_64kb.mp3","size":10636736,"type":"audio\/mp3"}]

在我的系统中查看条目很好,文件正确显示,正如预期的那样。当我尝试对内容进行XML下载时,结果非常奇怪。

理想情况下,XML输出应该是BookTitle,Author,然后是文件列表,而不包括其他大小,类型,名称,文件,斜杠和“标记”。

对我来说,最好的方法是什么?

谢谢,

2 个答案:

答案 0 :(得分:0)

我找到了解决方案。作为参考,如果有人感兴趣,请使用JSON Parse读取字段数据,然后提取名称字段。

示例:

if ($result = $mysqli->query("SELECT * FROM AudioBooks where BookTitle = 'War of the Worlds' LIMIT 1")) {
while($row = $result->fetch_assoc()){
$string = $row['FileLocation'];
$json_a=json_decode($string,true);

foreach($json_a as $p)
  {
echo $p[name] . '<br />';
  }

}
$result->close();
}

答案 1 :(得分:0)

查看你的回复,我建议用json格式的json_decode字段添加到数组中,然后将其转换为XML。这是一个小例子:

$files = json_decode($row['FileLocation'],true);
$values = array('BookTitle' => $row['BookTitle'],
                'Author' => $row['Author'],
                'FileLocation' => $files
)

$xml = new SimpleXMLElement('<root/>');
array_walk_recursive($values, array ($xml, 'addChild'));
print $xml->asXML();