0

嘿,我有一个 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'); `
4

2 回答 2

1

From what I understand, you want to create a child element. This is how I would go about doing this:

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

If you don't want all of this, you may be able to delete some non-crucial things like the parent elements. If you need more information, http://www.phpeveryday.com/articles/PHP-XML-Adding-XML-Nodes-P414.html is the place to go, or where I found my information.

于 2013-05-20T00:03:39.680 回答
1

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

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

$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

于 2013-05-20T00:56:31.437 回答