Jump to content

simplexml_load_string warnings. Is the XML the problem, or is it me?


kevinh448

Recommended Posts

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.