PHP traverses the XML data in the specified folder, 100 points, 100 points

Source: Internet
Author: User
Tags glob
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 ......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.