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');
Related
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!
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?
I got an xml response of
SimpleXMLElement Object
(
[#attributes] => Array
(
[version] => 2.0
[msg_type] => response
)
[header] => SimpleXMLElement Object
(
[param] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => SN
[value] => 1012
)
)
...
[9] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => ErrorInfo
[value] => NA
)
)
[10] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => ProcessTime
[value] => 0
)
)
)
)
[Heartbeat] => SimpleXMLElement Object
(
)
)
I tried:
$version1 = (string) $xml->version; // expecting to get "2.0"
$errorinfo = (string) $xml->name->ErrorInfo; // expecting to get "NA"
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
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']);
}