2

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");
2
  • Can you edit your question and add the array in question? Commented Dec 6, 2022 at 2:59
  • @JackFleeting added array Commented Dec 6, 2022 at 3:04

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

This solution is not suitable for me because I have thirty data inside the array, it will be hard to maintain if I use this solution
@C123D The array itself isn't changed, so I'm not sure what the solution has to do with maintenance.
Every time when someone add a new data in the array then the function need to be modified.
I found out this is actually a more sophisticated and easy way to create xml. I should use this approach at the beginning as it saved me a lot of time
@C123D Correct! Glad it worked for you!>
0

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();

Comments

0

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));
                    }
                }
            }
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.