Hi, i'm trying to access parts of this soap response xml, but I just cant get it. I dont understand the diffgram bit for a start. And do i need to think about the soap and vehicle_getvehicleandcomponentpartsresponse namespaces? I need the partnumber! Heres the xml <soap:envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:body> <vehicle_getvehicleandcomponentpartsresponse xmlns="http://tempuri.org/"> <vehicle_getvehicleandcomponentpartsresult> <xs:schema id="NewDataSet" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns=""> <diffgr:diffgram xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> <newdataset xmlns=""> <stddata msdata:roworder="0" diffgr:id="StdData1"> <enhanceddata msdata:roworder="0" diffgr:id="EnhancedData1"> <parts msdata:roworder="0" diffgr:id="Parts1"> <imagepath>http://topcat.adsapplications.co.uk/ImageStore/no_image.png</imagepath> <supplierid>1992</supplierid> <supplier>UK Reference</supplier> <partnumber>202</partnumber> Code (markup): I'm using curl, and the php code im trying: $result = curl_exec($ch); $enhanced_data = new SimpleXMLElement($result); $bat_part = $enhanced_data->children('diffgr', true)->children()->NewDataSet->Parts->partumber; PHP: I know this is wrong, but i have accessed similar data from a similar service, the only difference is the below soap response: <soap:envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:body> <vehicle_getvehicleandcomponentpartsresponse xmlns="http://tempuri.org/"> <vehicle_getvehicleandcomponentpartsresult> PHP: Surely this shouldnt be so difficult, am i doing somethign completely wrong?
Are you trying to just read data from the XML elements? If so, it's simple. Eg. <?php $doc = new DOMDocument(); $doc->load( 'books.xml' ); $books = $doc->getElementsByTagName( "book" ); foreach( $books as $book ) { $authors = $book->getElementsByTagName( "author" ); $author = $authors->item(0)->nodeValue; $publishers = $book->getElementsByTagName( "publisher" ); $publisher = $publishers->item(0)->nodeValue; $titles = $book->getElementsByTagName( "title" ); $title = $titles->item(0)->nodeValue; echo "$title - $author - $publisher\n"; } ?> PHP: So I expect for you to just grab the part number you'd just use: $partNums = $var->getElementsByTagName("partnumber"); foreach($partNums as $partNum) { $partNumber = $partNum->item(0)->nodeValue; } PHP: Good article on reading/writing PHP XML DOM: http://www.ibm.com/developerworks/library/os-xmldomphp/
unfortunately its not quite that simple, and your suggestions did not work. The xml is very complex. This is the entire xml returned, how do i get to the partnumber? <soap:envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:body> <vehicle_getvehicleandcomponentpartsresponse xmlns="http://tempuri.org/"> <vehicle_getvehicleandcomponentpartsresult> <xs:schema id="NewDataSet" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns=""> <diffgr:diffgram xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> <newdataset xmlns=""> <stddata msdata:roworder="0" diffgr:id="StdData1"> <make>CITROEN </make> <model>C3 SX </model> <cc>01360</cc> <vin>blah</vin> <engine_number>blah</engine_number> <reg_date>29-SEP-2002</reg_date> <manuf_date>29-SEP-2002</manuf_date> <colour>BLUE</colour> <fuel>PETROL</fuel> 5 DOOR HATCHBACK <co2>148</co2> <vehicle_gross_weight>0000000</vehicle_gross_weight> <imported>0</imported> <imported_date>01-JAN-0001</imported_date> <scrapped_date>01-JAN-0001</scrapped_date> <scrapped>0</scrapped> <unscrapped>0</unscrapped> <vehicle_max_tech_mass>00000</vehicle_max_tech_mass> <vehicle_max_power>000</vehicle_max_power> <vehicle_mass>000001080</vehicle_mass> <vehicle_seats>000</vehicle_seats> <wheelplan_desc>2 AXLE RIGID BODY</wheelplan_desc> <dvla_engine_code>KFV</dvla_engine_code> <vin_build_year>N/A</vin_build_year> <vin_build_month>N/A</vin_build_month> </stddata> <enhanceddata msdata:roworder="0" diffgr:id="EnhancedData1"> <fldmanlname>Citroen</fldmanlname> <fldmodlname>C3</fldmodlname> <fldmodverno>1A (4/2002 - 9/2005)</fldmodverno> <fldmanvercode>FC</fldmanvercode> <fldderlname>SX</fldderlname> <fldderverno>1A (4/2002 - 9/2005)</fldderverno> <fldspecdes> <fldstrgposlname>Right Hand Drive</fldstrgposlname> <fldstrgpossname>RHD</fldstrgpossname> <fldbstlname>Hatchback</fldbstlname> <fldbstsname>Hat</fldbstsname> <flddoocountlname>5 Doors</flddoocountlname> <flddoocountsname>5 drs</flddoocountsname> <fldseacountlname>5 Seats</fldseacountlname> <fldseacountsname>5 Seats</fldseacountsname> <flddrivetypelname>Front</flddrivetypelname> <flddrivetypesname>FWD</flddrivetypesname> <fldtratypelname>Manual</fldtratypelname> <fldtratypesname>Man</fldtratypesname> <fldgeacountlname>5 Gears</fldgeacountlname> <fldgeacountsname>5 gr</fldgeacountsname> <fldengsizelname>1.4 litres</fldengsizelname> <fldexactcc>1360</fldexactcc> <fldfuelname>Petrol</fldfuelname> <fldfuesname>Pet</fldfuesname> <fldfudlname>Injection</fldfudlname> <fldfudsname>Inj</fldfudsname> <fldvalcountlname>8 Valves</fldvalcountlname> <fldvalcountsname>8V</fldvalcountsname> <fldbhp>75</fldbhp> <fldkw>55</fldkw> <fldenginecode>KFV</fldenginecode> <fldgearboxcode> <fldcylcountlname>4 Cylinders</fldcylcountlname> <fldcylcountsname>4 Cyl</fldcylcountsname> <fldcamshaftlname>SOHC</fldcamshaftlname> <fldengalignlname>Transverse</fldengalignlname> <fldengalignsname>Trans</fldengalignsname> <fldcylconfigurationlname>Inline</fldcylconfigurationlname> <fldcylconfigurationsname>Inline</fldcylconfigurationsname> <fldengposlname>Front</fldengposlname> <fldengpossname>Frt</fldengpossname> <fldaxllname>4x2</fldaxllname> <fldaxlsname>4x2</fldaxlsname> <fldwheelbaselname>2460mm</fldwheelbaselname> <fldweilname>1470kg</fldweilname> <fldspec2des> <fldvehtypedes>Car</fldvehtypedes> <fldmfrcolour>Mauritius Blue,Oriental Blue,Tuscan Blue</fldmfrcolour> <fldspec2id>1226</fldspec2id> </fldspec2des> </fldgearboxcode> </fldspecdes> </enhanceddata> <parts msdata:roworder="0" diffgr:id="Parts1"> <imagepath>http://topcat.adsapplications.co.uk/ImageStore/no_image.png</imagepath> <supplierid>1992</supplierid> <supplier>UK Reference</supplier> <partnumber>202</partnumber> Code (markup): I've tried just about every example piece of code that i can find on the first 1000 pages of Google but i just cant get it to work Any ideas?
I think it's time to question your abilities then, not the example. Regardless of how complicated the XML is as long as you address correct children, nested elements and nodes you can get to anything. Why not post the code you tried? My example should work, even if the foreach is unnecessary, since there's only one part number element in the XML. Try this and post the result: <?php $doc = new DOMDocument(); $xml = //; dump all xml into var $doc->load( $xml ); print_r($doc->getElementsByTagName( "partnumber" )); ?> PHP: I shouldn't have to tell you about debugging and whitebox testing. Have you tied this before? Posting results would be helpful.
Hi code: $doc = new DOMDocument(); $xml = $result; $doc->load($xml); print_r($doc->getElementsByTagName( "partnumber" )); Code (markup): result: DOMNodeList Object ( [length] => 0 ) ??
Found a good example on StackOverflow. An excellent resource. I think the soap header was throwing the XML parser. I think you may be able to take it out and parse it as pure XML. Anyway, some answers below: $client = new SoapClient("http://path.to/wsdl?WSDL"); $res = $client->SoapFunction(array('param1'=>'value','param2'=>'value')); echo $res->PaymentNotification->payment; PHP: Read more here: http://stackoverflow.com/questions/4194489/how-to-parse-soap-xml
BRUm i appreciate your help and ive already spent the last 3 weeks trawling through stack overflow. Looking at examples of 'simple' xml structures yeah thats fine, but the xml im trying to parse has many child nodes and namespaces which is what is causing the problems and is where i need a little direction.
Your XML doesn't look complicated though. Partnumber isn't even nested. Have you posted your problem on stackoverflow? They've helped me a lot.
If its not complicated as you say, why am i having such trouble, and no one on the forums seems to be able to help me either. I always spend a lot of time trying to work everything out myself, and really only ask on forums etc as a last resort because I am literally getting no where. ok, maybe ill sign up to stack overflow and ask the question there. The problem is most people are just directing me to the php manual, which is no help at all
Trust me the guys on stackoverflow will be able to help you. What I've found is that complicated problems can often be a simple oversight or misunderstanding. The thing is that it shouldn't be complicated but has been, which is red flag. Php.net is only helpful as reference. Referring you there means they don't know the answer.
I managed to solve this by using a regex to remove everythign but what i needed, i know its not pretty but seems ok as its only a single result im after. $regex[] = '/.*?<PartNumber>/s'; $regex[] = '/<.*/s'; echo preg_replace($regex, '', $xml); Code (markup): One thing that was probably causing problems before is the xml i posted above was taken from firebug, which makes everything lowercase. Which im sure is what was causing the earlier issues with getting the data out as were were lookign for 'partnumber' when it was 'PartNumber' Thanks for your help and patience BRUm