0

hey there i have an xml file that has what i need to do is to add an event with same location name A

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

    </location>
    </timetable>

so it can be like this

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

i got stuck in my php code where i want to cheack if the event is created or not if its created modify my xml with name of the event and add the new one

this is my full php inserting code `

           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'); `
6
  • 1
    Hi Amer, welcome to SO. Here, you are welcome to try to solve the problem, show what you've tried and get support from very competent people. So look into simplexmland give it a try! Commented May 19, 2013 at 23:57
  • read file -> parse -> modify -> write file. pretty much like that. Commented May 19, 2013 at 23:58
  • Why are you using MySQL for this? XML is perfectly functional for storing information. After all, that's what it does. Commented May 20, 2013 at 0:36
  • You said you want to check if the event was created... in mysql or in XML? Commented May 20, 2013 at 0:39
  • yes it saves perfectly but when i want to add a new events under an exciting location , so i can modify the 2nd chiled 'events ; after that Commented May 20, 2013 at 0:42

2 Answers 2

1

Amer, rather than creating the XML from strings, I'd use the simplexml methods:

Inserting a new <event> in existing 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");

see it working: http://codepad.viper-7.com/12xtVD

Sign up to request clarification or add additional context in comments.

1 Comment

thank you man you just made my day I've been struggling with my code since the morning :)
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.

9 Comments

Max, I appreciate your code-example, and I think he should first try something himself...
How do you mean, he should try something himself first?
1) You can directly open the content with just one line DOMDocument::load($pathToFile); 2) the last 3 lines are a little bit confusing, and finally you are not adding the new node to the xml tree. Try to give a complete working example in order to help...
thanks max for you kind replay iv been trying to find a way to modify an exciting xml file in a 2nd node , @michi trust me iv been trying since the morning
Hey, I didn't mean to get into an argument, sorry bout that.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.