kevinh448 Posted May 27, 2011 Share Posted May 27, 2011 Im fairly new to php, but not programming. Im writing a program that communicates via RESTful api, with a bunch of other fun stuff. Everything has gone smoothly except parsing this one XML response. I used documented test xml, but the simplexml_load_string() function creates a ton of warnings, and my simpleXMLElement is a 0 value not an object. Is it the XML, or is it me? maybe a little both? This is not the best way, im sure, but I think it should work. <?php $xml = <<<EOF <resourceList xmlns="http://www.someplace.com/place/api/source/[ver sion_id]" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.someplace.com/place/api/reso urceList/[version_id] resourceList.xsd" location="http://some.location"> <resourceURL location=" http://[client_id].someplace. com/apifleet/rest/[client_id]/saledetails/[sale_id]" metadata1=”[campaign_id]“ metadata2=”[affiliate_id]“ metadata3=”[creative_id]“ metadata4=”[transactionID]” /> <resourceURL location=" http://[client_id].someplace. com/apifleet/rest/[client_id]/saledetails/[sale_id]" metadata1=”[campaign_id]“ metadata2=”[affiliate_id]“ metadata3=”[creative_id]“ metadata4=”[transactionID]” /> <resourceURL location=" http://[client_id].someplace. com/apifleet/rest/[client_id]/saledetails/[sale_id]" metadata1=”[campaign_id]“ metadata2=”[affiliate_id]“ metadata3=”[creative_id]“ metadata4=”[transactionID]” /> </resourceList> EOF; $xmlElem = simplexml_load_string($xml, 'SimpleXMLElement'); $count = 0; foreach($xmlElem as $resourceURL) { $saleItem[$count][0] = $resourceURL->location; //location url $saleItem[$count][1] = $resourceURL->metadata1;//campaign_id $saleItem[$count][2] = $resourceURL->metadata2;//affiliate_id $saleItem[$count][3] = $resourceURL->metadata3;//creative_id $saleItem[$count][4] = $resourceURL->metadata4;//transactionID $count++; } ?> Ok so if someone can explain to me why that doesnt work, But this works: <?php $xml = <<<EOF <people> <person name="Person 1"> <child/> <child/> <child/> </person> <person name="Person 2"> <child/> <child/> <child/> <child/> <child/> </person> </people> EOF; $elem = new SimpleXMLElement($xml); foreach ($elem as $person) { printf("%s has got %d children.\n", $person['name'], $person->count()); } ?> Its almost embarrassing... Any tips would be great! Thanks guys. Quote Link to comment Share on other sites More sharing options...
requinix Posted May 27, 2011 Share Posted May 27, 2011 XML can't have whitespace in the middle of the opening or closing tags. You also can't use smart quotes. Quote Link to comment Share on other sites More sharing options...
dreamwest Posted May 27, 2011 Share Posted May 27, 2011 Use CDATA with characters in strings http://www.wizecho.com/nav=php&s=xml Quote Link to comment Share on other sites More sharing options...
salathe Posted May 27, 2011 Share Posted May 27, 2011 Is the XML the problem, or is it me? Both. The XML has bad/invalid parts (see the warnings and earlier replies) and the way that you're trying to access the resourceURL attributes is wrong. To fix the former, don't use broken URLs in namespace declarations (xmlns=...). To fix the latter, read through the basic SimpleXML examples page in the manual. Have a stab at fixing the problems yourself, and let us know how you get on. Quote Link to comment Share on other sites More sharing options...
kevinh448 Posted May 27, 2011 Author Share Posted May 27, 2011 Yup. I didn't even know about Smart Quotes. It has to be one of the dumbest things.. Was also accessing them wrong, was an easy fix though. Learn something new everyday.. Thanks everyone! Quote Link to comment Share on other sites More sharing options...
salathe Posted May 27, 2011 Share Posted May 27, 2011 Excellent, have fun with your script. Quote Link to comment 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.