编辑xml文件

时间:2010-09-02 05:12:40

标签: php

我有一个xml文件

<?xml version="1.0" encoding="UTF-8"?>
<xml>
<settings>
<title>Calendar2</title>
<subTitle>Calendar2</subTitle>
</settings>
<events date="02-09-2010">
<event>
<title>HTML Tags</title>
<description>HTML Tags</description>
</event>
</events>
</xml>

我如何在事件标签内添加另一个事件,相对于日期而言

<?xml version="1.0" encoding="UTF-8"?>
    <xml>
    <settings>
    <title>Calendar2</title>
    <subTitle>Calendar2</subTitle>
    </settings>
    <events date="02-09-2010">
    <event>
    <title>HTML Tags</title>
    <description>HTML Tags</description>
    </event>
    <event>
    <title>Another Title</title>
    <description>Another description</description>
    </event>
    </events>
    </xml>

3 个答案:

答案 0 :(得分:1)

SimpleXml可能会有所帮助

修改:请参阅示例代码here(#9&amp;#10)

答案 1 :(得分:1)

我给你2个函数用于xml2array转换,另一个用于array2xml转换

function xml2ary(&$string)
{
        $parser = xml_parser_create ();
        xml_parser_set_option ($parser , XML_OPTION_CASE_FOLDING , 0);
        xml_parse_into_struct ($parser , $string , $vals , $index);
        xml_parser_free ($parser);

        $mnary = array();
        $ary = &$mnary;
        foreach ( $vals as $r )
        {
                $t = $r['tag'];
                if ($r['type'] == 'open')
                {
                        if (isset ($ary[$t]))
                        {
                                if (isset ($ary[$t][0]))
                                        $ary[$t][] = array();
                                else
                                        $ary[$t] = array($ary[$t] , array());
                                $cv = &$ary[$t][count ($ary[$t]) - 1];
                        }
                        else
                                $cv = &$ary[$t];
                        if (isset ($r['attributes']))
                        {
                                foreach ( $r['attributes'] as $k => $v )
                                        $cv['_a'][$k] = $v;
                        }
                        $cv['_c'] = array();
                        $cv['_c']['_p'] = &$ary;
                        $ary = &$cv['_c'];

                }
                elseif ($r['type'] == 'complete')
                {
                        if (isset ($ary[$t]))
                        { // same as open
                                if (isset ($ary[$t][0]))
                                        $ary[$t][] = array();
                                else
                                        $ary[$t] = array($ary[$t] , array());
                                $cv = &$ary[$t][count ($ary[$t]) - 1];
                        }
                        else
                                $cv = &$ary[$t];
                        if (isset ($r['attributes']))
                        {
                                foreach ( $r['attributes'] as $k => $v )
                                        $cv['_a'][$k] = $v;
                        }
                        $cv['_v'] = (isset ($r['value']) ? $r['value'] : '');

                }
                elseif ($r['type'] == 'close')
                {
                        $ary = &$ary['_p'];
                }
        }

        _del_p ($mnary);
        return $mnary;
}


function ary2xml($cary , $d = 0 , $forcetag = '')
{
        $res = array();
        foreach ( $cary as $tag => $r )
        {
                if (isset ($r[0]))
                {
                        $res[] = ary2xml ($r , $d , $tag);
                }
                else
                {
                        if ($forcetag)
                                $tag = $forcetag;
                        $sp = str_repeat ("\t" , $d);
                        $res[] = "$sp<$tag";
                        if (isset ($r['_a']))
                        {
                                foreach ( $r['_a'] as $at => $av )
                                        $res[] = " $at=\"$av\"";
                        }
                        $res[] = ">" . ((isset ($r['_c'])) ? "\n" : '');
                        if (isset ($r['_c']))
                                $res[] = ary2xml ($r['_c'] , $d + 1);
                        elseif (isset ($r['_v']))
                                $res[] = $r['_v'];
                        $res[] = (isset ($r['_c']) ? $sp : '') . "</$tag>\n";
                }

        }
        return implode ('' , $res);
}

将你的xml传递给函数xml2ary(),你将获得如下所示的输出

Array
(
    [xml] => Array
        (
            [_c] => Array
                (
                    [settings] => Array
                        (
                            [_c] => Array
                                (
                                    [title] => Array
                                        (
                                            [_v] => Calendar2
                                        )

                                    [subTitle] => Array
                                        (
                                            [_v] => Calendar2
                                        )

                                )

                        )

                    [events] => Array
                        (
                            [_a] => Array
                                (
                                    [date] => 02-09-2010
                                )

                            [_c] => Array
                                (
                                    [event] => Array
                                        (
                                            [_c] => Array
                                                (
                                                    [title] => Array
                                                        (
                                                            [_v] => HTML Tags
                                                        )

                                                    [description] => Array
                                                        (
                                                            [_v] => HTML Tags
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

然后按照你想要的方式转换这个数组。

例如。

Array
(
    [xml] => Array
        (
            [_c] => Array
                (
                    [settings] => Array
                        (
                            [_c] => Array
                                (
                                    [title] => Array
                                        (
                                            [_v] => Calendar2
                                        )

                                    [subTitle] => Array
                                        (
                                            [_v] => Calendar2
                                        )

                                )

                        )

                    [events] => Array
                        (
                            [_a] => Array
                                (
                                    [date] => 02-09-2010
                                )

                            [_c] => Array
                                (
                                    [event] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [_c] => Array
                                                        (
                                                            [title] => Array
                                                                (
                                                                    [_v] => HTML Tags
                                                                )

                                                            [description] => Array
                                                                (
                                                                    [_v] => HTML Tags
                                                                )

                                                        )

                                                )

                                            [1] => Array
                                                (
                                                    [_c] => Array
                                                        (
                                                            [title] => Array
                                                                (
                                                                    [_v] => Another Title
                                                                )

                                                            [description] => Array
                                                                (
                                                                    [_v] => Another description
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

再次使用ary2xml()函数

您将获得所需的输出..

<?xml version="1.0" encoding="UTF-8"?>
    <xml>
    <settings>
    <title>Calendar2</title>
    <subTitle>Calendar2</subTitle>
    </settings>
    <events date="02-09-2010">
    <event>
    <title>HTML Tags</title>
    <description>HTML Tags</description>
    </event>
    <event>
    <title>Another Title</title>
    <description>Another description</description>
    </event>
    </events>
    </xml>

答案 2 :(得分:0)

$xml_str = file_get_contents($xmlfile);
$xml = new SimpleXMLElement($xml_str);
$wantedEventsTag = $xml->xpath('/xml/events[@date="'.$date.'"]');
$wantedEventsTag = $wantedEventsTag [0];//since above fun will return an array
$event = $wantedEventsTag->addChild('event');
$event['id']=$id;   
$event->addChild('title', $title);
$event->addChild('description', $des);
file_put_contents($xmlfile, $xml->asXML());