It's pretty straightforward, I have an array with a number of nodes of the same structure available as Simple XML objects (extracted from different XML documents). What's the easiest way to add them to a single XML document, so I can output the combined XML? I've searched, but I can't find a good solution.
Edit: Array looks like this, how do I combine these objects in one single XML object?
Array (
[0] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[no] => 23432423
[type] => Array
)
[id] => 40043030
[title] => Cars
[cinemadate] => 2011-07-06
[changedate] => 2011-07-27T10:19:00
[year] => 2011
[length] => 112
[genres] => SimpleXMLElement Object
(
[genre] => animatie
)
[1] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[no] => 48050593
[type] => Array
)
[id] => 1231676
[title] => Arrietty
[cinemadate] => 2011-07-06
[changedate] => 2011-06-21T10:39:00
[genres] => SimpleXMLElement Object
(
[genre] => animatie
)
Iterate over the array and add the data to the existing simplexml object. You have not provided much information in your question how the array data looks like nor have you provided an example simplexml object / xml chunk, so there is not much more to say specifically.
Related
How would I access the [0] -> Array() located below and retrieve the [title] which is "Spirited Away" by iterating over the array?
Array
(
[#attributes] => Array
(
[version] => 2.0
)
[channel] => Array
(
[title] => Site Title
[link] => site/
[language] => en-us
[category] => All
[image] => Array
(
[title] => Site
[url] => http://example.com
[link] => example.com
)
[item] => Array
(
[0] => Array
(
[title] => Spirited Away
[pubDate] => date
[category] => Movies
[link] => linkhere
[enclosure] => Array
(
[#attributes] => Array
(
[url] => someurlhere
[length] => length
[type] => application/x-bittorrent
)
)
I'm currently trying to iterate over the array using a foreach loop but It's outputting nothing. I would post my code but the code section of this question is already too long. I'm using PHP and nothing I have tried so far was working.
This JSON code was generated from an XML document using json_encode and then decoded using json_decode into a JSON object.
comment to answer as its correct.
to loop through this particular array structure its:
foreach ( $array['channel']['item'] as $x){
echo $x['title'];
}
I have been working on this sports website as kind of a learning project.
I have a table laid out in html to organize the xml data
I am using this script to pull live data from a remote server that provides it in xml.
<?php
$url = "http://xml.feed.com";
$xmlinfo = simplexml_load_file($url);
print_r($xmlinfo);
?>
This is the first time I have ever use php so any advice/criticism would be greatly appreciated and excepted.
Here is a sample of simpleXML.
SimpleXMLElement Object
(
[params] => SimpleXMLElement Object
(
[#attributes] => Array
(
[legend] => False
[catId] => -1
[sportId] => -1
[matchId] => -1
[delta] => 60
[deltaUtc] => 2014-01-25T00:35
[utc] => 2014-01-25T01:35
[includeCent] => False
[includeFraction] => False
)
)
[data] => SimpleXMLElement Object
(
[s] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => 1
[n] => American Football
)
[cat] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => 1109
[n] => NFL Pro Bowl
)
[m] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => 1015211
[n] => Team Rice v Team Sanders
[mid] => 573422
[rot] => 495
[dt] => 2014-01-27T00:30
)
[t] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => 1
[n] => Single Match
)
[l] => Array
(
[0] => Team Rice
[1] => Team Sanders
)
I have been trying to figure out how to display this xml data using php tags embedded in the html code..
So I have 2 questions.
How would these tags be written?
And, Am I heading in the right direction as far as my approach using php embedded in html?
Thanks much in advance! And I hope I was clear enough...
I have code for read XML SOAP 1.2 from web service.
Im using this: https://stackoverflow.com/a/18580428/2629513
I get this code below:
SimpleXMLElement Object
(
[OdkazyResponse] => SimpleXMLElement Object
(
[OdkazyResult] => SimpleXMLElement Object
(
[odkazy] => SimpleXMLElement Object
(
[odkaz] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[kod_zbozi] => 31400001
[typ] => OBR1
[popis] => Oki ML 280 - foto
[url] => http://www.atcomp.cz/katalog/31400001/ML280.gif
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[kod_zbozi] => EC376123GB
[typ] => OBR1
[popis] => Malý obrázek
[url] => http://www.atcomp.cz/katalog/EC376123GB/lq-680_-_maly.jpg
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[kod_zbozi] => EC376123GB
[typ] => OBR2
[popis] => Velký obrázek
[url] => http://www.atcomp.cz/katalog/EC376123GB/lq-680_-_velky.jpg
)
)
And how I can read the [kod_zbozi], [typ], [popis], [url] attributes? I need to save it into my mysql database (this is not problem, the problem is read the data from this format XML). Thanks.
Well, the very first thing you might want to do is to convert that object into an array (to avoid naming problems)
You can use this function to do that:
function object2array($object) {
return json_decode(json_encode($object), true);
}
then something like this:
$data = object2array(simplexml_load_string('....'));
print_r($data); // Its regular array now, use it keys to access values, then simply insert them into db
I have a simpleXML output of:
SimpleXMLElement Object
(
[#attributes] => Array
(
[version] => 2
)
[currentTime] => 2013-02-05 21:26:09
[result] => SimpleXMLElement Object
(
[rowset] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => characters
[key] => characterID
[columns] => name,characterID,corporationName,corporationID
)
[row] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => Wrytha Cy
[characterID] => 209668693
[corporationName] => Deep Core Mining Inc.
[corporationID] => 1000006
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => Eve Mae
[characterID] => 624980803
[corporationName] => Viziam
[corporationID] => 1000066
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => Wrytha
[characterID] => 709227913
[corporationName] => The Flying Tigers
[corporationID] => 669350666
)
)
)
)
)
[cachedUntil] => 2013-02-05 21:35:04
)
I would like to loop through with my php loop and get "name' and "characterID". I've trying something like:
$simpleXML = simplexml_load_string($xml);
foreach ($simpleXML->result->rowset->row as $row) {
print_r($row);
$name = $row['#attributes']['name'];
echo $name.'<br>';
}
but $name is not being set. It's gonna be something simple, just not seeing it in my haste and first time with simpleXML.
Attributes are accessed using the syntax $element['attribute_name'], so in your case, you need $row['name'].
It's important to remember that SimpleXML objects are kind of magic - the $element->child, $element_list[0] and $element['foo'] syntax overloads the normal PHP logic to be useful. Similarly, (string)$element will give you the full textual content of an element, however it is broken up in the actual XML.
As such, the print_r output will not give you a "real" view of the object, so should be used with care. There are a couple of alternative debug functions I've written here which give a more accurate idea of how the object will behave.
I have a codeigniter app that queryies an api and gets a simplexml object. I had hard coded the values I wanted from the simplexml object, but found out later that the structure of the object isn't always the same, so I can't use casting to get the values I need.
Is there an array_search like function that I could use for this so I can use the "classid" to get the value? Here is a sampling of the simplexml object:
[Package] => SimpleXMLElement Object
(
[#attributes] => Array
(
[ID] => e981b9a72cd305c7d97cc530ef9b3015
)
[Postage] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[CLASSID] => 3
)
[MailService] => Express Mail<sup>®</sup>
[Rate] => 57.60
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[CLASSID] => 2
)
[MailService] => Express Mail<sup>®</sup> Hold For Pickup
[Rate] => 57.60
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[CLASSID] => 55
)
[MailService] => Express Mail<sup>®</sup> Flat Rate Boxes
[Rate] => 39.95
)
I was using this to get the [Rate] value:
$usps->Package->Postage[0]->Rate
The problem is I need to use [CLASSID] because that is consistant where the data in the number key(?) changes.
Use XPath
You can not only search for specific elements, but do all kinds of crazy searches using various axes.
This should work, but I have no way of testing it without the raw XML:
$rates = array();
foreach ($usps->Package->Postage as $postage)
$rates[$postage->attributes()->CLASSID] = $postage->Rate;