PHP JSON or Array to XML
Check it here: How to convert array to SimpleXML
and this documentation should help you too
Regarding Json to Array, you can use json_decode to do the same!
How do I Convert jSON to XML
Instead of feeding your function an object, try to feed an array instead:
$jSON = json_decode($raw_data, true);
// ^ add second parameter flag `true`
Example:
function array2xml($array, $xml = false){
if($xml === false){
$xml = new SimpleXMLElement('<result/>');
}
foreach($array as $key => $value){
if(is_array($value)){
array2xml($value, $xml->addChild($key));
} else {
$xml->addChild($key, $value);
}
}
return $xml->asXML();
}
$raw_data = file_get_contents('http://pastebin.com/raw.php?i=pN3QwSHU');
$jSON = json_decode($raw_data, true);
$xml = array2xml($jSON, false);
echo '<pre>';
print_r($xml);
Sample Output
Convert JSON to XML using PHP
It has been explained here:
Is there some way to convert json to xml in PHP?
Use json_decode() and PEAR::XML_Serializer
how to convert json response data to xml
function array2xml($array, $xml = false){
if($xml === false){
$xml = new SimpleXMLElement('<result/>');
}
foreach($array as $key => $value){
if(is_array($value)){
array2xml($value, $xml->addChild($key));
} else {
$xml->addChild($key, $value);
}
}
return $xml->asXML();
}
$jSON = json_decode($raw_data, true);
$xml = array2xml($jSON, false);
echo '<pre>';
print_r($xml);
PHP - limit the elements while converting JSON to XML
Please try this. I have added the counter in under if condition where checking value as array
function array_to_xml( $data, &$xml_data ) {
$counter = 1;
foreach( $data as $key => $value ) {
if( is_numeric($key) ){
$key = 'items'.$key; //dealing with <0/>..<n/> issues
}
if( is_array($value) ) {
if($counter <= 50) {
$subnode = $xml_data->addChild($key);
array_to_xml($value, $subnode);
$counter++;
}
} else {
$xml_data->addChild("$key",htmlspecialchars("$value"));
}
}
}
Convert json to xml in php without an online converter
You can convert JSON to array/object by using json_decode()
, once you have it inside array you can use any XML Manipulation method to build XML out of it.
For example you may build XML structure like this:
<array>
<data name="atrribute1">string 1</data>
<array name="array_atribute">
<data name="attribute2">string 2</data>
<data name="attribute2">string 2</data>
</array>
</array>
By DOMDocument
, and method DOMDocument::createElement()
class XMLSerialize {
protected $dom = null;
public function serialize( $array)
{
$this->dom = new DOMDocument('1.0', 'utf-8');
$element = $this->dom->createElement('array');
$this->dom->appendChild($element);
$this->addData( $element, $array);
}
protected function addData( DOMElement &$element, $array)
{
foreach($array as $k => $v){
$e = null;
if( is_array( $v)){
// Add recursive data
$e = $this->dom->createElement( 'array', $v);
$e->setAttribute( 'name', $k);
$this->addData( $e, $v);
} else {
// Add linear data
$e = $this->dom->createElement( 'data', $v);
$e->setAttribute( 'name', $k);
}
$element->appendChild($e);
}
}
public function get_xml()
{
return $this->dom->saveXML();
}
}
PHP convert XML to JSON
I figured it out. json_encode handles objects differently than strings. I cast the object to a string and it works now.
foreach($xml->children() as $state)
{
$states[]= array('state' => (string)$state->name);
}
echo json_encode($states);
Related Topics
Smtp Server Response: 530 5.7.0 Must Issue a Starttls Command First
PHP Function with Unlimited Number of Parameters
Warning: MySQLi_Query() Expects Parameter 1 to Be MySQLi, Resource Given
Allow Re-Sending New Order Notification in Woocommerce 5+
Preg_Replace: Add Number After Backreference
How to Read Cookie/Session Value While Executing PHP5 Script Through Command Prompt
Is There Way to Keep Delimiter While Using PHP Explode or Other Similar Functions
Convert Invalid JSON into Valid JSON
Transpose 2D Array, Join Second Level with Commas, and Join First Level with Pipes
Mysql_Num_Rows(): Supplied Argument Is Not a Valid MySQL Result Resource
MySQL - Couldn't Connect Unknown Database 'Databasename' Error
Call to a Member Function Execute() on Boolean In
Woocommerce - Hide Other Shipping Methods When Free Shipping Is Available
Import File Size Limit in PHPmyadmin
Php: How to Call Function of a Child Class from Parent Class