开发者

PHP deepening XML structure

开发者 https://www.devze.com 2022-12-17 10:41 出处:网络
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):

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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号