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.
Related
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>";
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 had some trouble wording the question, but I think I can explain it better once I show the code:
This is my xml file (test.xml)
<?xml version="1.0" encoding="utf-8"?>
<root>
<entry id="1">
<post>05/12/2014 12:00:00</post>
<page>1</page>
<part>1</part>
<body>BODY TEXT 1</body>
</entry>
<entry id="2">
<post>05/14/2014 12:00:00</post>
<part>1</part>
<page>2</page>
<body>BODY TEXT 2</body>
</entry>
</root>
This is my PHP code (call.php)
<?php
if(isset($_GET['p']))
{
$p=$_GET['p'];
echo $p . "<br>";
$xml=simplexml_load_file("test.xml");
$day=$xml->entry[$p]->post;//<-----------PROBLEM AREA
$post=strtotime("$day");
echo $post . "<br>";
echo time() . "<br>";
if(time() >= $post)
{
echo $xml->entry[$_GET['p']]->page . "<br>";
echo $xml->entry[$_GET['p']]->part . "<br>";
}
}else{
echo "<p>Main Page Stuff</p>";
}
?>
The problem I am having is with the $day variable. If I replace [$p] with [1] or [0], it runs perfectly, but I need to have it called with a variable so I can change what part of the XML file is loaded depending on the query string, currently '?p=1'.
The echo $p results prints out 1 or 0 depending on what I put into the URL, so the $_GET is working properly, but if I put either $p or $_GET into entry[] it gives the error
Notice: Trying to get property of non-object in C:\wamp\www\xmltest\call.php on line 20
(line 20 is the one marked 'PROBLEM AREA')
is there any way to fix this problem so I can call either the first or the second depending on the query string?
I could not find a way to make the solution I was hoping for work, so I worked around it by making a new xml file for each entry instead of having each entry in one xml file.
Code used to load the xml file:
p="docs/".$_GET['p'].".xml";
if(file_exists($p))
{
$xml=simplexml_load_file($p);
$day=$xml->entry->post;
$value = $xml->entry->body;
echo "Part ".$xml->entry->part;
echo "Page ".$xml->entry->page;
}
i today tried to export a big xml file with php to add the content later to a mysql database.
i got in contact with PHP SimpleXML today and it worked great for just one big plot of a xml tag, but when i add more like:
<features>
<name>Holy moly</name>
....
</features>
<features>
<name>what the...</name>
...
</features>
my script cant handle more than one big xml "all over" tag.
heres my php parse script:
<?php
include 'example.php';//heres my xml content
$features = new SimpleXMLElement($xmlstr);
/* For each <character> node, we echo a separate <name>. */
foreach ($features->properties as $properties) {
echo "<br />".$properties->name, ' played by ', $properties->website, PHP_EOL;
}
?>
thank you for your help
A well-formed XML document should have only one root element, encapsulating all other elements. So, I would suggest that you encapsulate the features element with a higher root element.
Solution:
foreach ($root->features as $features) {
foreach ($features->properties as $properties) {
echo "<br />".$properties->name, ' played by ', $properties->website, PHP_EOL;
} }
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