Get values from SimpleXMLElement Object for php array - php

I need help with this issue, it seems I can't get targetCurrency out of the SimpleXMLElement Object
$xmlString = <<<XML
<channel>
<title>XML ~~ Exchange Rates ~~</title>
<language>en</language>
<item>
<baseCurrency>USD</baseCurrency>
<targetCurrency>EUR</targetCurrency>
<targetName>Euro</targetName>
<exchangeRate>0.90900497</exchangeRate>
</item>
</channel>
XML;
$xml = simplexml_load_string($xmlString);
foreach($xml->item as $rate){
$rate = (string) $rate->exchangeRate;
$curr_code = (string) $rate->targetCurrency;
$money[] = array('rate' => $rate, 'curr_code' => $curr_code);
}
print_r($money);
This outputs:
Array
(
[0] => Array
(
[rate] => 0.90947603
[curr_code] =>
)
)
[curr_code] should output 'EUR'.
How can I fix it?

You are using the same variable name for two different things:
foreach($xml->item as $rate){
// at this point, $rate is the <item> element
$rate = (string) $rate->exchangeRate;
// now $rate is a string with the exchange rate
$curr_code = (string) $rate->targetCurrency;
// so now this won't work
$money[] = array('rate' => $rate, 'curr_code' => $curr_code);
}
If you were running with display_errors switched on or checking your logs, you would have seen a message like this:
Notice: Trying to get property 'targetCurrency' of non-object
Or in PHP 8, this:
Warning: Attempt to read property "targetCurrency" on string
The fix is simply to name your variables more carefully:
foreach($xml->item as $itemElement){
$rate = (string) $itemElement->exchangeRate;
$curr_code = (string) $itemElement->targetCurrency;
$money[] = array('rate' => $rate, 'curr_code' => $curr_code);
}

Try it using xpath instead:
$items = $xml->xpath("//item");
foreach($items as $item){
$rate = $item->xpath('.//exchangeRate')[0];
$curr_code = $item->xpath('.//targetCurrency')[0];
$money[] = array('rate' => $rate, 'curr_code' => $curr_code);
}
print_r($money);
The output should be as exptected.

Related

PHP can't get inner text from XML tag [duplicate]

I have something like this:
$url = "http://ws.geonames.org/findNearbyPostalCodes?country=pl&placename=";
$url .= rawurlencode($city[$i]);
$xml = simplexml_load_file($url);
echo $url."\n";
$cityCode[] = array(
'city' => $city[$i],
'lat' => $xml->code[0]->lat,
'lng' => $xml->code[0]->lng
);
It's supposed to download XML from geonames. If I do print_r($xml) I get :
SimpleXMLElement Object
(
[code] => Array
(
[0] => SimpleXMLElement Object
(
[postalcode] => 01-935
[name] => Warszawa
[countryCode] => PL
[lat] => 52.25
[lng] => 21.0
[adminCode1] => SimpleXMLElement Object
(
)
[adminName1] => Mazowieckie
[adminCode2] => SimpleXMLElement Object
(
)
[adminName2] => Warszawa
[adminCode3] => SimpleXMLElement Object
(
)
[adminName3] => SimpleXMLElement Object
(
)
[distance] => 0.0
)
I do as you can see $xml->code[0]->lat and it returns an object. How can i get the value?
You have to cast simpleXML Object to a string.
$value = (string) $xml->code[0]->lat;
You can also use the magic method __toString()
$xml->code[0]->lat->__toString()
If you know that the value of the XML element is a float number (latitude, longitude, distance), you can use (float)
$value = (float) $xml->code[0]->lat;
Also, (int) for integer number:
$value = (int) $xml->code[0]->distance;
if you don't know the value of XML Element, you can use
$value = (string) $xml->code[0]->lat;
if (ctype_digit($value)) {
// the value is probably an integer because consists only of digits
}
It works when you need to determine if value is a number, because (string) will always return string and is_int($value) returns false
Quick Solution if you in hurry.
convert a Xml-Object to an array (or object),
function loadXml2Array($file,$array=true){
$xml = simplexml_load_file($file);
$json_string = json_encode($xml);
return json_decode($json_string, $array);
}
This is the function that has always helped me convert the xml related values to array
function _xml2array ( $xmlObject, $out = array () ){
foreach ( (array) $xmlObject as $index => $node )
$out[$index] = ( is_object ( $node ) ) ? _xml2array ( $node ) : $node;
return $out;
}
you can use the '{}' to access you property, and then you can do as you wish.
Save it or display the content.
$varName = $xml->{'key'};
From your example her's the code
$filePath = __DIR__ . 'Your path ';
$fileName = 'YourFilename.xml';
if (file_exists($filePath . $fileName)) {
$xml = simplexml_load_file($filePath . $fileName);
$mainNode = $xml->{'code'};
$cityArray = array();
foreach ($mainNode as $key => $data) {
$cityArray[..] = $mainNode[$key]['cityCode'];
....
}
}
try current($xml->code[0]->lat)
it returns element under current pointer of array, which is 0, so you will get value
header("Content-Type: text/html; charset=utf8");
$url = simplexml_load_file("http://URI.com");
foreach ($url->PRODUCT as $product) {
foreach($urun->attributes() as $k => $v) {
echo $k." : ".$v.' <br />';
}
echo '<hr/>';
}
you can convert array with this function
function xml2array($xml){
$arr = array();
foreach ($xml->children() as $r)
{
$t = array();
if(count($r->children()) == 0)
{
$arr[$r->getName()] = strval($r);
}
else
{
$arr[$r->getName()][] = xml2array($r);
}
}
return $arr;
}
$codeZero = null;
foreach ($xml->code->children() as $child) {
$codeZero = $child;
}
$lat = null;
foreach ($codeZero->children() as $child) {
if (isset($child->lat)) {
$lat = $child->lat;
}
}
foreach($xml->code as $vals )
{
unset($geonames);
$vals=(array)$vals;
foreach($vals as $key => $value)
{
$value=(array)$value;
$geonames[$key]=$value[0];
}
}
print_r($geonames);

PHP parse xml to array

i need to convert an xml to array.
I get the xml from an online api.
My code so far:
function download_page($path){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$path);
curl_setopt($ch, CURLOPT_FAILONERROR,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$retValue = curl_exec($ch);
curl_close($ch);
return $retValue;
}
$sXML = download_page('https://url/api/user/distgroup/domain/user?t=ticketofuser');
echo "xml start: ". htmlentities($sXML);
$oXML = new SimpleXMLElement($sXML);
echo "xml: ". $oXML;
foreach($oXML["distributionGroups"] as $key=>$value)
{
$groups[$key]["group"]["id"]=$value["id"];
$groups[$key]["group"]["domain"]=$value["domain"];
$groups[$key]["group"]["name"]=$value["name"];
$groups[$key]["group"]["type"]=$value["type"];
$groups[$key]["group"]["loggedIn"]=$value["loggedIn"];
$groups[$key]["group"]["nightMode"]=$value["nightMode"];
$groups[$key]["group"]["loggedInAgents"]=$value["loggedInAgents"];
$groups[$key]["group"]["freeAgents"]=$value["freeAgents"];
$groups[$key]["group"]["callsWaiting"]=$value["callsWaiting"];
}
$temp=array();
foreach ($groups as $key => $row) {
$temp[$key] = $row["id"];
}
array_multisort($temp, SORT_ASC, $groups);
$_SESSION["groups"]=$groups;
echo "groups: ". $groups;
Afterdownloaded the xml it looks like this when i echo it with htmlentities($sXML);
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<distributionGroups>
<group>
<id>33247</id>
<domain>soluno.se</domain>
<name>Kamoda Support</name>
<type>ATTENDANT</type>
<loggedIn>true</loggedIn>
<nightMode>false</nightMode>
<loggedInAgents>1</loggedInAgents>
<freeAgents>1</freeAgents>
<callsWaiting>0</callsWaiting>
</group>
<group>
<id>33257</id>
<domain>soluno.se</domain>
<name>Test 5</name>
<type>ATTENDANT</type>
<loggedIn>false</loggedIn>
<nightMode>false</nightMode>
<loggedInAgents>0</loggedInAgents>
<freeAgents>0</freeAgents>
<callsWaiting>0</callsWaiting>
</group>
</distributionGroups>
My problem is that my array is empty after my try to foreach fill the array.
What am i doing wrong?
In your second foreach, you are missing the key group. Also, you could use $oXML->group to iterator over the XML elements:
$oXML = new SimpleXMLElement($sXML);
$groups = [] ;
foreach($oXML->group as $group)
{
$groups[]["group"] = [
'id' => (string)$group->id,
'domain' => (string) $group->domain,
'name' => (string) $group->name,
'type' => (string) $group->type,
'loggedIn' => (string) $group->loggedIn,
'nightMode' => (string) $group->nightMode,
'loggedInAgents' => (string) $group->loggedInAgents,
'freeAgents' => (string) $group->freeAgents,
'callsWaiting' => (string) $group->callsWaiting,
];
}
$temp=array();
foreach ($groups as $key => $row) {
$temp[$key] = $row['group']["id"]; // missing 'group' in $row['group']
}
array_multisort($temp, SORT_ASC, $groups);
print_r($temp);
print_r($groups);
Output of $temp:
Array
(
[0] => 33247
[1] => 33257
)
Output of $groups:
Array
(
[0] => Array
(
[group] => Array
(
[id] => 33247
[domain] => soluno.se
[name] => Kamoda Support
[type] => ATTENDANT
[loggedIn] => true
[nightMode] => false
[loggedInAgents] => 1
[freeAgents] => 1
[callsWaiting] => 0
)
)
[1] => Array
(
[group] => Array
(
[id] => 33257
[domain] => soluno.se
[name] => Test 5
[type] => ATTENDANT
[loggedIn] => false
[nightMode] => false
[loggedInAgents] => 0
[freeAgents] => 0
[callsWaiting] => 0
)
)
)
Or you could remove "group" in your first array :
$oXML = new SimpleXMLElement($sXML);
$groups = [] ;
foreach($oXML->group as $group)
{
$groups[] = [
'id' => (string)$group->id,
'domain' => (string) $group->domain,
'name' => (string) $group->name,
'type' => (string) $group->type,
'loggedIn' => (string) $group->loggedIn,
'nightMode' => (string) $group->nightMode,
'loggedInAgents' => (string) $group->loggedInAgents,
'freeAgents' => (string) $group->freeAgents,
'callsWaiting' => (string) $group->callsWaiting,
];
}
$temp=array();
foreach ($groups as $key => $row) {
$temp[$key] = $row["id"];
}
array_multisort($temp, SORT_ASC, $groups);
You could make it more flexible by getting the code to copy across each element within the group, adding an element to the array with the element name. This means that as the XML changes (or if) then the code will still retain all of the data being passed over.
I've also merged the two loops, so that $temp is set in the same loop as the main data.
$oXML = new SimpleXMLElement($sXML);
$groups = array();
$temp=array();
foreach ( $oXML->group as $group ) {
$data = array();
foreach ( $group as $element ) {
$data[ $element->getName() ] = (string)$element;
}
$groups[]["group"] = $data;
$temp[] = $data["id"];
}
print_r($temp);
print_r($groups);
new SimpleXMLElement($sXML) creates an object (not an array) of an XML element. So in your case, this $oXML = new SimpleXMLElement($sXML); gives you the distributionGroups element. From there you can access its child elements like foreach($oXML->group as $group), but remember that $group would also be an instance of SimpleXMLElement(). To access the content of the element you actually need to cast the object, i.e. (int) $group->loggedInAgents, to get an integer value. Otherwise $group-> loggedInAgents will actually give you another SimpleXMLElement() object, rather than a variable.
read more in the docs

Getting XML attribute from the inner node using PHP SimpleXML is failing

Chaps,
I am trying to get attributes of the 'file' node in the following XML with SimpleXml but every time is gives me back null. I have successfully got the attributes for the study node but fail to get the 'file' atts .
here is the xml :
<?xml version="1.0" encoding="UTF-8"?>
<studies>
<study uid="1.3.12.2" acc="181">
<date>20051218</date>
<time>2156</time>
<ref>CG</ref>
<desc>Abdomen</desc>
<id></id>
<path>S00001</path>
<modality>CR</modality>
<reports>
<file cat="UNK" date="20141124">Card_Cloud.txt</file>
</reports>
</study>
and here is my code :
$studyXML = new SimpleXMLElement($studyXML);
$studyXML_array = array();
foreach ($studyXML ->study as $study)
{
// Getting uid and accession from XML attributes
$uid = (!empty($study)) ? (String)$study->attributes()->uid : '';
$acc = (!empty($study)) ? (String)$study->attributes()->acc : '';
// Getting the reports and putting them in an array
$reports = array ();
foreach($study->reports as $rep)
{
$cat = (String)$rep->attributes()->cat;
$reports[] = (String)$rep->file;
}
// Constructing the xml as an array
$studyXML_array[] = array
(
'uid' => $uid,
'acc' => $acc,
'date' => (String)$study->date,
'reports' => $reports
);
}
I can get "uid" and "acc" but I can't get "cat" and "date" inside file node.
When I look at the array of the xml in my debug variables I can see uid and acc attributes but no sign of cat and date attributes.
I Would really appreciate any help.
I prefer to stick to SimpleXML as all my code is using that so far.
Cheers
This is how I did it based on Ghost answer.
$reports = array ();
foreach($study->reports->file as $rep)
{
$temp = array();
$temp['repName'] = (String)$rep;
// Getting cat and date attributes of the report
foreach($rep->attributes() as $key => $rep)
{
$temp[$key] = (string) $rep;
}
$reports[] = $temp;
}
If you're trying to use print_r()/var_dump() to check, then it will do no justice. But it is there, try to traverse to it along with using ->attributes() inside the foreach as well:
$studyXML = new SimpleXMLElement($studyXML);
$studyXML_array = array();
foreach ($studyXML ->study as $study)
{
// Getting uid and accession from XML attributes
$uid = (!empty($study)) ? (String)$study->attributes()->uid : '';
$acc = (!empty($study)) ? (String)$study->attributes()->acc : '';
// Getting the reports and putting them in an array
$reports = array();
// loop each attribute
foreach($study->reports->file->attributes() as $key => $rep)
{
$reports[$key] = (string) $rep;
}
// Constructing the xml as an array
$studyXML_array[] = array(
'uid' => $uid,
'acc' => $acc,
'date' => (String) $study->date,
'reports' => $reports
);
}
echo '<pre>';
print_r($studyXML_array);
Sample Output:
Array
(
[0] => Array
(
[uid] => 1.3.12.2
[acc] => 181
[date] => 20051218
[reports] => Array
(
[cat] => UNK
[date] => 20141124
)
)
)

XMl parsing query in php

I'm parsing xml nodes and I'm doing something wrong but I don't know what.
I have this xml:
$xml=" <root>
<info>
<company/>
<user_id>43</user_id>
<currency>EUR</currency>
</info>
<products>
<product>
<id>1336</id>
<pn>NX.RZNAA.003</pn>
<stock>1.00</stock>
</product>
<product>
<id>1337</id>
<pn>NX.RZNAA.004</pn>
<stock>4.00</stock>
<item_number>5</item_number>
</product>
</products>
</root>";
As you can see I have two "product" nodes with child nodes. But in first node "product" I have one node less then in second. In second the node "item_number" is added (it's optional node, if it has value it is in xml otherwise not) . When I'm parsing this nodes my parser returns value from second nodes "product" even if I'm on first node.
Anyone know what is the problem here?
Here is my code:
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml) ;
$xpath = new DOMXPath($xmlDoc);
$tagForCount="count(//".$arrayPolja[0].")";
$x=$xmlDoc->getElementsByTagName("product");
$xpath = new DomXpath($xmlDoc);
$count = 3;
$arrayPolja[0] = "id";
$arrayPolja[1] = "pn";
$arrayPolja[2] = "stock";
$arrayPolja[3] = "item_number";
$r=0;
foreach ($x as $product) {
$i=0;
while ($i<=$count)
{
$polje=$arrayPolja[$i];
$values[$i]=$xpath->query($polje, $product)->item($r)->textContent;
$i++;
}
$r++;
}
For one, the second iteration in the loop is overriding the values of your array "$values" which is why you'd only be seeing the values from the second product node (if you are inspecting the "$values" array which is what I'm assuming you are doing).
Try this:
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
$xpath = new DOMXPath($xmlDoc);
$x = $xmlDoc->getElementsByTagName("product");
$array = array('id','pn','stock','item_number');
$values = array();
foreach ($x as $product) {
$data = array();
// node name here
$data['node'] = $product->nodeName;
foreach ($array as $v){
$obj = $xpath->query($v, $product)->item(0);
if (gettype($obj) == 'object'){
$data[$v] = $obj->textContent;
}
}
$values[] = $data;
}
echo '<pre>' . print_r($values, true). '</pre>';
That should produce this:
Array
(
[0] => Array
(
[node] => product
[id] => 1336
[pn] => NX.RZNAA.003
[stock] => 1.00
)
[1] => Array
(
[node] => product
[id] => 1337
[pn] => NX.RZNAA.004
[stock] => 4.00
[item_number] => 5
)

Add existing array to 3d array

I'm trying to build a 3d php array, that ultimately gets outputted as xml... This is the code I'm trying to use to prove the concept...
$test = array('apple','orange');
$results = Array(
'success' => '1',
'error_number' => '',
'error_message' => '',
'results' => Array (
'number_of_reports' => mysql_num_rows($run),
'item' => $test
)
);
I want the resulting array to look like
<success>1</success>
<error_number/>
<error_message/>
<results>
<number_of_reports>18</number_of_reports>
<item>
<0>apple</0>
<1>orange</1>
</item>
</results>
In reality the apple and orange array would be a 3d one in itself... If you've ever used the ebay api... you'll have an idea of what I'm trying to do (I think)
Try it:
Code:
<?php
$test = array('apple','orange');
$results = Array(
'success' => '1',
'error_number' => '',
'error_message' => '',
'results' => Array (
'number_of_reports' => 1,
'item' => $test
)
);
print_r($results);
function addChild1($xml, $item, $clave)
{
if(is_array($item)){
$tempNode = $xml->addChild($clave,'');
foreach ($item as $a => $b)
{
addChild1($tempNode, $b, $a);
}
} else {
$xml->addChild("$clave", "$item");
}
}
$xml = new SimpleXMLElement('<root/>');
addChild1($xml, $results,'data');
$ret = $xml->asXML();
print $ret;
Output:
<?xml version="1.0"?>
<root><data><success>1</success><error_number></error_number><error_message></error_message><results><number_of_reports>1</number_of_reports><item><0>apple</0><1>orange</1></item></results></data></root>
See below URL. I think it very use full to you:-
How to convert array to SimpleXML
Or Try it:-
$xml = new SimpleXMLElement('<root/>');
array_walk_recursive($test_array, array ($xml, 'addChild'));
print $xml->asXML();

Categories