2010-09-02 134 views
0

我有一个XML文件编辑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> 

回答

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()); 
1

SimpleXml可能会有所帮助

编辑:见样品代码here(#9 &#10)

+0

能解释更多关于它用一个例子 – Warrior 2010-09-02 05:22:59

+1

我这样做一个 $ xml = new SimpleXMLElement($ xml_str); $ event = $ xml-> events-> addChild('event'); $ event-> addChild('title','More Parser Stories'); $ event-> addChild('description','这是所有关于使它工作的人。'); file_put_contents($ xmlfile,$ xml-> asXML()); 我怎么能给条件 – Warrior 2010-09-02 06:02:35

+0

@THOmas,你解决了你的问题? – systemovich 2010-09-02 07:32:46

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>