Simple XML cannot access name attribute successfully - php

I have an object that spits out this if i run this code:
print_r($xml->config->Exported->stats->children() );
SimpleXMLElement Object
(
[Stat] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Name] => Name
[abrev] =>
[format] => 2
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Name] => Hands
[abrev] => H:
[format] => 0
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Name] => V.total
[abrev] => V:
[format] => 0
)
)
)
My code is:
foreach($xml->config->Exported->stats->children() as $node){
echo $node . "<br />";
switch ( $node->getName() )
{
case 'Stat':
$stat_name = (string)$node['name'];
echo $stat_name . " | ";
break;
case 'New_Line':
echo '<hr />';
break;
}
Which should return something like this:
http://codepad.viper-7.com/hMvg9W
While that does work my code does not. Does the simple xml output differ from the standard xml output in the example that works, any idea how i tweak this to get it to work?

XML is case sensitive. "Name" and "name" are different qualified names.

Related

How to check array object and array?

First array:
[VariationSpecificsSet] => SimpleXMLElement Object
(
[NameValueList] => Array
(
[0] => SimpleXMLElement Object
(
[Name] => Size
[Value] => Array
(
[0] => 5FT King Size
[1] => 4FT6 Double
)
)
[1] => SimpleXMLElement Object
(
[Name] => Main Colour
[Value] => Array
(
[0] => Brown
[1] => Black
)
)
)
)
Second Array:
[Variation] => SimpleXMLElement Object
(
[StartPrice] => 14.99
[Quantity] => 12
[VariationSpecifics] => SimpleXMLElement Object
(
[NameValueList] => SimpleXMLElement Object
(
[Name] => Size
[Value] => No.10-1M
)
)
)
examine above two arrays
i want to store value NameValueList in database but the problem is sometimes it is SimpleXMLElement Object and sometimes it is Array
how can i store them ...??
You can detect is by is_array().
$myVal=$test['NameValueList'];
if(is_array($myVal) && count($myVal)>0){
foreach($myVal as $item){
echo $item->Name.":".echo $item->Value;
}
} else {
echo $myVal->Name.":".echo $myVal->Value;
}
Did you tried using json_encode like below.
You can convert the object to array.
$array=json_decode(json_encode($object),true);

PHP get value inside nested array/object

I'm trying to return the value of the typeName key inside this object (xml).
[geometricProperty] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[typeName] => localizacao // < - - - This is the value I need.
)
[Point] => SimpleXMLElement Object
(
[#attributes] => Array
(
[ID] => swrefVgetZredeVcollX757678785
[srs] => ut_cm
)
[coordinates] => 38871,739716
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[typeName] => limite
)
[Polygon] => SimpleXMLElement Object
(
[#attributes] => Array
(
[ID] => swrefVgeometryV08945038
[srs] => utm_23_cm
)
[outerBoundaryIs] => SimpleXMLElement Object
(
[LinearRing] => SimpleXMLElement Object
(
[coordinates] => 318950,7399585 31981,39650 31826,73990 316956,79750
)
)
)
)
...
I've tried the following but it returns NULL
echo "<p>This object has ".(count($n->Feature->geometricProperty))." items.</p>";
$c=0;
foreach($n->Feature->geometricProperty as $k){
$c++;
echo "<p>$c)".(gettype($k))."</p>";
foreach(array_keys(get_object_vars($k)) as $o){
echo "<p>$o</p>";
switch($o){
case "#attributes": var_dump($k->{$o}["typeName"]); $tipo=$k->{$o}["typeName"]; break;
case "Point": $value=$k->{$o}->coordinates; break;
case "Polygon": $value=$k->{$o}->outerBoundaryIs->LinearRing->coordinates; break;
case "Line...": break;
}
}
echo "<p>TIPO: $tipo</p><p>VALOR: $value</p>";
}
OUTPUT:
This object has 3 items.
1)object
#attributes
NULL
Point
TIPO:
VALOR: 31869871,739796106
2)object
#attributes
NULL
Polygon
TIPO:
Search and found this overflow question and can't guess what am I doing wrong.
You can use SimpleXmlElement::attributes() to get an array of attributes. So then you would just loop over them and display them or use implode or something... depending on the output you want.

How to retrieve value from SimpleXMLElement Object

After calling the prestashop webservice, I recieved the response as shown below:
SimpleXMLElement Object
(
[order] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => 1
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => 2
)
)
)
)
I tried to retrive the content by looping as shown below:
foreach($resources as $resource){
echo '<pre>';
print_r($resource['id']);
echo '</pre>';
}
This gives me:
SimpleXMLElement Object
(
[0] => 1
)
SimpleXMLElement Object
(
[0] => 2
)
How can I retrieve 1 and 2 which are the values of these objects? thanks
I hate simplexml...
<?php
$xml = file_get_contents('xml.xml');
$xml = new SimpleXMLElement($xml);
foreach($xml as $key => $value)
{
$attrs = $value->attributes();
foreach($attrs as $attr_k => $attr_v)
echo $attr_k.": ".$attr_v."\n";
}

Parsing PHP SimpleXMLElement

I am using php to consume a web service (in coldfusion) to validate against the active directory. My code is below.
<?php
$racf = $_SERVER['AUTH_USER'];
//echo $racf;
//echo $myracf = trim($racf, "FEDERATED\.");
//get authenticated user
$arrUser = explode("\\", $_SERVER["LOGON_USER"]);
$racf = $arrUser[1];
echo $racf.'<br ><br >';
$logins = "http://acoldfusionwebservice/login.cfc?method=loginad&racf=$racf";
if( ! $xml = simplexml_load_file($logins) )
{
echo 'unable to load XML file';
}
else
{
//echo 'XML file loaded successfully <br />';
print_r ($xml);
}
?>
And this produces the following.
SimpleXMLElement Object
(
[#attributes] => Array
(
[version] => 1.0
)
[header] => SimpleXMLElement Object
(
)
[data] => SimpleXMLElement Object
(
[recordset] => SimpleXMLElement Object
(
[#attributes] => Array
(
[rowCount] => 1
[fieldNames] => cn,mail,givenName,sn
)
[field] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => cn
)
[string] => B000000
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => mail
)
[string] => john.doe#company.com
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => givenName
)
[string] => John
)
[3] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => sn
)
[string] => Doe
)
)
)
)
)
Can someone help me to parse this information so that I can assign variables and use them. Thanks.
Try
$xml->data->recordset->field[0]->string
I think that field[0] is an Object again

Select elements other then a specific one form Xml Object

I am parsing the array given below. Looping through td using foreach. Now i want to select the value other than [#attributes]. I cannot use a or p specifically as they change through out the objects.
How can i achieve this?
[0] => SimpleXMLElement Object
(
[th] => SimpleXMLElement Object
(
[#attributes] => Array
(
[rowspan] => 2
[scope] => row
)
[p] => Memory
)
[td] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[class] => ttl
)
[a] => Card slot
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[class] => nfo
)
[p] => No
)
)
)
Want the solution to work in php.
Try below one
<?php
foreach($td as $element)
{
foreach($element as $key => $value)
{
if(!preg_match("/#/", $key) && !is_array($value))
echo $element[$key];
}
}
?>

Categories