0

I've recently tried a simple thing with php xml writer to output db calls in a more cross-platform way - using xml. The thing is, I'd like to transform my multi is_array and foreach loops into some kind of loop:

    $arr = array('param'=>'value','otherparam'=>array('vegetable'=>'tomato'));
    $xml = new XMLWriter();
    $xml->openURI("php://output");
    $xml->startDocument();
    $xml->setIndent(true);
    $xml->startElement('whmseo');
    $xml->startElement($module);
    foreach($arr as $fkey=>$fel)
    {
        if(is_array($fel))
        {
            foreach($fel as $skey=>$sel)
            {
                if(is_array($sel))
                {
                    foreach($sel as $tkey=>$tel)
                    {
                        $xml->startElement($tkey);
                        $xml->writeRaw($tel);
                        $xml->endElement();
                    }
                }
                else
                {
                    $xml->startElement($skey);
                    $xml->writeRaw($sel);
                    $xml->endElement();
                }
            }
        }
        else
        {
            $xml->startElement($fkey);
            $xml->writeRaw($fel);
            $xml->endElement();
        }
    }
    $xml->endElement();
    $xml->endElement();
    header('Content-type: text/xml');
    $xml->flush();
    exit();

How to do that in some simple iteration?

2
  • You want to make it recursive? Commented Sep 30, 2013 at 19:27
  • Yes, that's right :-) Commented Sep 30, 2013 at 19:30

2 Answers 2

1

Something like this? I can't test against XMLWriter atm..

function xmlrecursive($xml, $key, $value) {
    if (is_array($value)) {
        $xml->startElement($key);
        foreach ($value as $key => $sub) {
            xmlrecursive($xml, $key, $sub);
        }
        $xml->endElement();
    } else {
        $xml->startElement($key);
        $xml->writeRaw($value);
        $xml->endElement();
    }
}

$arr = array('param'=>'value','otherparam'=>array('vegetable'=>'tomato'));
$xml = new XMLWriter();
$xml->openURI("php://output");
$xml->startDocument();
$xml->setIndent(true);
$xml->startElement('whmseo');
//$xml->startElement($module);
foreach ($value as $key => $sub) {
    xmlrecursive($xml, $key, $sub);
}
//$xml->endElement();
$xml->endElement();
header('Content-type: text/xml');
$xml->flush();
exit();

Output:

<?xml version="1.0"?>
<whmseo>
 <test>
  <param>value</param>
  <otherparam>
   <vegetable>tomato</vegetable>
  </otherparam>
 </test>
</whmseo>
Sign up to request clarification or add additional context in comments.

1 Comment

Well, it seems that according to this part: foreach($value as $key... it takes second-level array element, and does not process keys i.e.: <first> <second> <third>db</third> // This should contain array <forth>1</forth> </second> </first> And I think that "foreach ($value as $key => $sub) {" should be "foreach ($arr as $key => $sub) {" according to my question, am I right?
0

Not a direct answer to your question, but I would highly suggest using JSON. It is just as cross-platform compatible as XML, yet less verbose and less cumbersome to work with. It is pretty much the serialization method of choice for modern web services.

With JSON, your code would be this:

header('Content-type: application/json');
$arr = array('param'=>'value','otherparam'=>array('vegetable'=>'tomato'));
echo json_encode($arr);

1 Comment

Well I do use json_encode json_decode across my works but this time I need to use xml in particular. Thanks for reminding :-)

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.