3

I'm working on an XML library, that can create / parse xml's from arrays/jsons. I managed to write the parser with xml_parser (and google help :p) cause SimpleXML wasn't good enough for what I'm doing.

I've managed to create an array that looks something like that:

Array 
(
[flow] => Array
    (
        [math] => Array
            (
                [apply] => Array
                    (
                        [lt] => Array
                            (
                            )

                        [apply] => Array
                            (
                                [divide] => Array
                                    (
                                    )

                                [apply] => Array
                                    (
                                        [minus] => Array
                                            (
                                            )
                                    )
                            )

                        [otherStuff] => 0
                    )

            )

        [true] => Array
            (


            )

        [true_attr] => Array
            (
                [xsi:type] => SomeStuff
                [id] => 2
            )

    )

[flow_attr] => Array
    (
        [id] => 0
        [xmlns:xsi] => http://www.w3.org/2001/XMLSchema-instance
    )
)

As you can see, it should look something like this ( not the best example :p ):

<flow id="0">
 <math>
 <lalaa/>
  <appyl>
 </apply>
  </math>
</flow>

Note that empty arrays should end with /> for example , and so on

As you can see I separated the node it self to node_attr that contains the attrs of the nodes. Like flow_attr, true_attr.

Anyone have an idea how to convert this array back to xml? I'm just lost and don't know what to do.

Thanks for the help!

1

3 Answers 3

3
function recurse2xml ($array, &$string = "") {
    foreach ($array as $key => $subArray) {
        if (substr($key, -5) == "_attr")
            continue;
        $attrs = "";
        if (isset($array["$key_attr"]))
            foreach ($array["$key_attr"] as $attr => $value)
                $attrs .= " $attr='".str_replace($value, "'", "\\'")."'";
        if (empty($subArray)) {
            $string .= "<$key$attrs />";
        } else {
            $string .= "<$key$attrs>";
            if (is_scalar($subArray))
                $string .= $subArray;
            else
                recurse2xml($subArray, $string);
            $string .= "</$key>";
        }
    }
    return $string;
}

This function called with recurse2xml($array); expands your array tree into an xml tree (string form).

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

6 Comments

Thanks for the answer! It didn't worked, but I'll try to fork it and move on from this.
@MichaelArenzon It outputs what I expect it to... but what are you expecting?
well there's a few syntax errors, and it doesn't support when there's a value, like in here: [otherStuff] => 0 so I'm getting something like: <otherStuff><0></0></otherStuff> and the attrs aren't showing from some strange reason :S but thanks yet
@MichaelArenzon Okay, but how should it handle scalars? As a value? like <otherStuff>0</otherStuff>? The attrs are showing up locally...
yep, exactly. Btw you can check out the parser I found here: sourcecookbook.com/en/recipes/58/parse-xml-to-array-in-php I've changed it a bit but the logic still the same
|
2

Try Array2XML, worked flawlesly for me. Including CDATA Parts, etc.

2 Comments

Try to place this kind of things in the comments.
The site is down so this is worthless now. Would have been good to have the source code for the function in your answer.
0
function recursiveArrayToXml($array, &$return=""){
    foreach ($array as $key => $subarray){
        if(empty($key)) {continue;}
        $key = preg_replace('/[^\da-z]/i', '', $key);
        if(preg_match('/[0-9]/i',$key[0])){
            $key = 'x'.$key;
        }
        $return .= "<".$key.">";
        if(!is_array($subarray)){
            $return .= htmlentities($subarray);
        }else{
            recursiveArrayToXml($subarray, $return);
        }
        $return .= "</".$key.">\n";
    }
    return $return;
}

This function will similar to the posted above fix problems in element names and element values. Called by recursiveArrayToXml($myArray);

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.