frobak Posted August 20, 2012 Share Posted August 20, 2012 Hi Im trying to get the contents of a domdocument element. I need to get to the 'partnumber' of the below xml response from the soap request. can you tell me if this object has some values in it? object(DOMNodeList)#3 (1) { ["length"]=> int(0) } This is 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"> <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> and this is the php code im trying $dom = new DomDocument("1.0", "ISO-8859-1"); $dom->loadXML($result); $xpath = new DomXPath($dom); $queryResult = $xpath->query('/parts[@id="Parts1"]/partnumber'); $node = $queryResult->item(0); echo "{$node->nodeName} - {$node->nodeValue}"; SO if there is somethign in the object, how would i get to it? or id the object empty? What do you think? Quote Link to comment https://forums.phpfreaks.com/topic/267337-domdocument-help/ Share on other sites More sharing options...
MMDE Posted August 20, 2012 Share Posted August 20, 2012 Maybe you can learn something from this: http://forums.phpfreaks.com/index.php?topic=363621 If you need any more help, please look it up in the PHP manual on php.net. You will probably need to know some OOP, and expect that some of the lists aren't always a list of the type of objects you think they are. Quote Link to comment https://forums.phpfreaks.com/topic/267337-domdocument-help/#findComment-1370819 Share on other sites More sharing options...
frobak Posted August 20, 2012 Author Share Posted August 20, 2012 yes, unfortunately ive spent many hours on php.net, where the info is usually very technical for someone that is learning. Quote Link to comment https://forums.phpfreaks.com/topic/267337-domdocument-help/#findComment-1370827 Share on other sites More sharing options...
MMDE Posted August 20, 2012 Share Posted August 20, 2012 If it loads correctly, I would think you can access what you are asking for with this: $dom->getElementsByTagName('partnumber')->item(0)->value; Quote Link to comment https://forums.phpfreaks.com/topic/267337-domdocument-help/#findComment-1370834 Share on other sites More sharing options...
frobak Posted August 20, 2012 Author Share Posted August 20, 2012 yeah that doesnt work, the $part variable hasnt got anything in it, as below. $dom = new DomDocument("1.0", "ISO-8859-1"); $dom->loadXML($result); $part = $dom->getElementsByTagName('partnumber')->item(0)->value; But i know that the xml that i have listed above is in $result So why is so difficult to get to that element in the xml? Quote Link to comment https://forums.phpfreaks.com/topic/267337-domdocument-help/#findComment-1370842 Share on other sites More sharing options...
MMDE Posted August 20, 2012 Share Posted August 20, 2012 yeah that doesnt work, the $part variable hasnt got anything in it, as below. $dom = new DomDocument("1.0", "ISO-8859-1"); $dom->loadXML($result); $part = $dom->getElementsByTagName('partnumber')->item(0)->value; But i know that the xml that i have listed above is in $result So why is so difficult to get to that element in the xml? I tried using the xml you posted in the opening post. You should turn on error reporting: error_reporting(-1); ^ put that at the top of your script! I got lots of errors about the xml wasn't properly formatted, with other words, it didn't load correctly, as I said it would need to do earlier. Quote Link to comment https://forums.phpfreaks.com/topic/267337-domdocument-help/#findComment-1370846 Share on other sites More sharing options...
MMDE Posted August 20, 2012 Share Posted August 20, 2012 Oh, if all you want is partnumber, you can do this: $regex[] = '/.*?<partnumber>/s'; $regex[] = '/<.*/s'; echo preg_replace($regex, '', $xml); Quote Link to comment https://forums.phpfreaks.com/topic/267337-domdocument-help/#findComment-1370848 Share on other sites More sharing options...
frobak Posted August 20, 2012 Author Share Posted August 20, 2012 Yes i only need the part number thanks but that doesnt return anything?! $result = curl_exec($ch); $xml = $result; $regex[] = '/.*?<partnumber>/s'; $regex[] = '/<.*/s'; echo preg_replace($regex, '', $xml); var_dump($regex); array(2) { [0]=> string(18) "/.*?/s" [1]=> string(6) "/<.*/s" } also tried this: $part_no = preg_replace($regex, '', $xml); var_dump($part_no); string(0) "" Quote Link to comment https://forums.phpfreaks.com/topic/267337-domdocument-help/#findComment-1370850 Share on other sites More sharing options...
MMDE Posted August 20, 2012 Share Posted August 20, 2012 Try to look at the xml instead, because that's where the problems are. <?php $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"> <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>'; //$dom = new DomDocument("1.0", "ISO-8859-1"); //$dom->loadXML($xml); //$part = $dom->getElementsByTagName('partnumber')->item(0)->value; $regex[] = '/.*?<partnumber>/s'; $regex[] = '/<.*/s'; echo preg_replace($regex, '', $xml); ?> I know for sure that printed 202 for me. What preg_replace does is to try match the regex string I wrote against the xml string, and replace it with nothing. Because the regex is an array, it will go through the array and delete everything it matches in the order the array is. regex: dot matches any character (though not new line, unless there is a modifier which I specificed, namely s at the end of the string. the star repeats the previous infinite amount of times question mark makes it stop at next time it finds whatever is after, but also remember regex is greedy. So yeah, the first one matches everything and including <partnumber> Imagine that part being gone, and then < and everything after gets removed. All you are left with then is 202. Quote Link to comment https://forums.phpfreaks.com/topic/267337-domdocument-help/#findComment-1370928 Share on other sites More sharing options...
frobak Posted August 21, 2012 Author Share Posted August 21, 2012 Well i finally figured it out. I took what you said and explored the xml because it was working when we put static xml in the variable. And after many hours of debugging i finally realised that the xml response i posted above, the tag name of the xml element was actually 'PartNumber' not 'partnumber'. I copied the above from firebug, which stupidly makes everything lower case. So i just changed the regex to: $regex[] = '/.*?<PartNumber>/s'; Thanks MMDE for your patience and help on this, otherwise i'd still be in the dark. peace out Quote Link to comment https://forums.phpfreaks.com/topic/267337-domdocument-help/#findComment-1371110 Share on other sites More sharing options...
MMDE Posted August 21, 2012 Share Posted August 21, 2012 Well i finally figured it out. I took what you said and explored the xml because it was working when we put static xml in the variable. And after many hours of debugging i finally realised that the xml response i posted above, the tag name of the xml element was actually 'PartNumber' not 'partnumber'. I copied the above from firebug, which stupidly makes everything lower case. So i just changed the regex to: $regex[] = '/.*?<PartNumber>/s'; Thanks MMDE for your patience and help on this, otherwise i'd still be in the dark. peace out And you get nothing when you use this instead: $regex = array('/.*?<partnumber>/s', '/<.*/s'); Because the idea is that the regex run in order, first the one that remove everything in the file up to the point where the number is, then the next will remove all the stuff after the numbers. echo preg_replace(array('/.*?<partnumber>/s', '/<.*/s'), '', $xml); ^ could even make it as ugly as that lol EDIT: I just noticed the regex you posted uses capital P and capital N, instead of both being lowercase. regex is case sensitive, so please use the correct case. This could be your problem. Quote Link to comment https://forums.phpfreaks.com/topic/267337-domdocument-help/#findComment-1371130 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.