XML parsing need help on PHP
I have home work for parsing XML This is the xml :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>code</key>
<string>27</string>
<key>programname</key>
<string>Anında İstek Hattı</string>
<key>programmername</key>
<string>Selma Akbaş</string>
<key>avatar</key>
<string>selma-akbas-11.jpg</string>
</dict>
<dict>
<key>code</key>
<string>16</string>
<key>programname</key>
<string>Beyaz Gece</string>
<key>programmername</key>
<string>Okan Karakoca</string>
<key>avatar</key>
<string>okan-karakoca-7.jpg</string>
</dict>
</array>
</plist>
I try to parse with this code :
<?php
Header('charset=utf-8');
$test = simplexml_load_file('programlar.xml');
foreach ($test->array as $ar) {
foreach开发者_如何学Python ($ar->dict as $dict) {
echo($dict->key);
echo "<br/>";
echo($dict->string);
echo "<br/>";
}
}
?>
The parse is done, but it get some errors. It only show the first key-string for each dict
Can some one please fix this code? thanks
<?php
Header('charset=utf-8');
$test = simplexml_load_file('programlar.xml');
foreach ($test->array as $ar) {
foreach ($ar->dict as $dict) {
for ($i = 0; $i < count($dict->key); $i++) {
echo $dict->key[$i].'<br />';
echo $dict->string[$i].'<br />';
}
}
?>
I think this should work. Make sure you understand what is going on here before you turn it in haha. I am iterating through the "key" and "string" pairs using array style index keys, basically treating the two node sets like arrays.
精彩评论