eeekin_07 Posted April 18, 2007 Share Posted April 18, 2007 Hi, Iam really new in php and mysql..and i got i project dat need to load data from .xml.. can anyone help me in this? i got many types of codes in internet but wat types of codes should i used? anyone xperienced in this? could u help me? plis.. Quote Link to comment Share on other sites More sharing options...
maxim Posted April 18, 2007 Share Posted April 18, 2007 i actualy just did this. basicly i use php 5. it has the simpleXML extension. i iterate throgh the xml file and store the values i want into one array, like so $movielist = simplexml_load_file('http://www.path to XML file'); //retrive the array of movie titles from the xml file foreach ($movielist->movie as $movie) { $array[] = $movie->title; } now all my values i want are in the $array variable. now ill chuck them into a database (assuming one is already created and has a table to put the data into) foreach ($array as $xml_title) { $sql = "INSERT INTO movie_list (title,date) VALUES(\"$xml_title\",\"$timestamp\");"; sqlite_query($dbase_connection,$sql); } for mysql specific functions - i find this book healpfull http://www.hudzilla.org/php/ i hope this helps edit, if the $timestamp var throws you off its just a simple $timestamp = time(); Quote Link to comment Share on other sites More sharing options...
eeekin_07 Posted April 18, 2007 Author Share Posted April 18, 2007 Thanx maxim, i've tried using your code an it is only success if the xml file like this: <TransactionMessage> <TransactionHeader> <ServiceName>KSM - Application for Incentive Under the Programme to Encourage Malaysian Citizens with Expertise Residing Overseas to Return to Malaysia</ServiceName> <Version>1.0</Version> <AgencyIdentifier>fksm</AgencyIdentifier> <TransactionDate>11/04/2007 14:20:14</TransactionDate> <PSPTransactionID>39265</PSPTransactionID> <PSPReferenceNumber>070411-00039265</PSPReferenceNumber> </TransactionHeader> </TransactionMessage> the problem is my xml stucture have 2 diff header: eg: <TransactionMessage> - <TransactionHeader> <ServiceName>KSM - Application for Incentive Under the Programme to Encourage Malaysian Citizens with Expertise Residing Overseas to Return to Malaysia</ServiceName> <Version>1.0</Version> <AgencyIdentifier>fksm</AgencyIdentifier> <TransactionDate>11/04/2007 14:20:14</TransactionDate> <PSPTransactionID>39265</PSPTransactionID> <PSPReferenceNumber>070411-00039265</PSPReferenceNumber> </TransactionHeader> - <TransactionData> - <KSM_IncentiveProgrammeEN> <?jetform ^Dat ^page Page1?> <FullName>LAY YUN</FullName> <newic1Applicant>770911</newic1Applicant> <newic2Applicant>10</newic2Applicant> </KSM_IncentiveProgrammeEN> </TransactionData> </TransactionMessage> can U and anyone help me... Quote Link to comment Share on other sites More sharing options...
jitesh Posted April 18, 2007 Share Posted April 18, 2007 <?php // Author: Koolb, Buddhima W.Wickramsinghe (buddhima@yahoo.com) $arfiles= array("basic2.xml","dataset.xml"); foreach($arfiles as $arFile) { echo "<BR>-------------------------------------------------<BR>"; echo "<BR>Parsing ".$arFile."<BR>"; $insXmlParser= new clsXmlParser($arFile); if($aArray=$insXmlParser->Parse()) { echo "<pre>"; print_r($aArray); //echo LIST_CONTENTS($aArray); } echo "<BR>-------------------------------------------------<BR>"; ##### // YOU WILL GET DATA HERE IN ARRAY FORMAT NOW YOU CAN SAVE DATA INTO MYSQL ####### } // Simple XML Parser class clsXmlParser { // general vars var $sTitle = ""; var $sLink = ""; var $sDescription = ""; var $arItems = array(); var $arsub = array(); var $itemCount = 0; var $prvTag=""; var $uFiles = ''; var $xml_parser; var $curTag=""; function clsXmlParser($uFiles) { $this->uFiles = $uFiles; } function startElement($parser, $name, $attrs) { $this->curTag .= "^$name"; //echo "start: ".$this->curTag." <BR>"; } function endElement($parser, $name) { $caret_pos = strrpos($this->curTag,'^'); $this->curTag = substr($this->curTag,0,$caret_pos); //echo "end: ".$this->curTag." <BR>"; } function characterData($parser, $data) { if(trim($data) != "") { if(trim($this->prvTag) == "") $this->prvTag=$this->curTag; elseif(trim($this->prvTag) == trim($this->curTag)) { $this->arItems[] = $this->arsub; $this->arsub = array(); } //find current element $c_pos = strrpos($this->curTag,'^'); $c_len = strlen($this->curTag); $c_val = substr($this->curTag,($c_pos+1),$c_len); //set data to sub array with the element name as the key $this->arsub[$c_val] = $data; } } function Parse() { $this->xml_parser = xml_parser_create(); xml_set_object($this->xml_parser, &$this); xml_set_element_handler($this->xml_parser, "startElement", "endElement"); xml_set_character_data_handler($this->xml_parser, "characterData"); if (!($fp = fopen($this->uFiles,"r"))) { die ("could not open XML for input"); } while ($data = fread($fp, 4096)) { if (!xml_parse($this->xml_parser, $data, feof($fp))) { die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($this->xml_parser)), xml_get_current_line_number($this->xml_parser))); } } xml_parser_free($this->xml_parser); //to handle the last array element if(count($this->arsub)>0) { $this->arItems[] = $this->arsub; $this->arsub = array(); } return $this->arItems; } } ///----------END OF CLASS function LIST_CONTENTS($arrayname,$tab="    ",$indent=0) { // recursively displays contents of the array and sub-arrays: // This function (c) Peter Kionga-Kamau // Free for unrestricted use, except sale - do not resell. // use: echo LIST_CONTENTS(array $arrayname, string $tab, int $indent); // $tab = string to use as a tab, $indent = number of tabs to indent result $retval=$currenttab=""; while(list($key, $value) = each($arrayname)) { for($i=0; $i<$indent; $i++) $currenttab .= $tab; if (is_array($value)) { $retval .= "$currenttab$key : Array: <BR>$currenttab{<BR>"; $retval .= LIST_CONTENTS($value,$tab,$indent+1)."$currenttab}<BR>"; } else $retval .= "$currenttab$key => $value<BR>"; $currenttab = NULL; } return $retval; } ?> Quote Link to comment Share on other sites More sharing options...
eeekin_07 Posted April 19, 2007 Author Share Posted April 19, 2007 Dear Jitesh and maxim thanx 4 trying to helping me to solve my problem the xml record already can be loaded now thanx guys... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.