SimpleXml problems accessing xml nodes - php

i have serious problems accessing nodes of a specific xml file:
http://write.fm/cqsmrf5
with print_r i get the following result:
SimpleXMLElement Object
(
[RecordSet] => SimpleXMLElement Object
(
[#attributes] => Array
(
[dataSource] => testdatabase
[totalRecordCount] => 3573
)
[Record] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[counter] => 1
)
[Fields] => SimpleXMLElement Object
(
[Field] => Array
(
[0] => Barcelona
[1] => 1
)
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[counter] => 2
)
[Fields] => SimpleXMLElement Object
(
[Field] => Array
(
[0] => Cádiz
[1] => 2
)
)
)
)
)
I tried to access through the command
print $xml->recordset->record[0]->fields->field[0];
But i only get the error:
Notice: Trying to get property of non-object in /Applications/MAMP/htdocs/test.php on line 18
Could anyone help me out.
Thanks in advance.

XML is case-sensitive. Try
$xml->RecordSet->Record[0]->Fields->Field[0]; // Barcelona

Related

Serialize/unserialize an encapsulated Array of SimpleXML-Elements

I have from an API-request an encapsulated Array like this:
Array
(
[1] => SimpleXMLElement Object
(
[id] => 1
[link_rewrite] => fashion-supplier
[name] => Fashion Supplier
[active] => 1
[date_add] => 2018-01-18 13:47:30
[date_upd] => 2018-01-18 13:47:30
[description] => SimpleXMLElement Object
(
[language] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => 1
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => 2
)
)
)
)
[meta_title] => SimpleXMLElement Object
....
This data I want to store in the Laravel-Cache. Because of the Simple-XML-Elements inside it is not possible to store the raw data ("Serialization of 'SimpleXMLElement' is not allowed").
Is there a way to convert this complex array-simplexml-object-combination in a storable form? The problem is, I need the XML-Attributes inside, that's why I can't use json_decode(json_encode($array));
Thank you for your help!
Alex

Getting last child in SimpleXMLElement Object with PHP

again I think I'm missing something really basic here, but I really can't figure it out.
I'm parsing an xml file formatted as excel, using SimpleXML in PHP.
My xml file looks like the following:
SimpleXMLElement Object (
[DocumentProperties] => SimpleXMLElement Object (
[Author] => Surname Name
[LastAuthor] => Name Surname
[Created] => 2016-08-01T10:15:25Z
[Version] => 14.00 )
[OfficeDocumentSettings] => SimpleXMLElement Object ( [AllowPNG] => SimpleXMLElement Object ( ) )
[ExcelWorkbook] => SimpleXMLElement Object ( [WindowHeight] => 9375 [WindowWidth] => 9705 [WindowTopX] => 270 [WindowTopY] => 615 [ProtectStructure] => False [ProtectWindows] => False )
[Styles] => SimpleXMLElement Object ( [Style] => SimpleXMLElement Object ( [Alignment] => SimpleXMLElement Object ( ) [Font] => SimpleXMLElement Object ( ) ) )
[Worksheet] => Array (
[0] => SimpleXMLElement Object (
[Table] => SimpleXMLElement Object (
[Column] => Array (
[0] => SimpleXMLElement Object ( )
[1] => SimpleXMLElement Object ( )
[2] => SimpleXMLElement Object ( ) )
[Row] => Array (
[0] => SimpleXMLElement Object (
[Cell] => Array (
[0] => SimpleXMLElement Object ( [Data] => Model )
[1] => SimpleXMLElement Object ( [Data] => Qty )
[2] => SimpleXMLElement Object ( [Data] => Description )
[3] => SimpleXMLElement Object ( [Data] => Tags ) ) )
[1] => SimpleXMLElement Object (
[Cell] => Array (
[0] => SimpleXMLElement Object ( [Data] => model_name )
[1] => SimpleXMLElement Object ( [Data] => 1 )
[2] => SimpleXMLElement Object ( [Data] => This is my item ) ) )
[....code repeats with other rows/worksheets....]
Now, if I want to display the Author name, I can simply do something like:
echo $xml->DocumentProperties->LastAuthor;
and I will get the expected result. How do I get to display the values in the various rows/cells (keeping in mind that the xml files will have a very diverse number of rows and rows are not always completely filled)?
I've tried something like the following:
echo $xml->Worksheet[0]->Table->Row[0]->Cell[0];
But doing so doesn't return any value. I would also like to be able to cycle through all the rows so that I can display them as an HTML table.
Any ideas?
Thanks a lot in advance for your help!

sorting simpleXML object

I have a problem to sort array of SimpleXMLElement Object. The data is:
[ServiceHotel] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[availToken] => 0
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[availToken] => 1
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[availToken] => 2
)
)
[3] => SimpleXMLElement Object
(
[#attributes] => Array
(
[availToken] => 3
)
)
)
I want to sort that data, order by availToken in PHP. Can anybody help me?

How to get the name and values from xml array using php

Here is my Xml code:
SimpleXMLElement Object
(
[resultsRows] => SimpleXMLElement Object
(
[row] => Array
(
[0] => SimpleXMLElement Object
(
[dimension] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => date
[value] => 20140102
[label] => Date
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => browserType
[value] => Chrome
[label] => Browser Type
)
)
)
[metric] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => visitDuration
[value] => 1242
[label] => Avg. Visit Duration
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => bounces
[value] => 3
[label] => Bounces
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => repeatVisitors
[value] => 0
[label] => Repeat Visitors
)
)
[3] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => newVisitors
[value] => 5
[label] => New Visitors
)
)
[4] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => visits
[value] => 10
[label] => Visits
)
)
[5] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => pageViews
[value] => 66
[label] => Page Views
)
)
)
)
)
)
)
Above Xml array need to print like key and values type.The array showing like dimension and metric.I want print the values inside #attributes nested array like key and value.
Thanks.
Found in the comments on php SimpleXml documentation:
function xml2array ( $xmlObject, $out = array () )
{
foreach ( (array) $xmlObject as $index => $node )
$out[$index] = ( is_object ( $node ) ) ? xml2array ( $node ) : $node;
return $out;
}
Check the docs here: http://cl1.php.net/ref.simplexml

XML xpath attribute value result to contain other results

I recently asked a question about how to select a parent's node attribute and its values at the same time using xpath, What i ended up with is :
Parent/#attr|Parent/x
It grab the parent attr's value and all of its x's nodes..
Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[attr] => attrValue
)
)
[1] => SimpleXMLElement Object
(
[0] => 1
)
[2] => SimpleXMLElement Object
(
[0] => 2
)
[3] => SimpleXMLElement Object
(
[0] => 3
)
)
The thing now.. In case there are several Parent nodes, it will mix them together and i wouldn't know which attr belongs to its x's .. it will result something like this :
Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[attr] => attrValue
)
)
[1] => SimpleXMLElement Object
(
[0] => 1
)
[2] => SimpleXMLElement Object
(
[0] => 2
)
[3] => SimpleXMLElement Object
(
[0] => 3
)
[4] => SimpleXMLElement Object
(
[#attributes] => Array
(
[attr] => anotherAttrValue
)
)
[5] => SimpleXMLElement Object
(
[0] => 1
)
[6] => SimpleXMLElement Object
(
[0] => 2
)
...
)
You see, they are all treated as a normal result, what i am trying to do is to make the attr contain all of it's results ( 1, 2, 3 ) in an array or something and the same thing goes to the other attr's to end up with something like this :
Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[attr] => attrValue
)
)
Array
(
[0] => SimpleXMLElement Object
(
[0] => 1
)
[1] => SimpleXMLElement Object
(
[0] => 2
)
[2] => SimpleXMLElement Object
(
[0] => 3
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[attr] => anotherAttrValue
)
)
Array
(
[0] => SimpleXMLElement Object
(
[0] => 1
)
[1] => SimpleXMLElement Object
(
[0] => 2
)
[2] => SimpleXMLElement Object
(
[0] => 3
)
)
)
using the xpath and the way given at first

Categories