PHP XML Tips - Part 11: Do you want to find only those nodes belonging to a particular namespace? You can look solution like this:
01 <?php
02 $xmlString = <<< END
03 <?xml version="1.0"?>
04 <data xmlns:home="http://www.mysite.com/xmlns/home" xmlns:pdf="http://www.mysite.com/xmlns/work">
05 <home:file>content1.doc</home:file>
06 <pdf:file>content2.pdf</pdf:file>
07 <pdf:file>content3.pdf</pdf:file>
08 <home:file>content4.txt</home:file>
09 <home:file>content5.rtf</home:file>
10 <pdf:file>content6.pdf</pdf:file>
11 </data>
12 END;
13
14 $xml = simplexml_load_string($xmlString);
15
16 foreach($xml->children("http://www.mysite.com/xmlns/work") as $file){
17 echo $file ." <br />";
18 }
19 ?>
Result like this:
1 content2.pdf
2 content3.pdf
3 content6.pdf
01 <?php
02 $xmlString = <<< END
03 <?xml version="1.0"?>
04 <data xmlns:home="http://www.mysite.com/xmlns/home" xmlns:pdf="http://www.mysite.com/xmlns/work">
05 <home:file>content1.doc</home:file>
06 <pdf:file>content2.pdf</pdf:file>
07 <pdf:file>content3.pdf</pdf:file>
08 <home:file>content4.txt</home:file>
09 <home:file>content5.rtf</home:file>
10 <pdf:file>content6.pdf</pdf:file>
11 </data>
12 END;
13
14 $xml = simplexml_load_string($xmlString);
15
16 foreach($xml->children("http://www.mysite.com/xmlns/work") as $file){
17 echo $file ." <br />";
18 }
19 ?>
Result like this:
1 content2.pdf
2 content3.pdf
3 content6.pdf
No comments:
Post a Comment