...

View Full Version : Parse Multiple XML files?



warlord
03-08-2007, 05:30 PM
I'm using the following code to parse an .xml file, although I need a way to do multiple files at once ... Is that possible?




<?php

$request = 'file.xml';

// Fetch it
$response = file_get_contents($request);

if ($response === false) {
die('The request failed');
}

// Create a new DOM object
$dom = new DOMDocument('1.0', 'UTF-8');

// Load the XML into the DOM
if ($dom->loadXML($response) === false) {
die('Parsing failed');
}

function xml_to_result($dom) {
$root = $dom->firstChild;
foreach($root->attributes as $attr) $res[$attr->name] = $attr->value;
$node = $root->firstChild;
$i = 0;
while($node) {
switch($node->nodeName) {
case 'Result':
$subnode = $node->firstChild;
while($subnode) {
$subnodes = $subnode->childNodes;
foreach($subnodes as $n) {
if($n->hasChildNodes()) {
foreach($n->childNodes as $cn) $res[$i][$subnode->nodeName][$n->nodeName]=trim($cn->nodeValue);
} else $res[$i][$subnode->nodeName]=trim($n->nodeValue);
}
$subnode = $subnode->nextSibling;
}
break;
default:
$res[$node->nodeName] = trim($node->nodeValue);
$i--;
break;
}
$i++;
$node = $node->nextSibling;
}
return $res;
}

$res = xml_to_result($dom);

echo '<pre>';
print_r($res);
echo '</pre>';

?>

shyam
03-08-2007, 06:16 PM
yes...parse them one after the other

warlord
03-08-2007, 06:28 PM
yes...parse them one after the other

I think anyone could come to that conclusion. :rolleyes:

Could you be a little more specific? An example is what I'm looking for ...

StupidRalph
03-08-2007, 06:37 PM
Can't you just put all the requested XML files into an array and loop thru with a foreach statement?



<?php

$request = array('file.xml','file2.xml','file3.xml','file4.xml');

foreach ($request as $req) {
// Fetch it
$response = file_get_contents($req);

if ($response === false) {
die('The request failed');
}

// Create a new DOM object
$dom = new DOMDocument('1.0', 'UTF-8');

// Load the XML into the DOM
if ($dom->loadXML($response) === false) {
die('Parsing failed');
}

function xml_to_result($dom) {
$root = $dom->firstChild;
foreach($root->attributes as $attr) $res[$attr->name] = $attr->value;
$node = $root->firstChild;
$i = 0;
while($node) {
switch($node->nodeName) {
case 'Result':
$subnode = $node->firstChild;
while($subnode) {
$subnodes = $subnode->childNodes;
foreach($subnodes as $n) {
if($n->hasChildNodes()) {
foreach($n->childNodes as $cn) $res[$i][$subnode->nodeName][$n->nodeName]=trim($cn->nodeValue);
} else $res[$i][$subnode->nodeName]=trim($n->nodeValue);
}
$subnode = $subnode->nextSibling;
}
break;
default:
$res[$node->nodeName] = trim($node->nodeValue);
$i--;
break;
}
$i++;
$node = $node->nextSibling;
}
return $res;
}

$res = xml_to_result($dom);

echo '<pre>';
print_r($res);
echo '</pre>';
}
?>

Inigoesdr
03-08-2007, 06:44 PM
Can't you just put all the requested XML files into an array and loop thru with a foreach statement?



$response = file_get_contents($request);


Yes, but change $request to $req.

StupidRalph
03-08-2007, 06:57 PM
:eek: doh! I thought I changed it...I'll edit it.

warlord
03-08-2007, 07:17 PM
I now get:

Fatal error: Cannot redeclare xml_to_result() on line 32

:eek:

Inigoesdr
03-08-2007, 07:21 PM
Move the function out of the loop.

warlord
03-08-2007, 07:36 PM
I did that, although now only the last .xml file displays the array info ...

Inigoesdr
03-08-2007, 07:49 PM
<?php

$request = array('file.xml','file2.xml','file3.xml','file4.xml');

function xml_to_result($dom) {
$root = $dom->firstChild;
foreach($root->attributes as $attr) $res[$attr->name] = $attr->value;
$node = $root->firstChild;
$i = 0;
while($node) {
switch($node->nodeName) {
case 'Result':
$subnode = $node->firstChild;
while($subnode) {
$subnodes = $subnode->childNodes;
foreach($subnodes as $n) {
if($n->hasChildNodes()) {
foreach($n->childNodes as $cn) $res[$i][$subnode->nodeName][$n->nodeName]=trim($cn->nodeValue);
} else $res[$i][$subnode->nodeName]=trim($n->nodeValue);
}
$subnode = $subnode->nextSibling;
}
break;
default:
$res[$node->nodeName] = trim($node->nodeValue);
$i--;
break;
}
$i++;
$node = $node->nextSibling;
}
return $res;
}

foreach ($request as $req)
{
// Fetch it
$response = file_get_contents($req);

if ($response === false)
{
die('The request failed');
}

// Create a new DOM object
$dom = new DOMDocument('1.0', 'UTF-8');

// Load the XML into the DOM
if ($dom->loadXML($response) === false)
{
die('Parsing failed');
}

$res = xml_to_result($dom);

echo '<pre>';
print_r($res);
echo '</pre>';
}
?>
That works fine for me.

warlord
03-08-2007, 07:54 PM
That works fine for me.

I guess it helps to know what you're doing too ... :thumbsup:

Works perfectly now! Thank you sooooo much Inigoesdr and StupidRalph ~

StupidRalph
03-08-2007, 07:54 PM
Post your current code...Does your code look something like this? If its only printin the last xml file its b/c the variable is being overwritten with each consecutive loop. You need to print out the variable before you leave the loop.


<?php


function xml_to_result($dom) {
$root = $dom->firstChild;
foreach($root->attributes as $attr) $res[$attr->name] = $attr->value;
$node = $root->firstChild;
$i = 0;
while($node) {
switch($node->nodeName) {
case 'Result':
$subnode = $node->firstChild;
while($subnode) {
$subnodes = $subnode->childNodes;
foreach($subnodes as $n) {
if($n->hasChildNodes()) {
foreach($n->childNodes as $cn) $res[$i][$subnode->nodeName][$n->nodeName]=trim($cn->nodeValue);
} else $res[$i][$subnode->nodeName]=trim($n->nodeValue);
}
$subnode = $subnode->nextSibling;
}
break;
default:
$res[$node->nodeName] = trim($node->nodeValue);
$i--;
break;
}
$i++;
$node = $node->nextSibling;
}
return $res;
}

$request = array('file.xml','file2.xml','file3.xml','file4.xml');

foreach ($request as $req) {

// Fetch it
$response = file_get_contents($req);

if ($response === false) {
die('The request failed');
}

// Create a new DOM object
$dom = new DOMDocument('1.0', 'UTF-8');

// Load the XML into the DOM
if ($dom->loadXML($response) === false) {
die('Parsing failed');
}
$res = xml_to_result($dom);

echo '<pre>';
print_r($res);
echo '</pre>';
}




?>

Nevermind :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum