Hi All, I need a PHP script to parse the following XML structure into PHP variables with the same name as the variables, I have not got the time to learn how to do it, and it needs to be done tonight! The first person to post here with the working answer will get $20 by paypal. Rgds, Chris <?xml version="1.0" encoding="UTF-8"?> <ResponseBlock Type="Live"> <Response Type="ADSLChecker"> <OperationResponse> <ErrorCode></ErrorCode> <PhoneNo><02082622254></PhoneNo> <FixedRate></FixedRate> <RateAdaptive></RateAdaptive> <ExchangeState></ExchangeState> <FullMsg>testing</FullMsg> <ExtraMsg>test</ExtraMsg> <ExchangeCode></ExchangeCode> <ExchangeName></ExchangeName> <ReasonCode></ReasonCode> <ReadyDate></ReadyDate> <RTPC></RTPC> </OperationResponse> </Response> </ResponseBlock> Code (markup):
here it the class : to use it : now to see results : You will have access to an array that contain your xml info .
Soz, I forgot to meantion it wouldn't be from a file. I have recieved it into a variable from a CURL command can you update your code for that Rgds, Chris
Working like this isn't going do. I have the need for the XML stuff in two scripts. If you want i'll pay you $50 to complete both scripts for me. I can provide full documentation for the XML webservice site. Please email me your MSN/AIM details if you are willing. Rgds, Chris
Try following class. Please note I have changed this to work and tested on PHP5, from the original code found on phpclasses.com USAGE: $xAry = new xml2array($xmlstring); $resultarray = $xAry->getResult(); print "<pre>\r\n"; print_r($resultarray); print "</pre>"; Code (markup): CLASS: class xml2array { function xml2array( $xml ) { if ( is_string($xml) ) { $responseDoc = new DomDocument(); if($responseDoc->loadXML($xml)) $this->root_element = $responseDoc->firstChild; } return FALSE; } function _recNode2Array( $domnode ) { if ( $domnode->nodeType == XML_ELEMENT_NODE ) { if ($domnode->hasChildNodes()) { $childs = $domnode->childNodes; for ($i = 0; $i < $childs->length; $i++) { $child = $childs->item($i); if ($child->nodeType == XML_ELEMENT_NODE) { $subnode = false; $prefix = ( $child->prefix ) ? $child->prefix.':' : ''; for ($j = 0; $j < $childs->length; $j++) { $testnode = $childs->item($j); if ( is_object($testnode) ) { if ($child->nodeName == $testnode->nodeName && !$child->isSameNode($testnode)) { $subnode = true; } } } // foreach ($childs as $testnode) // if ( is_object($testnode) ) // if ($child->nodeName == $testnode->nodeName && $child != $testnode) // $subnode = true; if ( is_array($result[ $prefix.$child->nodeName ]) ) $subnode = true; if ($subnode == true) $result[ $prefix.$child->nodeName ][] = $this->_recNode2Array($child); else $result[ $prefix.$child->nodeName ] = $this->_recNode2Array($child); } } } if ( !is_array($result) ) { $result['evalue'] = $domnode->nodeValue; } if ( $domnode->hasAttributes() ) foreach ( $domnode->attributes as $attrib ) { $prefix = ( $attrib->prefix ) ? $attrib->prefix.':' : ''; $result["@".$prefix.$attrib->name] = $attrib->value; } return $result; } } function getResult() { if ( $resultDomNode = $this->root_element ) { $array_result[ $resultDomNode->tagName ] = $this->_recNode2Array( $resultDomNode ); return $array_result; } else return false; } } Code (markup):