php数组转XML救助。
想写一个提取照片的插件,虽然我不懂PHP,不懂XML,但知道谷歌与开源的力量强大,于是乎“无知者无畏”。没想到再次被击得晕头转向了。
能够读取XML了,能够裁切图片了,能够生成相应的路径数据了,再建立一个XML就可以保存数据在本地,然后再加一个读取显示的功能就完全OK了。没想到就这个建立XML的问题搞垮了我。
方法是这样的,我将一组xml里的信息的一部分读取下来,并重新构造,再将值放进了一个这样数组里,数组样式如下:“$readdata[]['aaa']=值;$readdata[]['bbb']=值;$readdata[]['ccc']=值;”然后从网上搜罗了各种方法想去将这个数组转成xml,最终没能成功。
从网上搜罗一堆代码,没有哪种是类似的,而且非常让人恼火的是大部分都在重复着PHP5手册的例题,还大颜不惭的在那说是原创,天啊,喜悦国际村、CSDN,你们哪位里面有真实的东西可以帮我啊……
源代码如下:
$doc = new DOMDocument('1.0','utf-8');
$doc->formatOutput = true;
$root = $doc->createElement("root");
$doc->appendChild($root);
$items = $doc->createElement("item");
$root->appendChild($items);
foreach($readdata as $values) {
foreach($values as $key=>$val) {
$items->appendChild(new DOMElement($key,$val));
}
}
$doc->save("book1.xml");
想得到的结果如下:
<?xml version="1.0" encoding="utf-8"?>
<root>
<item>
<title>小伙伴</title>
<th_l_url>http://localhost/th_l_CIMG3627.jpg</th_l_url>
<th_l_filename>th_l_CIMG3627.jpg</th_l_filename>
<th_s_url>http://localhost/th_s_CIMG3627.jpg</th_s_url>
<th_s_filename>th_s_CIMG3627.jpg</th_s_filename>
<th_m_url>http://localhost/th_m_CIMG3627.jpg</th_m_url>
<th_m_filename>th_m_CIMG3627.jpg</th_m_filename>
<th_b_url>http://localhost/th_b_CIMG3627.jpg</th_b_url>
<th_b_filename>th_b_CIMG3627.jpg</th_b_filename>
</item>
<item>
<title>不许跑</title>
<th_l_url>http://localhost/th_l_CIMG5393.jpg</th_l_url>
<th_l_filename>th_l_CIMG5393.jpg</th_l_filename>
<th_s_url>http://localhost/th_s_CIMG5393.jpg</th_s_url>
<th_s_filename>th_s_CIMG5393.jpg</th_s_filename>
<th_m_url>http://localhost/th_m_CIMG5393.jpg</th_m_url>
<th_m_filename>th_m_CIMG5393.jpg</th_m_filename>
<th_b_url>http://localhost/th_b_CIMG5393.jpg</th_b_url>
<th_b_filename>th_b_CIMG5393.jpg</th_b_filename>
</item>
<item>
<title>笑一个</title>
<th_l_url>http://localhost/th_l_CIMG5413.jpg</th_l_url>
<th_l_filename>th_l_CIMG5413.jpg</th_l_filename>
<th_s_url>http://localhost/th_s_CIMG5413.jpg</th_s_url>
<th_s_filename>th_s_CIMG5413.jpg</th_s_filename>
<th_m_url>http://localhost/th_m_CIMG5413.jpg</th_m_url>
<th_m_filename>th_m_CIMG5413.jpg</th_m_filename>
<th_b_url>http://localhost/th_b_CIMG5413.jpg</th_b_url>
<th_b_filename>th_b_CIMG5413.jpg</th_b_filename>
</item>
</root>
实际得到的结果如下:
<?xml version="1.0" encoding="utf-8"?>
<root>
<item>
<title>小伙伴</title>
<th_l_url>http://localhost/th_l_CIMG3627.jpg</th_l_url>
<th_l_filename>th_l_CIMG3627.jpg</th_l_filename>
<th_s_url>http://localhost/th_s_CIMG3627.jpg</th_s_url>
<th_s_filename>th_s_CIMG3627.jpg</th_s_filename>
<th_m_url>http://localhost/th_m_CIMG3627.jpg</th_m_url>
<th_m_filename>th_m_CIMG3627.jpg</th_m_filename>
<th_b_url>http://localhost/th_b_CIMG3627.jpg</th_b_url>
<th_b_filename>th_b_CIMG3627.jpg</th_b_filename>
<title>不许跑</title>
<th_l_url>http://localhost/th_l_CIMG5393.jpg</th_l_url>
<th_l_filename>th_l_CIMG5393.jpg</th_l_filename>
<th_s_url>http://localhost/th_s_CIMG5393.jpg</th_s_url>
<th_s_filename>th_s_CIMG5393.jpg</th_s_filename>
<th_m_url>http://localhost/th_m_CIMG5393.jpg</th_m_url>
<th_m_filename>th_m_CIMG5393.jpg</th_m_filename>
<th_b_url>http://localhost/th_b_CIMG5393.jpg</th_b_url>
<th_b_filename>th_b_CIMG5393.jpg</th_b_filename>
<title>笑一个</title>
<th_l_url>http://localhost/th_l_CIMG5413.jpg</th_l_url>
<th_l_filename>th_l_CIMG5413.jpg</th_l_filename>
<th_s_url>http://localhost/th_s_CIMG5413.jpg</th_s_url>
<th_s_filename>th_s_CIMG5413.jpg</th_s_filename>
<th_m_url>http://localhost/th_m_CIMG5413.jpg</th_m_url>
<th_m_filename>th_m_CIMG5413.jpg</th_m_filename>
<th_b_url>http://localhost/th_b_CIMG5413.jpg</th_b_url>
<th_b_filename>th_b_CIMG5413.jpg</th_b_filename>
</item>
</root>
$items = $doc->createElement("item");
$root->appendChild($items);
如果将上面这两句移到第一层循环里,那么就产生如下结果:
<?xml version="1.0" encoding="utf-8"?>
<root>
<item>
<title>小伙伴</title>
</item>
<item>
<th_l_url>http://localhost/th_l_CIMG3627.jpg</th_l_url>
</item>
<item>
<th_l_filename>th_l_CIMG3627.jpg</th_l_filename>
</item>
<item>
<th_s_url>http://localhost/th_s_CIMG3627.jpg</th_s_url>
</item>
<item>
<th_s_filename>th_s_CIMG3627.jpg</th_s_filename>
........
肯定的说,我把循环或这个数组结构没搞清楚,或者我的方法根本就错了,拜托谁能帮帮忙啊……

最新评论