I want to perform an if check on an XML file I have loaded in using simplexml_load_file.
I have accessed the data when the output is as I want it to be, although as it's web services sometimes they may be a response which I need to cater for. The below is the XML I want to be able to check on:
<?xml version="1.0" encoding="utf-8"?>
<ProductSizes />
Can somebody help me out? I am using the below:
$xmlstock = simplexml_load_file($xmlfeed);
Assuming that the xml source looks something like the following when actually populated:
<?xml version="1.0" encoding="utf-8"?>
<ProductSizes>
<ProductSize />
<ProductSize />
<ProductSize />
</ProductSizes>
You can get a count using the count() api call:
$xmlstock = simplexml_load_file($xmlfeed);
$hasProductSizes = ($xmlstock->ProductSize->count() > 0) ? true : false;
More info on the count() API via PHP docs.
Related
i am having an issue with parsing an XML file using SimpleXML and PHP.
The XML file in question is provided by a third party and includes a number of child elements (going down multiple levels) within it. I know which elements i require and can see them within the XML file, but i just can't seem to get them to print using PHP.
Example XML feed for test.xml:
<?xml version="1.0" encoding="utf-8"?>
<Element1 xmlns="" release="8.1" environment="Production" lang="en-US">
<Element2>
<Element3>
<Element4>
<Element5>it worked</Element5>
</Element4>
</Element3>
</Element2>
</Element1>
The file only includes one of each attribute so i can be very particular with the request, the code i have so far is below:
$lib=simplexml_load_file("test.xml");
$make=$lib->Element1->Element2->Element3->Element4->Element5;
print $make;
I have tried to look this up before asking, but the only solutions i can see are when the child attributes are unknown or there are multiple results for each request, which is not the case in this instance.
Any help or guidance would be greatly received.
Thanks
In your code above, $lib is Element1. So you just need to drop one of your references. This:
$make=$lib->Element1->Element2->Element3->Element4->Element5;
Should become this:
$make=$lib->Element2->Element3->Element4->Element5;
Also, SimpleXML is an awful awful awful awful interface (considering that "Simple" is in the name and there is mass confusion about how to use it). I would always recommend DOMDocument instead.
I'd strongly recommend using xpath as it will give you more flexibility e.g. Allow you to restrict results based on xml node attributes.
$xml = simplexml_load_string('<?xml version="1.0" encoding="utf-8"?>
<Element1 xmlns="" release="8.1" environment="Production" lang="en-US">
<Element2>
<Element3>
<Element4>
<Element5>it worked</Element5>
</Element4>
</Element3>
</Element2>
</Element1>');
$data=$xml->xpath('/Element1/Element2/Element3/Element4/Element5');
echo (string)$data[0]; //outputs 'it worked'
//this also works
$data=$xml->xpath('//Element5');
echo (string)$data[0]; //outputs 'it worked'
I am trying to import a XML file via API into my php script which then will parse said XML file and extract a string. Ive searched across the webspace for an answer, and though I have found a ton of resources I still cannot get this script to work.
The XML file that I am loading will look something like this
<api version="2">
<currentTime>2012-07-28</currentTime>
<result>
<rowset name="accounts" key="accountID" columns="accountID,accountKey,balance">
<row accountID="555555555" accountKey="6666" balance="7777777777.23"/>
</rowset>
</result>
<cachedUntil>2012-07-28</cachedUntil>
</api>
I am trying to get my php script to fetch the value of the attribute balance.
This is the code that I have put together so far:
<?php
$apiurl = "api.some-arbitrary-api-site.com;
$xml = simplexml_load_file($apiurl);
print_r($xml);
$balance = $xml->balance;
print_r($balance);
?>
This returns:
SimpleXMLElement Object ( )
Also following some other web tutorials I have tried this change
$balance = $xml->row->attributes()->balance;
print_r($balance);
Which spits out
Warning: main() [function.main]: Node no longer exists in C:\xampp\htdocs\EVE\progress\import.php on line 22
Warning: main() [function.main]: Node no longer exists in C:\xampp\htdocs\EVE\progress\import.php on line 22
What am I doing wrong? The end result is for the page to load this API and fetch the balance once every two days and store the data to be used in a chart rendered using highchart.
Any help will be appreciated!
Thanks
Musa answered the question
I don't know simplexml but shouldn't it be
$xml->result->rowset->row->attributes()->balance – Musa
I was being an idiot and didnt check the xml over again when typing in the path.
Thanks
I have a PHP client that has to query datasets from a service that uses .NET. So, I was trying to make a query by passing a POST field called 'query'. Inside this variable, I have a string version of XML document. What's happening now is that whenever I passed this 'query' to the server, it will throw an exception at me 'Data at the root level is invalid.' and point to the last line of my XML document.
Any tips?
Here's the XML
<?xml version="1.0" encoding="utf-8"?>
<Predicate>
<ComparisonPredicate name="like" isEnabled="True" id="" DistinctIndividualQueries="False">
<FacetOperand reference="TriggeredAlerts.Alert.Name"/>
<Comparison operation="like"/>
<ValueOperand type="System.String" value="Sample"/>
</ComparisonPredicate>
</Predicate>
Here's the var_dump()
string(335) "<?xml version="1.0" encoding="utf-8"?>
<Predicate>
<ComparisonPredicate name="like" isEnabled="True" id="" DistinctIndividualQueries="False">
<FacetOperand reference="TriggeredAlerts.Alert.Name"/>
<Comparison operation="like"/>
<ValueOperand type="System.String" value="TestAlert"/>
</ComparisonPredicate>
</Predicate>
"
I just figured it out and the solution was to use fsocksopen instead of curl. I do not have an accurate answer for that but when I was using curl, it added some extra stuffs to my content; my content length was significantly longer than it is actually supposed to be.
Basically I am trying to add Google Checkout order processing (level 2 integration) into a CakePHP app using the Google Checkout PHP sample code.
I can successfully create carts and receive notifications sent from Google to my app, however it cannot acknowledge these properly.
The function in google's code to do this echo's this:
<?xml version="1.0" encoding="UTF-8" ?><notification-acknowledgment xmlns="http://checkout.google.com/schema/2" serial-number="119963104284921-00001-7" />
However Google Checkout receives that code like this:
<?xml version=1.0 encoding=UTF-8 ?><notification-acknowledgment xmlns="http://checkout.google.com/schema/2" serial-number=119963104284921-00001-7 />
I can't work out what is causing this, I don't think it has anything to do with Cake and I've disabled PHP short tags so I can use inline XML but this makes no difference.
If I remove the first < from the string, the function echo's the rest of the code correctly, leave it in and it brakes (obviously this also applies for the ? and closing > symbol too, I just cant't have all of them at once!).
I have been able to replicate this myself in attempt to remove any unseen stuff being done in the Google Code using the function below. It does exactly the same thing but is contained within a (Cake) controller and hard codes the variables.
public function blank($tags = null) {
$schema = 'http://checkout.google.com/schema/2';
$serial = '119963104284921-00001-7';
if ($tags != null) {
$acknowledgment = '<?xml version="1.0" encoding="UTF-8" ?'.'>'.'<notification-acknowledgment xmlns="'.$schema.' '.'serial-number="'.$serial.'" />';
} else {
$acknowledgment = '?xml version="1.0" encoding="UTF-8" ?>'.'<notification-acknowledgment xmlns="'.$schema.' '.'serial-number="'.$serial.'" />';
}
$this->set('_ack', $acknowledgment);
}
The IF statement is used to show the difference the < symbol makes.
Calling it with nothing ($tags == null) gives this output:
<?xml version="1.0" encoding="UTF-8" ?><notification-acknowledgment xmlns="http://checkout.google.com/schema/2" serial-number="119963104284921-00001-7" />
Source.
Calling it with any other value ($tags != null) gives this output:
<?xml version=1.0 encoding=UTF-8 ?><notification-acknowledgment xmlns="http://checkout.google.com/schema/2" serial-number=119963104284921-00001-7 />
Source.
My question (finally!) is, why does this happen? and how can I get it output the XML correctly?
Sorry if I've missed something really obvious, but I'd rather have it pointed out here than faff around getting nowhere for another day!
this "problem" has to do with the PHP interpreter.
There are solutions but everything in View not in the Controlled.
//var
$questionmark = "?";
echo "<".$questionmark."xml version=\"1.0\" encoding=\"utf-8\"".$questionmark.">";
// separate
echo '<' . '?xml version="1.0" encoding="utf-8"?' . '>';
You forgot to close the quotes on the xmlns attribute, that's the probable cause. Try this:
$acknowledgment = '<?xml version="1.0" encoding="UTF-8" ?'.'>'.'<notification-acknowledgment xmlns="'.$schema.'" '.'serial-number="'.$serial.'" />';
Some XML parsers do not allow specifying the encoding in capital letters (e.g. UTF-8 is incorrect).
Try changing the encoding to "utf-8" to see if that fixes it.
I need to parse all data from below XML file. Please guide me how can I do it? How can I put data into a separate variables or array. Like, the value of <p:productid> tag into $pid and value of <p1:devices> into $pdevice?
Here is a sample file:
<?xml version="1.0" encoding="UTF-8"?>
<p:application>
<p1:devices>
<p1:device>First device</p1:device>
<p1:device>Second Device</p1:device>
</p1:devices>
</p:versions>
</p:application>
You can use the SimpleXML for that.
Here is a good tutorial to get you started:
Introduction To SimpleXML With PHP