I have the following xml document:
<?xml version="1.0"?>
<root>
<offers count="3009">
<offer>
<offerId>25</offerId>
<offer_type>for sale</offer_type>
<offer_status>available</offer_status>
<building_floors></building_floors>
<title>title</title>
<photos count="4">
<photo zIndex="1">1.jpg</photo>
<photo zIndex="2">2.jpg</photo>
<photo zIndex="3">3.jpg</photo>
<photo zIndex="4">4.jpg</photo>
</photos>
</offer>
</offers>
</root>
I am using this php code to get results from the xml:
$xml = simplexml_load_file("offers.xml")
or die("Error: Cannot create object");
foreach($xml->children() as $offers){
foreach($offers->children() as $offer => $data){
echo $data->offerId;echo "<br />";//and so on for each element
foreach ($xml->offers->offer->photos->photo as $aaa){
$photos=$aaa; echo "<br />";
//echo $xml;
$photo = explode("<br /> ", $photos);
foreach($photo as $value) echo $value; echo '<br />';//echo $value
}
}
}
When I try to loop the photo element I get all photos but only for the first offer.
My question is what I am doing wrong and why I do not get photos for each offer?
The problem is with the way you retrieve the photo elements. You start from the root element so it always give you the same node.
You should use $data like this :
foreach ($data->photos->photo as $aaa){
cleaning a bit your code you should use this to display the photo elements:
$xml = simplexml_load_file("offers.xml") or die("Error: Cannot create object");
foreach($xml->children() as $offers){
foreach($offers->children() as $offer){
echo "Offer id : " . $offer->offerId . "<br>";
foreach ($offer->photos->photo as $photo){
$val = (string) $photo;
echo $val . "<br>";
}
}
}
Related
Trying to parse the following xml:
<?xml version="1.0"?>
<devices>
<device name="TEMP" index="0" available="1" type="" id="xxx">
<field type="2" max="50" min="-20" niceName="Temperature (C)" value="24.75" key="TempC"/>
<field type="2" max="122" min="-4" niceName="Temperature (F)" value="76.55" key="TempF"/>
and pull the TempF value of 76.55.
This is the code I was trying, but I'm not doing something isn't right:
$xml = simplexml_load_file('c:\data.xml') or die("Error: Cannot create object");
echo '<h2>Server Temperature</h2>';
$list = $xml->devices;
for ($i = 0; $i < count($list); $i++) {
echo 'Temp: ' .$list[$i]->TempF[value] . '<br>';
You can loop $devices->device instead without instead of using a for loop
echo '<h2>Server Temperature</h2>';
foreach ($xml->device as $device) {
foreach ($device->field as $field) {
$att = $field->attributes();
if ((string)$att->key === "TempF") {
echo 'Temp: ' . $field->attributes()->value . "<br>";
}
}
}
Output
<h2>Server Temperature</h2>Temp: 76.55<br>
See a Php demo
Or if you want to loop all the values of field, you can use xpath:
echo '<h2>Server Temperature</h2>';
foreach ($xml->xpath('/devices/device/field[#key="TempF"]/#value') as $value) {
echo 'Temp: ' . $value . "<br>";
}
Output
<h2>Server Temperature</h2>Temp: 76.55<br>
See another Php demo
I am having a hard time trying to understand why I can't compare the values of two arrays in PHP. If I echo both of these during the loop using "echo $description->ItemDesriptionName;" and "echo $item->ItemName;" the values seem to show as the same, but when I try to compare them using if, nothing works. What am I missing?
<?php
$xml=simplexml_load_file("test.xml") or die("Error: Cannot create object");
$categories = $xml->Menu->Categories;
$items = $xml->Menu->Categories->Items->ItemObject;
$itemdescription = $xml->Menu->Options->Description->DescriptionObject;
foreach($items as $item) {
echo $item->ItemName . ' - ' . $item->Price . '</br>';
foreach ($itemdescription as $description) {
if ($description->ItemDescriptionName == $item->ItemName) {
echo 'We have a match!';
//where I would echo $description->ItemDescription;
}
}
}
?>
Here is the XML file
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Menu>
<Categories>
<Name>Category 1</Name>
<Items>
<ItemObject>
<ItemName>Item 1</ItemName>
<Price>1</Price>
</ItemObject>
<ItemObject>
<ItemName>Item 2</ItemName>
<Price>3</Price>
</ItemObject>
</Items>
</Categories>
<Options>
<Description>
<DescriptionObject>
<ItemDescriptionName>Item 1</ItemDescriptionName>
<ItemDescription>A Great item</ItemDescription>
</DescriptionObject>
<DescriptionObject>
<ItemDescriptionName>Item 2</ItemDescriptionName>
<ItemDescription>A Great item as well</ItemDescription>
</DescriptionObject>
</Description>
</Options>
</Menu>
</Root>
compare as string
and you have typo in ItemDescriptioName (ItemDescriptionName)
if ( (string)$description->ItemDescriptionName == (string)$item->ItemName) {
Convert to string and then compare
<?php
$xml=simplexml_load_file("test.xml") or die("Error: Cannot create object");
$menu = $xml->Menu;
$categories = $xml->Menu->Categories;
$items = $xml->Menu->Categories->Items->ItemObject;
$itemdescription = $xml->Menu->Options->Description->DescriptionObject;
foreach($items as $item) {
$itemname = $item->ItemName;
foreach ($itemdescription as $description) {
$descriptionname = $description->ItemDescriptionName ;
echo $itemname." ---- ".$descriptionname."<br/>";
if((string)$itemname === (string)$descriptionname){
echo "Yes its matched";
}
}
}
?>
Working fine for me
The properties like $description->ItemDescriptionName are SimpleXMLElement objects. So you do not compare strings but two objects.
SimpleXMLElement objects implement the magic method __toString(). They can be cast to string automatically, but a compare between to objects will not trigger that. You can force it:
if ((string)$description->ItemDescriptionName === (string)$item->ItemName) {
...
Can you access them directly instead using an accordant index?
...
$items = $xml->Menu->Categories->Items->ItemObject;
$itemdescription = $xml->Menu->Options->Description;
$i = 0;
foreach ($items as $item) {
echo $i.' '.$item->ItemName . ' - ' . $item->Price;
echo $itemdescription->DescriptionObject[$i]->ItemDescriptionName[0];
echo ' ';
echo $itemdescription->DescriptionObject[$i]->ItemDescription[0];
echo '</br>';
$i++;
}
I am trying to read multiple xml file from a folder and want to get tag attribute for further processing.
So far i have tried many available examples but no luck so far.
below is my code, any help will be appreciated..! Thanks..!
foreach (glob("unzip_temp/other/newfolder/*.xml") as $filename) {
$xml_file = file_get_contents($filename, FILE_TEXT);
$xml = simplexml_load_file($xml_file);
foreach ($xml->tag1 as $fileinfo) {
$name = $fileinfo['Name'];
$language = $fileinfo['Language'];
$creator = $fileinfo['Creator'];
echo "<li> $name ,$language ,$creator";
}
below is the part of xml file that i want to read:
<?xml version="1.0" encoding="utf-16"?>
<tag1 xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Name="Bethanien_Befunder_CTKolon" Language="other" Type="Simple" Creator="ADMINISTRATOR" IsMultiSelect="false" IsFloatingPickList="false" IsPhraseGenerated="false">
I got the solution: Thanks anyways
$doc = new DOMDocument();
foreach (glob("unzip_temp/other/newfolder/*.xml") as $filename) {
$doc->load($filename);
$intros = $doc->getElementsByTagName("tag1");
foreach ($intros as $intro) {
echo "<br />";
$name = $intro->getAttribute('Name');
echo $name;
echo "<br />";
echo $intro->getAttribute('Creator');
echo "<br />";
echo $intro->getAttribute('Language');
echo "<hr />";
}
}
here is my xml file note.xml file
<?xml version="1.0" encoding="ISO-8859-1"?>
<agents>
<agent>
<id>1</id>
<image> img/primary-nav-logo.png</image>
<name>Tommy Jenkin</name>
<company>CJenkins Insurance</company>
<street>Insurance150 S State Stree</street>
<city>Linkend</city>
<phone>(773) 561-4331</phone>
</agent>
<agent>
<id>2</id>
<image> img/primary-nav-logo.png</image>
<name>Tommy Jenkin</name>
<company>CJenkins Insurance</company>
<street>Insurance150 S State Stree</street>
<city>Linkend</city>
<phone>(773) 561-4331</phone>
</agent>
</agents>
and i have to print xml record of id 1 and i have write code in php like this
<?php
$xml = simplexml_load_file("note.xml") or die("Error: Cannot create object");
foreach($xml->xpath('//agent') as $item) {
$row = simplexml_load_string($item->asXML());
$v = $row->xpath('//id[. ="1"]');
if($v[0]){
print $item->id;
print $item->image;
print $item->name;
print $item->company;
print $item->street;
print $item->city;
print $item->phone;
}
else{
echo 'No records';
}
?>
please suggest me where i am wrong
You do not need to call $item->asXML() since $item is already a SimpleXML object. And you don't have to loop over your array since you can query the necessary agent directly. Try this:
<?php
$xml = simplexml_load_file("note.xml") or die("Error: Cannot create object");
$agent = $xml->xpath('//agent[id=1]');
if (!empty($agent)) {
$item = $agent[0];
print $item->id;
print $item->image;
print $item->name;
print $item->company;
print $item->street;
print $item->city;
print $item->phone;
} else {
echo 'No records';
}
First of all, you are missing the closing bracket of foreach loop.
Second, you can do this with xPath itself.
Look at this code:
<pre><?php
$xml = simplexml_load_file("note.xml") or die("Error: Cannot create object");
$item = $xml->xpath('//agents/agent[id=1]')[0];
if($item!=null){
print_r($item);
}else{
echo "No Records";
}
?></pre>
I am new to PHP so maybe this is silly question.
How can I easily manipulate with xml document using PHP and get data from it?
Can someone show me some example how it is done?
I tried this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
And this is my php:
<?php
$xml = simplexml_load_file("test.xml");
echo $xml->getName() . "<br />";
foreach($xml->children() as $child)
{
echo $child->getName() . ": " . $child . "<br />";
}
?>
This works for only this document, but when i try some bigger xml it doesn't work. It only writes back one tag all the time.
Your problem is that with your code you only fetch first nodes of xml documents. Try like this:
$xml = simplexml_load_file($url) ;
echo '<br /><b>all of the data:</b>'.'<br> </br><div class="data">';
echo $xml->getName() . "<br />";
foreach($xml -> children() as $name => $child ){
echo $name.'<br />';
foreach($child -> children() as $name1 => $child1 ) {
echo $name1.'<br />'; //fetching children nodes
foreach($child1 -> children() as $name2 => $child2 ) {
echo $name2.'<br />';
}
// ..... and so on...depends how deep is you node tree
}
}
you can use xpath to fetch what you want. It's easy and you can build queries with it. Try like this:
$result=$xml->xpath($your_query);
foreach($result as $bla) {
echo "$bla<br />";
}
You can use this CD catalog to test you code: http://www.w3schools.com/xml/cd_catalog.xml
Here are some examples of using xpath: http://www.w3schools.com/xpath/xpath_examples.asp