Good, again I ask your help, I have an xml document (http://inlivefm.6te.net/AirPlayHistory.xml) which provides the name of songs played.
What I'm trying is to remove the information from xml with php echo, I realized a php code but must be wrong, because it gives me nothing so I came to ask your help in solving this problem.
<?php
$xml = simplexml_load_file("http://inlivefm.6te.net/AirPlayHistory.xml");
print $xml->Event->Song['title'];
echo '';
?>
<?php
$xml = simplexml_load_file("http://inlivefm.6te.net/AirPlayHistory.xml");
print $xml->Event->Song->Artist['name'];
echo '';
?>
Someone I can help?
Thank you before too.
simplexml does not see root element. Write it so:
$xml = simplexml_load_file("http://inlivefm.6te.net/AirPlayHistory.xml");
foreach($xml->Song as $item)
echo $item->Artist['name'] . " - " . $item['title'] ."<br>";
Related
I'm struggling with PHP code that would read following XML file:
<putovanja agencija="Kompas Zagreb d.d.">
<putovanje url="http://www.kompas.hr/Packages/Package.aspx?idPackage=3151" tip="Krstarenja Jadranom" destinacija="Krstarenja" naziv="Mini Krstarenje Jadranom Zadar-Opatija (5 noći, jedan smjer) " id="3151" polazak="Zadar (Krstarenje)">
<ukrcaji>
<ukrcaj>Zadar</ukrcaj>
</ukrcaji>
<datumiIcijene>
<data od="28.08.2017" do="02.09.2017" cijena="3695"/>
<data od="04.09.2017" do="09.09.2017" cijena="3360"/>
<data od="11.09.2017" do="16.09.2017" cijena="3360"/>
</datumiIcijene>
</putovanje>
<putovanje url="http://www.kompas.hr/Packages/Package.aspx?idPackage=3151" tip="Odmor" destinacija="Krstarenja" naziv="Mini Krstarenje Jadranom Zadar-Opatija (5 noći, jedan smjer) " id="3151" polazak="Zadar (Krstarenje)">
<ukrcaji>
<ukrcaj>Zadar</ukrcaj>
</ukrcaji>
<datumiIcijene>
<data od="28.08.2017" do="02.09.2017" cijena="3695"/>
<data od="04.09.2017" do="09.09.2017" cijena="3360"/>
</datumiIcijene>
</putovanje>
</putovanja>
I found sample online, more specifically on w3schools(https://www.w3schools.com/php/php_xml_simplexml_get.asp), I understand my XML is more complex, but I can't even get the URL of first "CHILD". I think loop goes trough right child as it write BREAK twice in the output. Does anyone have any clue where I made a mistake?
I'm really sorry if my question is stupid, I'm still learning how to code.
Thanks for all the help and wish you all a nice day :D
oh and my current code:
<?php
$xml=simplexml_load_file("putovanja.xml") or die("Error: Cannot create object");
foreach($xml->children() as $putovanja) {
echo $putovanja->putovanje['url'];
echo "Break <br>";
}
?>
Here is how to access the URLs:
$xml=simplexml_load_file("putovanja.xml") or die("Error: Cannot create object");
foreach($xml->putovanje as $p) {
echo $p->attributes()->url;
echo "\n";
}
You don't need children() and you'll find attributes() useful
To access more elements then here is an example:
<?php
$xml=simplexml_load_file("putovanja.xml") or die("Error: Cannot create object");
foreach($xml->putovanje as $p) {
echo $p->attributes()->url;
echo "\n";
echo $p->ukrcaji->ukrcaj;
echo "\n";
echo $p->datumiIcijene->data[0]->attributes()->od;
echo "\n\n";
}
If you add print_r($p); within the loop then you'll see the data structure and be able to follow my example and access the other elements you need.
Consider an XML file like this :
<title>sometitle</title>
<a>
<abc>content1</abc>
<xyz>content2</sxyz>
<metadata>
<b>
<c>content3</c>
<d><attribute></d>
</b>
</metadata>
</a>
I use this code to parse my file and i get the output such as :
title : abc
a:content1 content2 content 3
i.e it only parses the first level tags and fails to parse subtags and get the value ,any help is much appreciated since I'am a complete newbie in this.So far this is what I have tried:
$xmlDoc = new DOMDocument();
$xmlDoc->load("somedoc.xml");
$x = $xmlDoc->documentElement;
foreach($x->childNodes AS $item)
{
print $item->nodeName . " = " . $item->nodeValue . "<br>";
}
Check the below link for php documentation on haschildnodes() functions.
You can see samples in the below page for usage.
http://php.net/manual/en/domnode.haschildnodes.php
I dont seem to quite understand how xml_parse works. What i was doing was getting the contents of the xml file, create a parser, and pass it into xml_parse(). I dont know what to do after that. I was thinking that in my case, $xml was the array i can now iterate through. I was trying to see how i would parse my data.
$fp = file_get_contents("memberdata.xml");
echo "pre-create";
$xml = xml_parser_create();
echo "pre-parse";
$status = xml_parse($xml,$fp);
if(!$status){
die("Error parsing data from $fp into $xml");
}
echo "pre XML posting";
echo "<br />".$xml."<br />";
print_r($xml);
xml_parser_free($xml);
I cant seem to figure out how to access this.
Sample xml data is as follows:
<currentTime>2012-09-05 03:43:25</currentTime>
<result>
<rowset name="members" key="characterID" columns="characterID,name,startDateTime,baseID,base,title,logonDateTime,logoffDateTime,locationID,location,shipTypeID,shipType,roles,grantableRoles">
<row ..>
</rowset>
</result>
<cachedUntil></cashedUntil>
Instead of using the base tools, using one of the toolkits, SimpleXML will alleviate a lot of the frustrations. The answer to this question is redone using SimpleXML.
$fp = file_get_contents("memberdata.xml");
$eve = new SimpleXMLElement($fp);
$cols = $eve->{'result'}->rowset['columns'];
//I put result in {} because result is a special character in php.
$cols = explode(",",$cols);
foreach ($cols as $c){
echo "".$c."<br />";
}
//output is printed to screen
I have build a simple site map script, i am not able to get URL output in URL field.
My PHP Script.
header("Content-Type: text/xml;charset=iso-8859-1");
echo '<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
';
require_once('_ls-global/php/sr-connect.php');
$db = mysql_select_db($database,$connection) or trigger_error("SQL", E_USER_ERROR);
$sqlquery = mysql_query("SELECT * FROM $tablename ORDER by id")or die (mysql_error());
while ($list = mysql_fetch_assoc($sqlquery)){
$pflink=$list['pflink'];
$pagelink=$list['pagelink'];
$site="http://mysite.com";
$url='$site/$pflink/$pagelink';
$changefreq="weekly";
$priority="1.0";
echo '<url>
<loc>'.$url.'</loc>
<changefreq>'.$changefreq.'</changefreq>
<priority>'.$priority.'</priority>
</url>';
}
echo '</urlset>';
The Output of this script is this.
<url>
<loc>$site/$pflink/$pagelink</loc>
<changefreq>weekly</changefreq>
<priority>1.0</priority>
</url>
If i change $url='$site/$pflink/$pagelink'; to $url="$site/$pflink/$pagelink";
then i get only one value and error "XML Parsing Error: not well-formed".
Please see and suggest any modification to make it work.
Thanks
I guess you are having characters in the vars which are messing up the XML.
For example &, ä, <, >... You need to encode the content correctly.
Try wrapping the output:
At first change $url to $url = $site .'/'. $pflink .'/'. $pagelink; and then update the output of the XML to:
<?php
// ...
echo '<url>
<loc><![CDATA['.$url.']]></loc>
<changefreq>'.$changefreq.'</changefreq>
<priority>'.$priority.'</priority>
</url>';
?>
Explanation to CDATA available at http://en.wikipedia.org/wiki/CDATA
Based on thedom and FrontEndJohn answers and comment I got it right this way.
Changing $url='$site/$pflink/$pagelink'; to $url = $site .'/'. $pflink .'/'. $pagelink;
And modifying.
echo '<url>
<loc>'.$url.'</loc>
<changefreq>'.$changefreq.'</changefreq>
<priority>'.$priority.'</priority>
</url>';
to
echo '<url>';
echo '<loc><![CDATA['.$url.']]></loc>';
echo '<changefreq>'.$changefreq.'</changefreq>';
echo '<priority>'.$priority.'</priority>';
echo '</url>';
Hope this helps others too.
If I understand your problem correctly, you cannot currently get the value of the variable due to the use of ' but when trying to use " so that the variables echo the XML gets upset.
Try:
$url = $site . '/' . $pflink . '/' . $pagelink;
This will give the value of the variables without using ". If I have miss-understood you please let me know.
Edit: Thinking about it, it looks more the the value of one or more of the variables may be what is upsetting the XML, assuming the variables are not giving their values while using '. It could be worth checking the contents of the variables for issues if you have not done so already.
I have a slight problem. I need to parse a file and populate a web banner with the results. Problem is, the file is called : "_banner_products.php" and it's contents are as follows:
<?php header('Content-Type:text/xml'); ?><?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<carouselle>
<firstLayer>
<layerName>Leica Disto X310</layerName>
<layerProduct>Disto X310</layerProduct>
<layerPic>http://www.leicaestonia.ee/extensions/boxes_design/flashpics/1334482548.jpg</layerPic>
<layerPrice>0,-</layerPrice>
<layerPriceOld></layerPriceOld>
<layerLink>http://www.leicaestonia.ee/index.php?id=11627</layerLink>
<layerTimer>01.05.2012 00:00</layerTimer>
</firstLayer>
<firstLayer>
.....
.....
</firstLayer>
</carouselle>
How can I loop through this file to group all the "firstLayer" children into one and so on..
If I just use:
$file = fopen("_banner_products.php", "r");
while (!feof($file)){
$line = fgets($file);
}
simplexml_load_file throws this-
"_banner_products.php:1: parser error : Start tag expected, '<' "
Then I only get the contents of the <...> tags meaning there is no way for me to differentiate if I am out of the scope already.
Thanks for anyone responding. If anything is unclear I´ll try to explain more.
EDIT.
Thank you for the solution, indeed using the full URL worked:
simplexml_load_file("http://localhost/MySite/_banner_products.php");
You are having issue because simplexml_load_file is treating your file like a local xml file .. what you need to do is add the full URL
Example
simplexml_load_file("http://localhost/web/_banner_products.php");
Use Case getting layerName for example
_banner_products.php
<?php
header ( 'Content-Type:text/xml' );
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<carouselle>
<firstLayer>
<layerName>Leica Disto X310</layerName>
<layerProduct>Disto X310</layerProduct>
<layerPic>http://www.leicaestonia.ee/extensions/boxes_design/flashpics/1334482548.jpg</layerPic>
<layerPrice>0,-</layerPrice>
<layerPriceOld></layerPriceOld>
<layerLink>http://www.leicaestonia.ee/index.php?id=11627</layerLink>
<layerTimer>01.05.2012 00:00</layerTimer>
</firstLayer>
<firstLayer>
<layerName>Leica Disto X310</layerName>
<layerProduct>Disto X310</layerProduct>
<layerPic>http://www.leicaestonia.ee/extensions/boxes_design/flashpics/1334482548.jpg</layerPic>
<layerPrice>0,-</layerPrice>
<layerPriceOld></layerPriceOld>
<layerLink>http://www.leicaestonia.ee/index.php?id=11627</layerLink>
<layerTimer>01.05.2012 00:00</layerTimer>
</firstLayer>
</carouselle>
view details
$xml = simplexml_load_file("http://localhost/lab/stockoverflow/_banner_products.php");
echo "<pre>" ;
foreach($xml as $key => $element)
{
echo $element->layerName , PHP_EOL ;
}
The most obvious way to do this is to strip out the first line, and add the XML declaration back in with your code.
You could also parse the file with PHP, using eval(), but be very sure about what you are parsing, as this could be a very large security hole.