PHP traverses the XML file in the specified folder, with 100 points and 100 points. The XML file format is:
<! [CDATA [notice of the krud family]>
130112001. jpg
《克鲁德一家》预告
130112001.3gp
An XML file is equivalent to a piece of data.
Since I am a beginner in PHP, I want to translate the following code...
Specify the directory path of the folder: D: \ 001 \ 1003 (there are multiple XML files in this folder) (read four XML files, and then stop traversing) read the content of an XML file $ title = title $ img in XML = image $ path in XML = path of the XML file. output the three variables to the end of page traversal.
4 Data records are read. each data entry contains the values of the title, img, and path variables.
Reply to discussion (solution)
The Avatar was suddenly cleared ...... Wei Chen retired ......
The Avatar was suddenly cleared ...... Wei Chen retired ......
Help me solve the problem, and I will pay back my profile picture right away.
$doc = new DOMDocument();$doc->load ("a.xml");$tmp = $doc->getElementsByTagName('template');foreach($tmp as $t){Echo 'title:'.$t->getElementsByTagName('title')->item(0)->nodeValue."
";Echo 'image:'.$t->getElementsByTagName('image')->item(0)->nodeValue."
";Echo 'intro:'.$t->getElementsByTagName('intro')->item(0)->nodeValue."
";Echo 'mediapath:'.$t->getElementsByTagName('mediapath')->item(0)->nodeValue."
";}
Manual, opendir traversal, DOMDocument parsing XML
$ I = 0 foreach (glob ("D: \ 001 \ 1003 \\*. xml ") as $ filename) {if ($ I> 3) break; read $ filename ...... $ I ++ ;}
Partial Reference http://bbs.csdn.net/topics/390354196 for reading $ filename
Too lazy to write
Er, I wrote the double equal sign ......
$dir='D:\001\1003';function readxml($dir,&$arr){if($handle=opendir($dir)){while(false!==($file=readdir($handle))){$sub_dir=$dir.DIRECTORY_SEPARATOR.$file;if(is_dir($sub_dir)&&$file!='.'&&$file!='..'){readxml($sub_dir,$arr);}elseif(preg_match('/\.xml$/',$file)){if(count($arr)>3){return false;}$arr[]=$sub_dir;}}}}readxml($dir,$arr);foreach($arr as $x){$doc = new DOMDocument();$doc->load ($x);$tmp = $doc->getElementsByTagName('template');foreach($tmp as $t){Echo 'title:'.$t->getElementsByTagName('title')->item(0)->nodeValue."
";Echo 'image:'.$t->getElementsByTagName('image')->item(0)->nodeValue."
";Echo 'intro:'.$t->getElementsByTagName('intro')->item(0)->nodeValue."
";Echo 'mediapath:'.$t->getElementsByTagName('mediapath')->item(0)->nodeValue."
";}Echo "--------------------------------------
";}
Subscribe !!!!
Actually cleared, Dizzy
Foreach (glob ("D: \ 001 \ 1003 \ *. xml") as $ key => $ filename)
You can use $ key to determine whether to read the four tables to stop traversal. you do not need $ I.
PHP code? 123456 $ I = 0 foreach (glob ("D: \ 001 \ 1003 \\*. xml ") as $ filename) {if ($ I> 3) break; read $ filename ...... $ I ++ ;}
Partial Reference http://bbs.csdn.net/topics/390354196 for reading $ filename
Too lazy to write ......
PHP code? 1234567891011121314151617181920212223242526272829 $ dir = 'd: \ 001 \ 1003 '; function readxml ($ dir, & $ arr) {if ($ handle = opendir ($ dir) {while (false! ==( $ File = readdir ($ handle )))......
Thank you, bro. I have time to talk about it and add a few comments to this code. I am so good at learning.
Here is also a function for PHP to traverse folders.
You can refer to: http://www.bacysoft.cn/thread-77-1-1.html