Basic simpleXML working example? - php

It looks like there are many problems with simpleXML in PHP. I'm running the latest version of php on Windows and I just can not get the basic examples of simpleXML to work as in the documentation.
My xml file is:
<?xml version="1.0" encoding="ISO-8859-1"?>
<programme>
<title>Billy Bushwaka</title>
<episodeNumber>2</episodeNumber>
<description>Billy Bushwaka entertains</description>
<url>play.swf</url>
</programme>
My PHP program is:
<?php
$xml = simplexml_load_file("local.xml");
$result = $xml->xpath("//programme");
echo "Title: " . $result . "</br>";
?>
All I get is the following:
Title: Array
How can I get "Title: Billy Bushwaka"?
There are no repeats of XML data so I do not want to use arrays.

SimpleXML 101
First of all, always name your PHP variables after the node they represent.
// the root node is <programme/>
$programme = simplexml_load_file("local.xml");
Access to children (nodes) as if they were object properties.
echo $programme->title;
If there are multiple children using the same name, you can specify their 0-based position
// first <title/> child
echo $programme->title[0];
// create or change the value of the second <title/> child
$programme->title[1] = 'Second title';
Access to attributes as if they were array keys
// <mynode attr="attribute value" />
echo $mynode['attr'];
XPath always returns an array.
Back to your case, the best way to access that <title /> node would be
$programme = simplexml_load_file("local.xml");
echo "Title: " . $programme->title;

First of all, simplexml xpath method always returns an array of matches. Even if there is only 1 match (or even 0, in which case result is an empty array). This is why you get "Array" in the output.
Secondly, if you want just the title, then you need to change your xpath query:
$result = $xml->xpath("//programme/title");
echo "Title: " . $result[0] . "</br>";

You should probably change the xpath to //programme/title and then echo $result[0] or leave the xpath as it is and echo $result[0]->title. Remember var_dump will always help you.

I think you want:
$result = $xml->xpath("/programme/title");
echo "Title: " . $result[0] . "</br>";

$xml = simplexml_load_file("local.xml");
echo $xml->programme->title;
......
echo $xml->programme->description;

Related

How to read xml file from url with symfony [duplicate]

It looks like there are many problems with simpleXML in PHP. I'm running the latest version of php on Windows and I just can not get the basic examples of simpleXML to work as in the documentation.
My xml file is:
<?xml version="1.0" encoding="ISO-8859-1"?>
<programme>
<title>Billy Bushwaka</title>
<episodeNumber>2</episodeNumber>
<description>Billy Bushwaka entertains</description>
<url>play.swf</url>
</programme>
My PHP program is:
<?php
$xml = simplexml_load_file("local.xml");
$result = $xml->xpath("//programme");
echo "Title: " . $result . "</br>";
?>
All I get is the following:
Title: Array
How can I get "Title: Billy Bushwaka"?
There are no repeats of XML data so I do not want to use arrays.
SimpleXML 101
First of all, always name your PHP variables after the node they represent.
// the root node is <programme/>
$programme = simplexml_load_file("local.xml");
Access to children (nodes) as if they were object properties.
echo $programme->title;
If there are multiple children using the same name, you can specify their 0-based position
// first <title/> child
echo $programme->title[0];
// create or change the value of the second <title/> child
$programme->title[1] = 'Second title';
Access to attributes as if they were array keys
// <mynode attr="attribute value" />
echo $mynode['attr'];
XPath always returns an array.
Back to your case, the best way to access that <title /> node would be
$programme = simplexml_load_file("local.xml");
echo "Title: " . $programme->title;
First of all, simplexml xpath method always returns an array of matches. Even if there is only 1 match (or even 0, in which case result is an empty array). This is why you get "Array" in the output.
Secondly, if you want just the title, then you need to change your xpath query:
$result = $xml->xpath("//programme/title");
echo "Title: " . $result[0] . "</br>";
You should probably change the xpath to //programme/title and then echo $result[0] or leave the xpath as it is and echo $result[0]->title. Remember var_dump will always help you.
I think you want:
$result = $xml->xpath("/programme/title");
echo "Title: " . $result[0] . "</br>";
$xml = simplexml_load_file("local.xml");
echo $xml->programme->title;
......
echo $xml->programme->description;

Setting Parent Node Variable in SimpleXML and XPath

I am working with PHP and SimpleXML/XPath, and I'm just wondering how to set a certain parent (with a certain attribute value) equal to a variable, which I could use in a 'foreach'?
I'm currently getting this error:
Notice: Array to string conversion
and this output
Array
Thanks for any leads.
Here is the php code:
<?php
$url = "test_b.xml";
$xml = simplexml_load_file($url);
$xml_report_abbrev_b = $xml->xpath('//poster[#name="U-Verify"]')[0];
if($xml_report_abbrev_b){
foreach($xml_report_abbrev_b as $node_a) {
echo '<h1>'.$node_a->xpath('/full_image/#url').'</h1>';
}
} else {
echo 'XPath query failed';
}
?>
Here's the xml:
<data>
<poster name="U-Verify" id="uverify">
<full_image url="u-verify.jpg"/>
<full_other url=""/>
</poster>
<poster name="Minimum" id="min">
<full_image url="min.jpg"/>
<full_other url="spa_min.jpg"/>
</poster>
</data>
Using SimpleXML's element access and attribute access directly instead of using the XPath query will make the code simpler and perform better.
Your code could be reduced to...
$xml_report_abbrev_b = $xml->xpath('//poster[#name="U-Verify"]');
if($xml_report_abbrev_b){
echo '<h1>'.$xml_report_abbrev_b[0]->full_image['url'].'</h1>';
} else {
echo 'XPath query failed';
}
Note the way the echo line says - with the <poster> element you found from the XPath expression, use the <full_image> element and fetch the url attribute.
I also moved the [0] into the if because if the XPath didn't find a value, this produced an error as there isn't any data to get a value from.
This outputs...
<h1>u-verify.jpg</h1>

Retrieve XML element data with PHP

i'm trying to allow the user to retrieve a specific XML node and print the information. My XML data is as follows:
<people>
<person id="1">
<forename>Jim</forename>
<surname>Morrison</surname>
</person>
<person id="2">
<forename>James</forename>
<surname>Frank</surname>
</person>
</people>
I want to be able to search my XML document with a name or ID, for instance I want to be able to say, 'check that james exists, and if james does, print out his information, otherwise print out an error message'.
I've figured that I first need to include the file with:
$xml=simplexml_load_file("credentials.xml") or die("Error: Cannot create object");
From this point onwards i'm unsure how to proceed, i've looked at SimpleXML and XPATH but i'm unsure on how to use these to acheive this. Thanks if you can help
Let's xpath():
$xml = simplexml_load_string($x); // assume XML in $x
$result = $xml->xpath("/people/person[forename = 'James']")[0];
The above xpath-expression will select any <person> having 'James' as a <forename> and store it as an array in $result.
With the [0] at the end of line 2, we select only the first entry in that array and store it in $result. This requires PHP >= 5.4.
We could also write to get the same result:
$result = $xml->xpath("/people/person[forename = 'James']");
$result = $result[0];
If there were more than 1 James in the XML, we would only get the first.
To get all Jameses, do as in line 1 above.
Then, let's output the <person> selected by our xpath expression:
echo $result->forename . ' ' . $result->surname .' has id ' . $result['id'] . ".";
In case $result contains several Jameses, do:
foreach ($result as $person) {
echo $person->forename . ' ' . $person->surname .' has id ' . $person['id'] . "." . PHP_EOL;
}
see it working: https://eval.in/222195
You should use the manual.
simplexml_load_file returns a SimpleXMLElement object.
From here you can use the objects children method to get the children you want, which would also be more of that SimpleXMLElement objects. Let's say you first want the children called people, and then want person. When you got the person objects, you can get the value of the attribute by the method attributes to get the names and the values etc. Because SimpleXMLElement implements Traversable, you can use a foreach loop to loop through the lists/arrays you get in return.
Figured out how to mostly solve the problem with this:
$xml=simplexml_load_file("credentials.xml") or die("Error: Cannot create object");
foreach($xml->children() as $people){
if ($people->forename == "james" ){echo $people->forename;}
}
This may be inefficient so if anyone else has a better way please do specify :)

I'm confused about parsing XML in PHP

How can I parse XML data using PHP? I want to get the contents of just the element named
<profile xmlns="mysite.com">
<confirmationNumber>128686132</confirmationNumber>
<merchantRefNum>123456</merchantRefNum>
<customerEmail>test#test.com</customerEmail>
</profile>
I was trying to use this code
$sxe = new SimpleXMLElement($decodedMessage);
echo $sxe->getName() . "\n";
foreach ($sxe->children() as $child)
{
echo $child->getName() . "\n";
}
But I don't know how to select a specific element.
You don't need the traversal methods of SimpleXML. Just use simplexml_load_string() if you already have it in one:
$xml = simplexml_load_string($decodedMessage);
Then accessing the content is as simple as:
print $xml->customerEmail;
You can try the function simplexml_load_file(), and use the XML as object. Or convert to array with cast.
You should look at xpath.
One of the easiest ways to deal with it is look for a function on google called xml2array, since associative arrays are much easier to deal with in PHP than XML.
you can try this.
$xml = simplexml_load_file($filePath) or die ("Unable to load XML file!");
//Access XML Data
echo "confirmation Number: " . $xml->confirmationNumber;
echo "merchant Ref Num: " . $xml->confirmationNumber;
echo "customer Email: " . $xml->customerEmail;
If you want to find that one element within a larger document, you could use XPath.
For example, for the XML
<some>
<hierarchy>
<profile>
<etc>et cetera</etc>
</profile>
</hierarchy>
</some>
you can simply do $sxe->xpath('//profile'), which will return an array of all the profile elements everywhere in the document (as SXEs, which you can then process). If there is only one profile element, then you're done, but if there are many you will need a more specific XPath.

Extracting Certain XML Elements with PHP SimpleXML

I am having some problems parsing this piece of XML using SimpleXML. There is always only one Series element, and a variable number of Episode elements beneath. I want to parse XML so I can store the Series data in one table, and all the Episode data in another table.
XML:
<Data>
<Series>
<id>80348</id>
<Genre>|Action and Adventure|Comedy|Drama|</Genre>
<IMDB_ID>tt0934814</IMDB_ID>
<SeriesID>68724</SeriesID>
<SeriesName>Chuck</SeriesName>
<banner>graphical/80348-g.jpg</banner>
</Series>
<Episode>
<id>935481</id>
<Director>Robert Duncan McNeill</Director>
<EpisodeName>Chuck Versus the Third Dimension 2D</EpisodeName>
<EpisodeNumber>1</EpisodeNumber>
<seasonid>27984</seasonid>
<seriesid>80348</seriesid>
</Episode>
<Episode>
<id>935483</id>
<Director>Robert Duncan McNeill</Director>
<EpisodeName>Buy More #15: Employee Health</EpisodeName>
<EpisodeNumber>2</EpisodeNumber>
<seasonid>27984</seasonid>
<seriesid>80348</seriesid>
</Episode>
</Data>
When I attempt to access just the first Series element and child nodes, or iterate through the Episode elements only it does not work. I have also tried to use DOMDocument with SimpleXML, but could not get that to work at all.
PHP Code:
<?php
if(file_exists('en.xml'))
{
$data = simplexml_load_file('en.xml');
foreach($data as $series)
{
echo 'id: <br />' . $series->id;
echo 'imdb: <br />' . $series->IMDB_ID;
}
}
?>
Output:
id:80348
imdb:tt0934814
id:935481
imdb:
id:1534641
imdb:
I just discovered the stupid mistake I was making.
The PHP code should have been like this:
<?php
if(file_exists('en.xml'))
{
$data = simplexml_load_file('en.xml');
foreach($data->Series as $series)
{
echo 'id: <br />' . $series->id;
echo 'imdb: <br />' . $series->IMDB_ID;
}
}
?>
The first time I tried this (and it did not work) I was using;
$data->series as $series
So it was a capital 'S' that was the issue.

Categories