i am having some xml file i want to parse it and store it in mysql

Discussion in 'PHP' started by kailashr2k, Jun 6, 2008.

  1. #1
    I am having some xml file and i want to parse the XML data and store it in corresponding fields in the mysql table can any one please sort my problem..

    Thank u in advance
     
    kailashr2k, Jun 6, 2008 IP
  2. Lordo

    Lordo Well-Known Member

    Messages:
    2,082
    Likes Received:
    58
    Best Answers:
    0
    Trophy Points:
    190
    #2
    Lordo, Jun 6, 2008 IP
  3. mehmetm

    mehmetm Well-Known Member

    Messages:
    134
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #3
    mehmetm, Jun 6, 2008 IP
  4. kailashr2k

    kailashr2k Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I try all the think but i cant get the data to store it to the database.


    $xmlDoc->load("xm URl");


    $books = $xmlDoc->documentElement;

    $i=0;

    foreach ($books->childNodes As $child_node)
    {
    $i++;
    $book1 = $book->nodeName;

    // echo $book1;

    if($child_node->hasChildNodes())
    {

    $book_name = $child_node->nodeName;

    echo $book_name;

    if($book_name == "AirportName")
    {

    $data_array[] = $child_node->nodeValue;

    if($book_name == "Location")
    {

    $data_array[] = $child_node->nodeValue;

    if($book_name == "AirportOverview")
    {

    $data_array[] = $child_node->nodeValue;

    if($book_name == "ByBus")
    {

    $data_array[] = $child_node->nodeValue;

    if($book_name == "ByTaxi")
    {

    $data_array[] = $child_node->nodeValue;


    if($book_name == "ByTrain")
    {

    $data_array[] = $child_node->nodeValue;


    if($book_name == "PhoneNum")
    {

    $data_array[] = $child_node->nodeValue;


    if($book_name == "Website")
    {

    $data_array[] = $child_node->nodeValue;

    }
    }
    }
    }
    }
    }
    }
    }
    }

    $boo = mysql_escape_string($data_array[0]);

    echo $boo . "</br>";

    echo $data_array[1] . "</br>";

    echo $data_array[2] . "</br>";

    echo $data_array[3] . "</br>";

    echo $data_array[4] . "</br>";

    echo $data_array[5] . "</br>";

    echo $data_array[6] . "</br>";

    echo $data_array[7] . "</br>";
    $sql = "INSERT INTO adetails VALUES('$data_array[0]','$data_array[1]','$data_array[2]','$data_array[3]','$data_array[4]','$data_array[5]','$data_array[6]','$data_array[7]')";
    $q = mysql_query($sql);
    unset($data_array);

    // }

    }


    // $sql = "INSERT INTO adetails (name,location,airportoverview,bybus,bycar,bytrain,phno,website)
    // VALUES('$data_array[0]','$data_array[1]','$data_array[2]','$data_array[3]','$book','$data_array[5]','$data_array[6]','$data_array[7]')";
    // $q = mysql_query($sql);

    mysql_close($connect);
    ?>
     
    kailashr2k, Jun 7, 2008 IP
  5. graham23s

    graham23s Well-Known Member

    Messages:
    188
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    105
    #5
    or use the load string:

    
     // xml testing //
     $xml_file = "5300538.xml";
     
     // load the nzb //
     $xml = simplexml_load_file("$xml_file");
     
    // test //
    foreach ($xml as $details)
    {
      ###############################################
      # Put the details into variables
      ###############################################
      $n_date = $details['date'];
      $n_poster = $details['poster'];
      $n_subject = $details['subject'];
      
      print("<b>Date</b> - ") . $n_date;
      print("<br />");
      print("<b>Posted By</b> - ") . $n_poster;
      print("<br />");
      print("<b>Subject</b> - ") . $n_subject;
      print("<br />");
    
      // another foreach for the group //
      foreach($details->groups->group as $group)
      {
      print("<b>Group</b> - ") . $group;
      } 
    }
    PHP:
    this can be modified for your needs though.

    Graham
     
    graham23s, Jun 7, 2008 IP