array2xml conversion in php - php

I need to convert array to XML, everything is working fine but when numeric value comes it is difficult to convert
function array2XML($obj, $array)
{
foreach ($array as $key => $value)
{
if(is_numeric($key))
$key = 'item' . $key;
if (is_array($value))
{
$node = $obj->addChild($key);
array2XML($node, $value);
}
else
{
$obj->addChild($key, htmlspecialchars($value));
}
}
}
I need to have previous key if numeric value exists
here is my json data
{"aaa":"111","bbb":222,"ccc":{"abc":[{"aaa":"123","bbb":253,"ccc":147},{"aaa":"123","bbb":253,"ccc":147}]},"bbc":{"bcc":[{"aaa":"1222","dfg":"123","vfbh":741},{"aaa":"1222","dfg":"123","vfbh":741}]}}
here is my created XML
<aaa>111<aaa>
<bbb>222</bbb>
<ccc>
<abc>
<item0>
<aaa></aaa>
<bbb></bbb>
<ccc></ccc>
</item0>
<item1>
<aaa></aaa>
<bbb></bbb>
<ccc></ccc>
</item1>
But i need item0 to be prevoius key for ex in this case
Here is what i expect
<aaa>111<aaa>
<bbb>222</bbb>
<ccc>
<abc>
<aaa></aaa>
<bbb></bbb>
<ccc></ccc>
</abc>
<abc>
<aaa></aaa>
<bbb></bbb>
<ccc></ccc>
</abc>

<?php
$test_array = array (
'bla' => 'blub',
'foo' => 'bar',
'another_array' => array (
'stack' => 'overflow',
),
);
$xml = new SimpleXMLElement('<root/>');
array_walk_recursive($test_array, array ($xml, 'addChild'));
print $xml->asXML();
results in
<?xml version="1.0"?>
<root>
<blub>bla</blub>
<bar>foo</bar>
<overflow>stack</overflow>
</root>
You shouldn't have any issues with numeric values.

Related

adding node in xml file via php

I have a php array and I need to convert it into xml file but I cant get the format I want.
Array:
Array
(
[1] => Array
(
[2000307] => Array
(
[eventid] => 2000307
[eveseq] => 100
[fee] => 200
)
[2000310] => Array
(
[eventid] => 2000310
[eveseq] =>101
[fee] => 300
)
)
)
convert array to xml:
$xml = new SimpleXMLElement('<Event/>');
event_to_xm($array, $xml);
function event_to_xml($array, &$xml) {
$array = json_decode($array,TRUE);
foreach($array as $key => $value) {
foreach($value as $id => $index) {
foreach($index as $title => $result) {
if(is_array($result)) {
if(!is_numeric($title)){
$subnode = $xml->addChild($title);
array_to_xml($result, $xml);
} else {
array_to_xml($result, $xml);
}
} else {
$xml->addChild($title, htmlspecialchars($result));
}
}
}
}
}
event.xml:
<?xml version="1.0"?>
<Event>
<eventid>2000307</eventid>
<eveseq>100</eveseq>
<fee>zz</fee>
<eventid>2000310</eventid>
<eveseq>101</eveseq>
<fee>0</fee>
</Event>
What I expect is that it will create a cd tag when a new array begin:
xml:
<?xml version="1.0"?>
<Event>
<cd>
<eventid>2000307</eventid>
<eveseq>100</eveseq>
<fee>200</fee>
</cd>
<cd>
<eventid>2000310</eventid>
<eveseq>101</eveseq>
<fee>300</fee>
</cd>
</Event>
What I tried:
I tried to direct add a attribute but I encounter this error Call to a member function addChild() on null
$xml=new SimpleXMLElement("event.xml", 0, TRUE);
$child = $xml->event[0]->addChild("cd");
I would take a somewhat different approach - first, use DOMDocument instead of SimpleXML, and, second, use xpath and a fragment to insert elements into the document. Something like this:
$events = array(
"2000307" => array(
"eventid" => 2000307,
"eveseq" => 100,
"fee" => 200,
),
"2000310" => array(
"eventid" => 20003010,
"eveseq" => 101,
"fee" => 300,
)
);
#create a blank document with a root element
$xml = <<<'XML'
<?xml version="1.0"?>
<Event></Event>
XML;
$document = new DOMDocument();
$document->loadXML($xml);
$xpath = new DOMXpath($document);
$expr = "//Event";
#indicate where to insert the new elements
$dest = $xpath->query($expr);
foreach($events as $event) {
$fragment = $document->createDocumentFragment();
#this is the new element to be inserted
$instance = " <cd>
<eventid>{$event['eventid']}</eventid>
<eveseq>{$event['eveseq']}</eveseq>
<fee>{$event['fee']}</fee>
</cd>\n";
$fragment->appendXML($instance);
$dest[0]->appendChild($fragment);
}
$document->formatOutput = TRUE;
echo $document->saveXML();
Output should be your expected output.
You can iterate array data and construct new simplexml object with new 'cd' child. Try like this:
$data = array (
"2000307" => array(
"eventid" => 2000307,
"eveseq" => 100,
"fee" => 200,
),
"2000310" => array(
"eventid" => 20003010,
"eveseq" => 101,
"fee" => 300,
)
);
function to_xml(SimpleXMLElement $object, array $data)
{
foreach ($data as $key => $value) {
if (is_array($value)) {
$new_object = $object->addChild('cd');
to_xml($new_object, $value);
} else {
$object->addChild($key, $value);
}
}
}
$xml = new SimpleXMLElement('<event/>');
to_xml($xml, $data);
print $xml->asXML();
I found a way of doing it and without iterate array data
$xml = new SimpleXMLElement('<Event/>');
event_to_xm($array, $xml);
function event_to_xml($array, &$xml) {
$array = json_decode($array,TRUE);
foreach($array as $key => $value) {
foreach($value as $id => $index) {
if(is_array($value)){
$neweve = $xml->addChild('cd');
}
foreach($index as $title => $result) {
if(is_array($result)) {
$subnode = $xml->addChild($title);
event_to_xml($subnode, $xml);
} else {
$neweve->addChild($title, htmlspecialchars($result));
}
}
}
}
}

php simpleXml generate a invalid XML

i have tryed some things to figure out what wrong with my code, but i dont see the problem.
Simply i have a structured assoziative Array like this:
$data = array(
'field1' => array(
'filed11' => '',
'field12' => array(
'field121' => 'blub'
)
),
'field2' => 'foo',
'field3' => array(
'field31' => 'val',
'field32' => 'other value'
)
);
i have writen a small recursive method to generate an xml from the Array:
protected function buildXmlFromArray(SimpleXMLElement $xml, $data)
{
if(is_array($data)) {
foreach($data as $key => $value) {
if(is_array($value)) {
$child = $xml->addChild($key);
$this->buildXmlFromArray($child, $value);
} else {
$xml->addChild($key, $value);
}
}
}
}
So i generate the root-node and put it in the method:
$xml = new SimpleXMLElement('<root />');
$res = $this->buildXmlFromArray($xml, $data);
print_r($xml->asXML());
exit;
i would expect that iam getting somthing like this:
<?xml version="1.0"?>
<root>
<field1>
<filed11 />
<field12>
<field121>blub</field121>
</field12>
</field1>
<field2>foo</field2>
<field3>
<field31>val</field31>
<field32>other value</field32>
</field3>
</root>
but iam getting an invalid XML:
<?xml version="1.0"?>
<root>
<field1>
<filed11>
<field12>
<field121>blub</field121>
</field12>
</field1>
<field2>foo</field2>
<field3>
<field31>val</field31>
<field32>other value</field32>
</field3>
</root>
so field11 is not closed
Whats wrong with this?
My PHP Version is 7.2.10-0ubuntu0.18.04.1

Array won't convert to XML

I have this array ($data):
Array (
[status_code] => X
[key_1] => 12345
[key_2] => 67890
[short_message] => test
[long_message] => test_long_message
)
But I am struggling to convert this to an XML element.
Here is my code:
$xml = new SimpleXMLElement('<root/>');
array_walk_recursive($data, array ($xml, 'addChild'));
print $xml->asXML();
Here is the results I am wanting:
<root>
<status_code>X</status_code>
<key_1>12345</key_1>
<key_2>67890</key_2>
<short_message>test</short_message>
<long_message>test_long_message</long_message>
</root>
Instead I get:
<X>1234567890testtest_message_long
Could somebody please point out what I am doing wrong?
Thanks,
Peter
SimpleXMLElement.addChild() takes its parameters in the order $elementName, $elementValue.
array_walk_recursive() passes the elements in the order $value, $key (in your example that would be $elementValue, $elementName).
This should work:
$data = array(
'status_code' => 'X',
'key_1' => 12345
);
$xml = new SimpleXMLElement('<root/>');
array_walk_recursive(
$data,
function ($value, $key) use ($xml) {
$xml->addChild($key, $value);
}
);
echo $xml->asXML();
Just write it out, it's much easier to read and change:
foreach ($data as $name => $value) {
$xml->$name = $value;
}
It's not always necessary to just have a callback when a simple foreach can do. Howver, creating a utility function isn't that bad either:
function simplexml_add_array(SimpleXMLElement $xml, array $data) {
foreach ($data as $name => $value) {
$xml[$name] = $value;
}
}
And then finally by inheritence (Demo):
class SimplerXMLElement extends SimpleXMLElement
{
public function addArray(array $data) {
foreach ($data as $name => $value) {
$this->$name = $value;
}
}
}
$xml = new SimplerXMLElement ('<root/>');
$xml->addArray($data);
print $xml->asXML();
Program Output (beautified):
<?xml version="1.0"?>
<root>
<status_code>X</status_code>
<key_1>12345</key_1>
<key_2>67890</key_2>
<short_message>test</short_message>
<long_message>test_long_message</long_message>
</root>

Add existing array to 3d array

I'm trying to build a 3d php array, that ultimately gets outputted as xml... This is the code I'm trying to use to prove the concept...
$test = array('apple','orange');
$results = Array(
'success' => '1',
'error_number' => '',
'error_message' => '',
'results' => Array (
'number_of_reports' => mysql_num_rows($run),
'item' => $test
)
);
I want the resulting array to look like
<success>1</success>
<error_number/>
<error_message/>
<results>
<number_of_reports>18</number_of_reports>
<item>
<0>apple</0>
<1>orange</1>
</item>
</results>
In reality the apple and orange array would be a 3d one in itself... If you've ever used the ebay api... you'll have an idea of what I'm trying to do (I think)
Try it:
Code:
<?php
$test = array('apple','orange');
$results = Array(
'success' => '1',
'error_number' => '',
'error_message' => '',
'results' => Array (
'number_of_reports' => 1,
'item' => $test
)
);
print_r($results);
function addChild1($xml, $item, $clave)
{
if(is_array($item)){
$tempNode = $xml->addChild($clave,'');
foreach ($item as $a => $b)
{
addChild1($tempNode, $b, $a);
}
} else {
$xml->addChild("$clave", "$item");
}
}
$xml = new SimpleXMLElement('<root/>');
addChild1($xml, $results,'data');
$ret = $xml->asXML();
print $ret;
Output:
<?xml version="1.0"?>
<root><data><success>1</success><error_number></error_number><error_message></error_message><results><number_of_reports>1</number_of_reports><item><0>apple</0><1>orange</1></item></results></data></root>
See below URL. I think it very use full to you:-
How to convert array to SimpleXML
Or Try it:-
$xml = new SimpleXMLElement('<root/>');
array_walk_recursive($test_array, array ($xml, 'addChild'));
print $xml->asXML();

PHP array to XML with attributes

In CakePHP I know the XML-class in cake.libs. What I like on this easy to use class is the fact that you can convert an array to xml with attributes.
On a current project I'm working with Zend Framework and I am missing this nice class.
Does anybody know how I can convert an PHP-array to XML with attributes easily?
I tried this one but unfortunately its not easy to handle result arrays from database cause you have to define the attributes inside the array.
Maybe I have missed something in ZF? Or does anybody know how to adapt the CakePHP class to ZF?
Many thanks for any helpful hints.
This function will allow you to create XML document with simple PHP array variable. Nodes are just stacked onto each other and naming it 'Attribute_XXX' will add atribute XXX to that parent node.
function to_xml(SimpleXMLElement $object, array $data)
{
$attr = "Attribute_";
foreach ($data as $key => $value) {
if (is_array($value)) {
$new_object = $object->addChild($key);
to_xml($new_object, $value);
} else {
if(strpos($key, $attr) !== false){
$object->addAttribute(substr($key, strlen($attr)), $value);
}else{
$object->addChild($key, $value);
}
}
}
}
Usage:
$my_array = array (
'TagN1' => array (
'TagInsideOfIt' => array (
'Atrribute_IDuser' => 'anything',
'RegularTag' => 'whatever',
'Address' => array(
'Attribute_ID' => '111',
'Attribute_key' => 'aaa',
'Company' => 'Google Inc.'
)
)
)
);
$xml = new SimpleXMLElement('<root/>');
to_xml($xml, $my_array);
Header('Content-type: text/xml');
print($xml->asXML());
You can get some answers at this URL How to convert array to SimpleXML
An alternative/fast and easy approach would be to convert array data into JSON, that would be faster as well using Zend library. Converting array into JSON is very easy as using follows functions.
Zend_Json::encode() and Zend_Json_Encoder::encode()
Tanks, a using the code of #jezda159 and i modified for me, this is my code:
the array:
$elements_array = array(
"node" => array(
"subnode" => array(
"[attribute]" => "valueOfAttribute",
"[second-attribute]" => "valueOfAttribute",
"second-subnode" => array(
"[attribute]" => "valueOfAttribute",
"[second-attribute]" => "valueOfAttribute",
),
"ListOfElements" => array(
"one",
"two",
"tree"
)
)
),
);
The code:
function array_to_xml($data, $object){
foreach ($data as $key => $value) {
$keyname = is_numeric( $key ) ? "item".$key : $key;
if (is_array($value)) {
$new_object = $object->addChild($keyname);
array_to_xml($value, $new_object);
} else {
preg_match("#\[([a-z0-9-_]+)\]#i", $keyname, $attr);
if( count($attr) ){
$object->addAttribute($attr[1], $value);
}else{
$object->addChild($keyname, $value);
}
}
}
}
$xml_user_info = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><Elements></Elements>');
//function call to convert array to xml
array_to_xml($elements_array,$xml_user_info);
header('Content-type: text/xml');
//show generated xml file
echo $xml_user_info->asXML();
The result:
<?xml version="1.0" encoding="UTF-8"?>
<Elements>
<node>
<subnode attribute="valueOfAttribute" second-attribute="valueOfAttribute">
<second-subnode attribute="valueOfAttribute" second-attribute="valueOfAttribute"/>
<ListOfElements>
<item0>one</item0>
<item1>two</item1>
<item2>tree</item2>
</ListOfElements>
</subnode>
</node>
</Elements>

Categories