Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP4 XML HELP

Status
Not open for further replies.

iteach2

Technical User
Sep 16, 2001
120
US
Hi Gurus,

I am using code from yahoo developer network to retrieve/parse the XML below into an Array:
Code:
  <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
- <SearchResult count="388" xmlns="">
- <products>
- <productSummary id="1010042542">
  <manufacturer sku="45010" name="Elite Image" id="102404" /> 
- <skus>
  <sku val="14111525" type="UNSPSC" /> 
  <sku val="ELI 45010" type="SP Richards Catalog" /> 
  <sku val="SPR45010" type="SP Richards Old" /> 
  <sku val="035255450102" type="UPC" /> 
  <sku val="ELI45010" type="SP Richards" /> 
  <sku val="UNV95200" type="United" /> 
  <sku val="45010" type="MFGPARTNUMBER" /> 
  </skus>
- <descriptions>
  <pd t="3">Letter - 8.5" x 11" - 5000 Sheet - Multipurpose Paper - White</pd> 
  <pd t="2">Elite Image Multipurpose Paper</pd> 
  <pd t="1">Elite Image Multipurpose Paper - Letter - 8.5" x 11" - 5000 Sheet - Multipurpose Paper - White</pd> 
  <pd t="0">Multipurpose Paper, 96 Bright, 20Ib., 8-1/2"x11", White</pd> 
  </descriptions>
  </productSummary>



Here is the yahoo code

Code:
// The web service request
$request =  '$url';

$response = file_get_contents($request);

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

// Parse the XML into a DOM tree
$error = null;
if (!$dom = domxml_open_mem($response, DOMXML_LOAD_PARSING, $error)) {
	echo "XML parse error\n";
	foreach ($error as $errorline) {
		// For production use this should be logged to a file instead
		echo $errorline['errormessage']."<br />\n";
		echo " Node  : " . $errorline['nodename'] . "<br />\n";
		echo " Line  : " . $errorline['line'] . "<br />\n";
		echo " Column : " . $errorline['col'] . "<br />\n";
	}
	die('XML parse error');
}


function xml_to_result($dom) {
	$root = $dom->document_element();
	$res['totalResultsAvailable'] = $root->get_attribute('totalResultsAvailable');
	$res['totalResultsReturned'] = $root->get_attribute('totalResultsReturned');
	$res['firstResultPosition'] = $root->get_attribute('firstResultPosition');

	$node = $root->first_child();
	$i = 0;
	$k = null;
	while($node) {
		switch($node->tagname) {
			case 'Result':
				$subnode = $node->first_child();
				while($subnode) {
					$subnodes = $subnode->child_nodes();
					if(!empty($subnodes)) {
						foreach($subnodes as $k=>$n) {
							if(empty($n->tagname)) {
								$res[$i][$subnode->tagname] = trim($n->get_content());
							} else {
								$res[$i][$subnode->tagname][$n->tagname]=trim($n->get_content());
							}
						}
					}
					$subnode = $subnode->next_sibling();
				}
				break;
			default:
				$res[$node->tagname] = trim($node->get_content());
				$i--;
				break;
		}
		$i++;
		$node = $node->next_sibling();
	}
	return $res;
}

$res = xml_to_result($dom);

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

The code works, but it does not traverse all the child nodes. It only grabs the <descriptions> out of the<product> tag......Like so...

Code:
Array
(
    [totalResultsAvailable] => 388
    [#text] => 
    [products] => Letter - 8.5" x 11" - 5000 Sheet - Multipurpose Paper - White
                Elite Image Multipurpose Paper
                Elite Image Multipurpose Paper - Letter - 8.5" x 11" - 5000 Sheet - Multipurpose Paper - White
                Multipurpose Paper, 96 Bright, 20Ib., 8-1/2"x11", White
            
         
            
                Letter - 8.5" x 11" - 2500 Sheet - Multipurpose Paper - White
                Elite Image Multipurpose Paper
                Elite Image Multipurpose Paper - Letter - 8.5" x 11" - 2500 Sheet - Multipurpose Paper - White
                Multipurpose Paper, 96 Bright, 20Ib., 8-1/2"x11", White
            
        
        
                
            
            
                1" x 2.62" - 3000 Label - Mailing Label - White
                Elite Image White Mailing Laser Label
                Elite Image White Mailing Laser Label - 1" x 2.62" - 3000 Label - Mailing Label - White
                Mailing Label, Laser, 1"x2-5/8", 3000/BX, White
            

            
                2000 Page - Black - Toner Cartridge
                Elite Image Black Toner Cartridge
                Elite Image Black Toner Cartridge - 2000 Page - Black
                Toner Cartridge, 2000 Page Yield, Black
)


I was hoping some could help me to modify the yahoo code to get all the child node....I appreciate "ANY" feedback thank you all. PEACE
 
i can't help thinking that the built-in function xml_parse_into_struct() might be more useful to you.

Code:
<?php
ini_set('error_display', 'on');
error_reporting (E_ALL);
$xml = <<<XML
<SearchResult count="388" xmlns="">
 <products>
 <productSummary id="1010042542">
  <manufacturer sku="45010" name="Elite Image" id="102404" /> 
 <skus>
  <sku val="14111525" type="UNSPSC" /> 
  <sku val="ELI 45010" type="SP Richards Catalog" /> 
  <sku val="SPR45010" type="SP Richards Old" /> 
  <sku val="035255450102" type="UPC" /> 
  <sku val="ELI45010" type="SP Richards" /> 
  <sku val="UNV95200" type="United" /> 
  <sku val="45010" type="MFGPARTNUMBER" /> 
  </skus>
 <descriptions>
  <pd t="3">Letter - 8.5" x 11" - 5000 Sheet - Multipurpose Paper - White</pd> 
  <pd t="2">Elite Image Multipurpose Paper</pd> 
  <pd t="1">Elite Image Multipurpose Paper - Letter - 8.5" x 11" - 5000 Sheet - Multipurpose Paper - White</pd> 
  <pd t="0">Multipurpose Paper, 96 Bright, 20Ib., 8-1/2"x11", White</pd> 
  </descriptions>
  </productSummary>
XML;
$p = xml_parser_create();
xml_parse_into_struct($p, $xml, $vals, $index);
echo "<pre>" . print_r($vals, true) . print_r($index, true) . "</pre>";
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top