homepage Welcome to WebmasterWorld Guest from 54.198.139.141
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Website
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
PHP XML Parser into .sql file
alpacinofr




msg:3660618
 7:33 am on May 28, 2008 (gmt 0)

Hi all

I need to do XML import in MySQL database. I have found this script and it reads and parse my XML file to display it in browser.

How can I modify code to write results into SQL style format file with INSERT statements...

Thanks


<?php

$file = "sodim.xml";

function contents($parser, $data){
echo $data;
}

function startTag($parser, $data){
echo "<b>";
}

function endTag($parser, $data){
echo "</b><br />";
}

$xml_parser = xml_parser_create();

xml_set_element_handler($xml_parser, "startTag", "endTag");

xml_set_character_data_handler($xml_parser, "contents");

$fp = fopen($file, "r");

$data = fread($fp, 80000);

if(!(xml_parse($xml_parser, $data, feof($fp)))){
die("Error on line " . xml_get_current_line_number($xml_parser));
}

xml_parser_free($xml_parser);

fclose($fp);

?>

 

ahmedtheking




msg:3661913
 2:48 pm on May 29, 2008 (gmt 0)

You need to focus on these functions:

function startTag($parser, $data){
echo "<b>";
}

function endTag($parser, $data){
echo "</b><br />";
}

xml_set_element_handler($xml_parser, "startTag", "endTag");

As xml_set_element_handler() tells PHP how to handle each element. So, if you rewrote them so that instead of putting the data inside <b></b>, it put them inside '' and added the VALUES and other SQL, you'd be there!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved