PHP traverses the XML file in the specified folder, with 100 points and 100 points. The XML file format is: & lt ;? Xml & nbsp; version = "1.0" & nbsp; encoding = "UTF-8 "? & Gt; & lt; template & gt; & lt; title & gt; & lt ;! [CDATA [the Krude family predicts that PHP will traverse the XML files 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 folder directory path: D: \ 001 \ 1003 (multiple XML files exist in this folder)
Start of traversal (read 4 XML files, and stop traversing if necessary)
Read the content of an XML file
$ Title = title in XML
$ Img = image in XML
$ Path = path of the XML file
Output the three variables to the page.
Traversal ends
4 Data records are read. each data entry contains the values of the title, img, and path variables.
Php xml
------ Solution --------------------
Manual, opendir traversal, DOMDocument parsing XML
------ Solution --------------------
$ 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
------ Solution --------------------
$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 !!!!
------ Solution --------------------
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.
Reference:
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 ......