simplexml parsing with php [duplicate] - php

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
I am trying to print out the xml child "pizza" on a php page. Though it is not working, here is my code for parsing the xml file.
<?
$dom = simplexml_load_file("menu.xml");
foreach ($dom->menu->categories->pizzas->pizza as $pizza)
{
echo $pizza;
}
?>
This is the xml file
<menu>
<categories>
<pizzas>
<pizza>Cheese Pizza</pizza>
<pizza>Beef PIzza</pizza>
<pizza>Chickens Wings Pizza</pizza>
</pizzas>
</categories>
</menu>
I would just like to print out the different kinds of pizzas on my php page. I would like to display a menu on the page.
I get an error of "Trying to get property of non-object in /Applications/XAMPP/xamppfiles/htdocs/home.php on line 15"
Please Help!
Thanks

Remove the top level element menu from that loop and use
foreach ($dom->categories->pizzas->pizza as $pizza)
{
echo $pizza;
}
If you do print_r($dom); you will get a proof of that structure.

<?php
$dom = simplexml_load_file("menu.xml");
foreach($dom->categories->pizzas->pizza as $pizza) {
echo $pizza;
} // Try This
?>

Related

PHP - Remove children with a specific attribute in php [duplicate]

This question already has answers here:
Remove a child with a specific attribute, in SimpleXML for PHP
(18 answers)
xml remove child node in php
(3 answers)
Closed 7 years ago.
The goal is to delete every XML child with the same parent that matches the id.
The problem I've encountered was that only the first XML child was removed. When I ran this file the output was "The id is: 1" and also "CarlCarlok". The script removed the first XML child with the attribute "1". Which also was proven when I checked the XML file afterwards where the comment with id "9" was removed. So..To resolve this issue i have to know:
How do I remove every XML child with the same parent?
Because now it only removes the first one it encounters.
<?php
require '../functions.php';
$id = $_GET['id'];
echo "The id is: " . $id . "\n";
$comment_xml_path = '../data/comments.xml';
$comments_xml_file = simplexml_load_file($comment_xml_path);
foreach ($comments_xml_file->comment as $c) {
echo $c->author;
if (strcmp($id, $c['parent']) == 0) {
$dom = dom_import_simplexml($c);
$dom->parentNode->removeChild($dom);
echo "ok";
}
save_xml($comment_xml_path, $comments_xml_file);
(save_xml() is a function for saving utilizing asXML that we made during class)
The XML file
<?xml version="1.0" encoding="UTF-8"?>
<comments>
<comment id="6" parent="3" date="20150317224217">
<author>Carl</author>
<cmt>3</cmt>
</comment>
<comment id="9" parent="1" date="20150312225112">
<author>Carl</author>
<cmt>dsa</cmt>
</comment>
<comment id="10" parent="1" date="20150356225256">
<author>Carl</author>
<cmt>2</cmt>
</comment>
<comment id="11" parent="1" date="20150357225257">
<author>Carl</author>
<cmt>2</cmt>
</comment>
</comments>
Here's a way with SimpleXML and xpath(), which is like SQL for XML:
$id = 1; // parent
$xml = simplexml_load_file("some/path/to/an/xml/file.xml");
$comments = $xml->xpath("/comments/comment[#parent='$id']");
foreach ($comments as $comment) unset($comment[0]);
echo $xml->asXML(); // show result
Comments:
line 3: the xpath-statement will select all <comment> where attribute parent = $id. The # in the statement means attribute. The condition is wrapped in []. The result is stored in $comments as an array of SimpleXML elements.
line 4: deleting each <comment> in the array.
If you want to remove all the children, I would instead loop over the childNodes and call removeChild().
Updated Code:
if (strcmp($id, $c['parent'])== 0) {
foreach ($c->childNodes as $child) {
$child->parentNode->removeChild($child);
}
}
This assumes you are using DOMDocument. However, you seem to be mixing this and SimpleXML.

php simplxml - Trying to get property of non-object in [duplicate]

This question already has an answer here:
Trying to get property of non-object SimpleXML?
(1 answer)
Closed 8 years ago.
So I'm getting an error trying to access player name in an XML File using the code. I'm not sure if i'm accessing the information right is there something I'm doing wrong?
<?php
$xml = simplexml_load_file('trueskill.bcn');
$playername = $xml->SK92->Players[0]->Name;
echo $playername;
?>
Using the XML File
<?xml version="1.0" encoding="utf-8"?>
<SK92>
<Settings Multiplier="200" Decay="0" />
<Players>
<Player Name="Mark" Team="" Invisible="false" Characters="" Alts="JFG;Mark;SEBA" />
<Player Name="Antonio" Team="" Invisible="false" Characters="" Alts="Proxy;Toni" />
</Players>
</SK92>
echo $xml->Players->Player['Name'];
should do the trick.
To have it in variables:
$playername = $xml->Players->Player['Name'];
echo $playername;
SIDENOTE:
As I suppose you want to access all the players, you can do this by:
foreach ($xml->Players->Player as $play) {
echo $play['Name'] . "<br>\n";
}

parsing xml file in php [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 9 years ago.
I have an xml file which consist of name of the country and its code.
<country>
<name>ALBANIA</name>
<code>AL</code>
</country>
<country>
<name>ALGERIA</name>
<code>DZ</code>
</country>
<country>
<name>AMERICAN SAMOA</name>
<code>AS</code>
</country>
now I am using following php code to store them in array and printing them(country.xml file is in the same folder as this php code.
$countries = array();
$file = new SimpleXMLElement(__DIR__ . '/country.xml', null, true);
foreach ($file->country as $country) {
$name = trim($country['name']);
$code = trim(strtoupper($country['code']));
$countries[$code] = $name;
echo $code;
}
but this php code shows blank page. Can anyone guide me where I am making mistake and help me to correct it or give some better method to parse xml file.
The simplexml_load_file() in PHP will do the job.
<?php
$xml = simplexml_load_file('country.xml');
$i=0;
$countryName=array();
$countryCode=array();
foreach($xml as $k=>$v)
{
$countryName[$i] = (string) $xml->country[$i]->name;
$countryCode[$i] = (string) $xml->country[$i]->code;
$i++;
}
print_r($countryName);
print_r($countryCode);
?>

Linked in xml response to php variables [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 9 years ago.
i am getting this result from my linked in connect script,
<person>
<email-address>xzenia1#gmail.com</email-address>
<picture-url>http://m3.licdn.com/mpr/mprx/0_UiHHf6SiF4yuBerHUkfUfkshFpomUIrHMbpBf5Iy4sOYk7FecL4XTLxtdAEl42AXsho9hGzDtRBl</picture-url>
</person>
this is the php call
$xml_response = $linkedin->getProfile("~:(email-address,picture-url)");
how to make them assign to separate PHP variable.
You can load your xml as string with simplexml_load_string and then loop in it to get all data
$xml = simplexml_load_string($xml_response);
foreach($xml as $key => $val)
{
echo "$key=>$val<br>" . "\n";
}
This will output
email-address=>xzenia1#gmail.com
picture-url=>http://m3.licdn.com/mpr/mprx/0_UiHHf6SiF4yuBerHUkfUfkshFpomUIrHMbpBf5Iy4sOYk7FecL4XTLxtdAEl42AXsho9hGzDtRBl
Live sample
Try,
$xml = (array)simplexml_load_string($xml_response);
echo $email=$xml['email-address'];
echo $picture=$xml['picture-url'];
$xml = simplexml_load_string($linkedin->getProfile("~:(email-address,picture-url)"));
echo $xml->{'email-address'}[0] . "<br />";
echo $xml->{'picture-url'}[0];
simplexmldoesn't like - in node names, therefore use $xml->{'email-address'} instead of $xml->email-address.
use index [0] on both nodes, just in case, if one day your simplexml object would contain more than one <person> node...
see it working: http://codepad.viper-7.com/dQQ6sa

simpleXML get node child based on attribute [duplicate]

This question already has answers here:
SimpleXML: Selecting Elements Which Have A Certain Attribute Value
(2 answers)
Implementing condition in XPath [duplicate]
(2 answers)
Closed 9 years ago.
I am trying to parse out the value of a node I am referencing by one of its attributes. but I am not sure of the syntax
XML:
<data>
<poster name="E-Verify" id="everify">
<full_image url="e-verify-swa-poster.jpg"/>
<full_other url=""/>
</poster>
<poster name="Minimum Wage" id="minwage">
<full_image url="minwage.jpg"/>
<full_other url="spa_minwage.jpg"/>
</poster>
</data>
here is where I want to get the url value of full_image where poster id equal to minwage:
$xml = simplexml_load_file('PosterData.xml');
$main_url = $xml->full_image[name] where poster[id] = "minwage";
//something like that.
echo $main_url;
Result: minwage.jpg
If anyone has any resources that cover this stuff please share.
You should be able to use SimpleXMLElement::xpath() to run an xpath query on a simple XML document.
$xml = simplexml_load_file('PosterData.xml');
$main_url = $xml->xpath('name[#id="minwage"]/full_image')[0];
echo $main_url;
Simply loop the poster elements and remember to cast the attribute values to strings, since you want to compare them (and probably output them) as strings:
$xml = simplexml_load_file('PosterData.xml');
foreach ($xml->poster as $poster) {
if ((string) $poster['id'] == 'minwage') {
echo (string) $poster->full_image['url'];
}
}
You can use:
$object = simplexml_load_string('<data><poster name="E-Verify" id="everify"><full_image url="e-verify-swa-poster.jpg"/><full_other url=""/></poster><poster name="Minimum Wage" id="minwage"><full_image url="minwage.jpg"/><full_other url="spa_minwage.jpg"/></poster></data>');
foreach ($object as $value) {
echo "URL: ".$value->full_image->attributes();
echo "<br>";
}
Either use simplexml_load_file('Some external file.xml') if calling external file.

Categories