Hi ,
I need help to convert JSON to XML
$json_arr = ‘[{“Id”:“1”,“server”:“Test1”},{“Id”:“2”,“server”:“Test2”}]’;
Hi ,
I need help to convert JSON to XML
$json_arr = ‘[{“Id”:“1”,“server”:“Test1”},{“Id”:“2”,“server”:“Test2”}]’;
Hi here are some links which might be useful to you.
http://stackoverflow.com/questions/856833/is-there-some-way-to-convert-json-to-xml-in-php
http://www.ibm.com/developerworks/xml/library/x-xml2jsonphp/
Thanks !!! , I will try…
hmm. I want to convert JSON to XML without using any framework like PEAR, CAKE PHP …
Convert this Array object to normal Array like,
Array
(
[0] => stdClass Object
(
[Id] => 1
[server] => Test1
)
)
$data = array(
“Id”=>“1”,
“server”=>“Test1”
);
Is that a demand or a question?
If your “array object” was held in $array, then you could do: $data = (array) $array[0];
What would you like the XML to look like? (Give an example)
word
Hai Salathe,
Thanks , i got the result.
my Idea is to convert json to xml. So first i decode the JSON via json_decode and i got Array Object. So I can’t pass this array object to my XML Parsing Class. Its need normal Array. If anyone know to convert directly from JSON to XML… Its great.[QUOTE][/QUOTE] Thank You…
Hai,
XML format depends on JSON
Thank U.
For what it’s worth, you can create an array directly using json_decode’s second parameter.
For examples:
$json = '[{"id":1,"server":"Test1"},{"id":2,"server":"Test2"}]';
// Decode to object
print_r(json_decode($json));
// Decode to array
print_r(json_decode($json, TRUE));
The above shows the difference:
Array
(
[0] => stdClass Object
(
[id] => 1
[server] => Test1
)
[1] => stdClass Object
(
[id] => 2
[server] => Test2
)
)
Array
(
[0] => Array
(
[id] => 1
[server] => Test1
)
[1] => Array
(
[id] => 2
[server] => Test2
)
)
Please, give an example. What would you like your existing JSON ([{“Id”:“1”,“server”:“Test1”},{“Id”:“2”,“server”:“Test2”}]) to look like in XML?
public function array_to_xml($data){
$xml = new XmlWriter();
$xml->openMemory();
$xml->startDocument(‘1.0’, ‘UTF-8’);
$xml->startElement(‘root’);
function write(XMLWriter $xml, $data){
foreach($data as $key => $value){
if(is_array($value)){
$xml->startElement($key);
write($xml, $value);
$xml->endElement();
continue;
}
$xml->writeElement($key, $value);
}
}
write($xml, $data);
$xml->endElement();
//header("Content-Type: text/xml");
return $xml->outputMemory(true);
}
This is the function i have using to convert to XML.
In case you don’t have xmlWriter installed, read this snip and associated comments for some ideas,