Home:ALL Converter>Store XML Data to MySQL

Store XML Data to MySQL

Ask Time:2012-02-07T19:41:43         Author:Manoj

Json Formatter

I have an XML file. I want to save all the data from the XML file to the database

The file structure of XML is like

<STORY>
<BYLINE>abc</BYLINE>
<STORYID>123456</STORYID>
</STORY>

The code for storing data to database that I am using is

$dom = new DOMDOcument();
    $dom->loadXML(equitymarketnews/$zname);
    $xpath = new DOMXpath($dom);
    $res = $xpath->query("//STORY/");
$allres = array();
foreach($res as $node){
        $result = array();
$byline = mysql_real_escape_string($node->getElementsByTagName("BYLINE")->item(0)->nodeValue);
$storyid = mysql_real_escape_string($node->getElementsByTagName("STORYID")->item(0)->nodeValue);

}
$sql12="insert into equitymarketnews values('$byline','$storyid')"; 
                        mysql_query($sql12);

I am getting nothing in my database. All values are blanks. Where am I going wrong? I think something is wrong with this line

$res = $xpath->query("//STORY/");

i want to story the data ie ABC and 12345 FROm XML File To Table in database

Author:Manoj,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/9175614/store-xml-data-to-mysql
Chris :

I don't really know what your question is but assuming that the code you posted does not work as you aspect, one thing i noticed is the insertion of the record:\n\n$sql12=\"insert into equitymarketnews values('$byline','$storyid','$pubdate','$author','$cat','$subcat','$titleline','$subtitleline,'$storymain','$flag')\"; \nmysql_query($sql12);\n\n\nshouldn't it be inside your foreach loop? Otherwise you won't get anything into your database.\n\nIn my opinion it should look something like that:\n\nforeach($res as $node){\n $result = array();\n $byline = mysql_real_escape_string($node->getElementsByTagName(\"BYLINE\")->item(0)->nodeValue);\n $storyid = mysql_real_escape_string($node->getElementsByTagName(\"STORYID\")->item(0)->nodeValue);\n $sql12=\"insert into equitymarketnews values('$byline','$storyid')\"; \n mysql_query($sql12);\n}\n",
2012-02-07T11:48:57
yy