Getting last child in SimpleXMLElement Object with PHP - 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!

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

SimpleXMLElement php, foreach

I have this xml object, and I want to get each "p" $xml->query->search["p"][0]->attribute["title"], how can get each one using foreach?
SimpleXMLElement Object
(
[query] => SimpleXMLElement Object
(
[search] => SimpleXMLElement Object
(
[p] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[ns] => 0
[title] => Amebiasis
[timestamp] => 2013-09-16T16:11:24Z
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[ns] => 0
[title] => Entamoeba histolytica
[timestamp] => 2013-09-10T19:59:49Z
)
)
)
)
)
)
you can use xpath like this
$titles = $o->xpath('query/search/p/#title');
Or to get all the title attributes in the document
$titles = $o->xpath('//#title');

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

Codeigniter Unidentified Index

I have a function foo that returns an array with the below structure and what I am trying to work out is there a better way to structure my PHP as I am unsure about the indexing within the foreach as I want every ListingId
Goal:
I am wanting to write a foreach loop that gets the XML <Name> from an external XML document (Have not coded this section yet as I need to pass the ListingID from foointo the url to get the <Name>)
PHP:
$test0 = $this->foo();
$test = $test0[0]['ListingId'];
Structure:
Array ( [0] => Array ( [ListingId] => SimpleXMLElement Object ( [0] => 532712629 ) [ListingCategory] => SimpleXMLElement Object ( [0] => 0350-5748-3400- ) ) [1] => Array ( [ListingId] => SimpleXMLElement Object ( [0] => 532712202 ) [ListingCategory] => SimpleXMLElement Object ( [0] => 0350-5748-3400- ) ) [2] => Array ( [ListingId] => SimpleXMLElement Object ( [0] => 532711566 ) [ListingCategory] => SimpleXMLElement Object ( [0] => 0350-5748-3400- ) ) [3] => Array ( [ListingId] => SimpleXMLElement Object ( [0] => 532710864 ) [ListingCategory] => SimpleXMLElement Object ( [0] => 0350-5748-3400- ) ) [4] => Array ( [ListingId] => SimpleXMLElement Object ( [0] => 532710271 ) [ListingCategory] => SimpleXMLElement Object ( [0] => 0350-5748-3400- ) ) [5] => Array ( [ListingId] => SimpleXMLElement Object ( [0] => 532691526 ) [ListingCategory] => SimpleXMLElement Object ( [0] => 0350-5748-3400- ) ) [6] => Array ( [ListingId] => SimpleXMLElement Object ( [0] => 527496168 ) [ListingCategory] => SimpleXMLElement Object ( [0] => 0350-5748-3399- ) ) )
Try this way:
foreach($this->foo() as $foo) {
//here you can use your ListingId
var_dump($foo['ListingId']);
}

SimpleXml problems accessing xml nodes

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

Categories