开发者

Editing xml file

I have an xml file

<?xml version="1.0" encoding="UTF-8"?>
<xml>
<settings>
<title>Calendar2</title>
<subTitle>Calendar2</subTitle>
</settings>
<events date="02-09-2010">
<event>
<tit开发者_如何学Pythonle>HTML Tags</title>
<description>HTML Tags</description>
</event>
</events>
</xml>

How i can add another event inside events tag with respect to date i mean

<?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>


I am giving you 2 function one for xml2array conversion and another for array2xml conversion

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);
}

pass your xml to function xml2ary() you will get output as shown below

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
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

Then convert this array as how you want it.

for eg.

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
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

and again use ary2xml() function

You will get your desired output..

<?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>


SimpleXml might be helpful

Edit: see samples code here (#9 & #10)


$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());
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜