...

View Full Version : please help me on Dom XML



xiaodao
01-05-2008, 06:35 PM
My xml file


<?xml version="1.0" encoding="UTF-8"?>
<imgList>
<list path="images/ads/shop1.jpg" info="">http://www.lemon.com</list>
<list path="images/ads/shop2.jpg" info="">http://www.lemon.com</list>
</imgList>



Below is my php code to get the xml file values



$xml= new DOMDocument();
$xml->preserveWhiteSpace=false;
$xml->load('../imgList.xml');
print_r(xmlToarray($xml));
print_r(attToarray($xml));
function xmlToArray($n)
{
$xml_array = array();
$occurance = array();

foreach($n->childNodes as $nc)
{
$occurance[$nc->nodeName]++;
}

foreach($n->childNodes as $nc){
if( $nc->hasChildNodes() )
{
if($occurance[$nc->nodeName] > 1)
{
$xml_array[$nc->nodeName][] = xmlToArray($nc);
}
else
{
$xml_array[$nc->nodeName] = xmlToArray($nc);
}
}
else
{
return utf8_decode($nc->nodeValue);
}
}
return $xml_array;
}


however this code can only get the values of my xml file, i cannot get the attributes of the xml file which is important, can anybody help me?

the-dream
01-05-2008, 06:46 PM
I don't think I understand, do you want to get this bit:


<?xml version="1.0" encoding="UTF-8"?>
<imgList>
<list path="images/ads/shop1.jpg" info="">http://www.lemon.com</list>
<list path="images/ads/shop2.jpg" info="">http://www.lemon.com</list>
</imgList>

All of it, if so...

Then...


<?php
file_get_contents('yourfile.xml');
?>

Would work...
http://uk3.php.net/manual/en/function.file-get-contents.php

If you echo'd it:


<?php
echo file_get_contents('yourfile.xml');
?>

The output would be:

<?xml version="1.0" encoding="UTF-8"?>
<imgList>
<list path="images/ads/shop1.jpg" info="">http://www.lemon.com</list>
<list path="images/ads/shop2.jpg" info="">http://www.lemon.com</list>
</imgList>

xiaodao
01-05-2008, 07:02 PM
no, the xml file is my original file

i need to get all attributes and nodevalues into arrays, so i can work on the data



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum