繁体   English   中英

使用php更新xml文件

[英]updating xml file using php

嘿,我有一个xml文件,我需要做的是添加一个具有相同位置名称A的事件

  <timetable>
  <location name="A" >
         <event >
                  <title>EVENT TITLE </title>
                  <subtitle>Amman -text </subtitle>
                  <description>Amman -text </description>
         </event>

    </location>
    </timetable>

所以它可以是这样的

<timetable>
  <location name="A" >
         <event >
                  <title>EVENT TITLE </title>
                  <subtitle>Amman -text </subtitle>
                  <description>Amman -text </description>
         </event>
         <event >
                  <title>EVENT TITLE </title>
                  <subtitle>Amman -text </subtitle>
                  <description>Amman -text </description>
         </event>

    </location>
    </timetable>

我陷入了我的PHP代码,如果事件被创建,我想要解决,如果它创建了修改我的xml与事件的名称并添加新的事件

这是我的完整php插入代码`

           if(isset($_GET['coname'])){

          $coid = $_GET['id'];

          $cname=$_GET['coname'];

          $title = $_POST ['title'];
          $sub = $_POST ['sub'];
          $description = $_POST ['description'];
          $location = $_POST ['location'];
          $event = $_POST ['event'] ;

          $str =$_POST ['str'] ;
          $end =$_POST ['end'] ;
          $topic = $_POST ['topic'] ;


          $sql="INSERT INTO timeline (title,sub,description,location,event,str,end,topic,coid)
          VALUES
          ('$title','$sub','$location','$location','$event','$str','$end','$topic','$coid')";
          if (!mysqli_query($con,$sql))
            {
            die('Error: ' . mysqli_error($con));
            }
          echo "1 record added";

          $q = mysqli_query($con,"SELECT * FROM timeline where coid = $coid") or  die(mysqli_error());

          $xml = '<timetable start="'.$st.'" end="'.$en.'" interval="'.$in.'"                                title="'.$da.'">';
          while($r = mysqli_fetch_array($q)){
              $loc=$r['topic'];
              $evns=$r['str'];
              $evne= $r['end'];

               $xml .= '<location name="'.$loc.'" subtext=" ">';
            $xml .= '<event start="'.$evns.'" end="'.$evne.'">';
            $xml .= "<title>".$r['title']."</title>";
            $xml .= "<subtitle>".$r['location']."</subtitle>";  
            $xml .= "<description>".$r['description']."</description>";    
             $xml .= "</event>"; 

            $xml .= "</location>";  

                 }

                 $xml .= "</timetable>";

              $sxe = new SimpleXMLElement($xml);

             $sxe->asXML('xml/'.$cname.'.xml'); `

据我所知,你想创建一个子元素。 这就是我要这样做的方式:

PHP:

$eleone=$_POST['elementone'];
           $eletwo=$_POST['elementtwo'];
           $file = "verbs.xml";
           $openf = fopen($file, "c+") or die ("Cannot open file");
           $str = fread ($openf, filesize($file));
           $xml = new DOMDocument('1.0', 'iso-8859-1');
           $xml->formatOutput=TRUE;
           $xml->preserveWhiteSpace = FALSE;
           $xml ->loadXML($openf) or die ("There has been an error with opening the XML file. Our team has been notified and will start working to fix this problem.");

           //this is the original document
           echo "<xmp>OLD:\n". $xml->saveXML(). "<xmp>";

           //this is how you get the document element
           $root= $xml ->documentElement;
           $firstnode= $root->firstChild;

           //good stuff right here; how to make a node
           $ori= $firstnode->childNodes->item(2);
           $eleadd= $xml->createElement($elementone);
           $eleaddt= $xml->createTextNode($//what gets shown in $eleadd );
           $eleadd->appendChild("$idt");

如果你不想要所有这些,你可以删除一些非关键的东西,比如父元素。 如果您需要更多信息,可以访问http://www.phpeveryday.com/articles/PHP-XML-Adding-XML-Nodes-P414.html ,或者找到我的信息。

Amer,而不是从字符串创建XML,我使用simplexml方法:

在现有XML中插入新的<event>

$xml = simplexml_load_string($x); // assume XML in $x
$loc = $xml->xpath("location[@name = 'A']")[0]; // select <location> with name = A 
$event = $loc->addChild("event");
$event->addAttribute("start", "2013-05-20 10:00:00");
$event->addAttribute("end", "2013-05-20 14:30:00");
$event->addChild("title", "some title");
$event->addChild("subtitle", "some subtitle");
$event->addChild("description", "some description");

看它工作: http//codepad.viper-7.com/12xtVD

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM