views:

67

answers:

2

given the following XML structure (i have this in an XML-file, with lots of other content - the <p> tags are just there to indicate that other tags may follow):

<TITEL1>...</TITEL1>  
<p>..</p>
<TITEL2>...</TITEL2>  
<TITEL3>...</TITEL3>
<TITEL3>...</TITEL3>  
<P>...<P>  

is there a way to get to this using PHP (write it to a new file):

<TITEL1>
    <TITEL>...</TITEL>  
    <p>...</p>
    <TITEL2>
        <TITEL>...</TITEL>  
        <TITEL3>
            <TITEL>...</TITEL>
            <P>...</P>
        </TITEL3>
        <TITEL3>
            <TITEL>...</TITEL>
            <P>...</P>
        </TITEL3>
    </TITEL2>
</TITEL1>

or in other words,is there a way to have higher level titels inclose lower level titels and all content that follows them, thus creating a nested structure. The content of each TITEL1,2 and 3 tag should go into a new <TITEL>-element

I already asked the same question on the XSLT-side of the forum but got the advise to try with c# or java. Since I don't know those languages and know somewhat more than the basics of PHP i thought of trying it that way. Can anyone set me on my way?

+2  A: 

PHP also has a very well built in DOM support which you can use to build such structures. A place to start documenting about this extension would be http://de2.php.net/dom.

In your case, you first have to create a document then use DOMDocument::createElement DOMElement::appendChild to append that element to another element

After you're done, call DOMDocument::save to save the DOM into a specified file.

alexb
This is actually the correct way to do it. The reason I don't use this is because I don't need full DOM support, I am usually just ripping arrays out of say for instance a sql query, and then passing them off to some other service... Voted up :)
A: 

Here some code I wrote awhile ago for just this:

function array2xml($array) {
    static $index = 0;
    $pre = '';
    for ($i = 0; $i < $index;$i++) {
        $pre .= "\t";
    }
    $text = '';
    if (is_array($array)){
        foreach ($array as $k=>$v) {
            if (is_int($k)){
                if (is_array($v)){
                    $text = $text . array2xml($v);
                }
            } else if (is_string($k)){
                if (!is_array($v)){
                    $text .=  "$pre<$k>$v</$k>\n";
                } else if (is_array($v)) {
                    $index++;
                    $text .= "$pre<$k>\n".array2xml($v)."$pre</$k>\n";
                    $index--;
                }
            } else {
                if (is_array($v)){
                    $index++;
                    $text .= array2xml($v);
                    $index--;
                }
            }
        }
        return "$text";
    } else {
        return $text . $array;
    }
}
$a = array(
    "hello" => array(
        "world" => " Yay!",
    ),
    "messages" => array(
        array(
            array(
                "message" => "Goodby cruel world!"
            ),
            "message" => array(
                "text1" => "Yo",
                "text2" => "dude!",
            ),
        )
    ),
);
echo array2xml($a);

Which outputs this:

<hello >
    <world> Yay!</world>
</hello>
<messages >
    <message>Goodby cruel world!</message>
    <message >
        <text1>Yo</text1>
        <text2>dude!</text2>
    </message>
</messages>

Not 100% sure why the first tag is indented, but it never really bothered me.

Also, if you look around the documentation, there's a way to convert the xml to an array.
Hi Jolierouge. As far as i can tell your code converts an array that already has the desired structure into xml-code. What i'm looking for is a way to wrap higher level TITEL-elements around lower level ones (see my example)
AZtec