By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,287 Members | 1,544 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,287 IT Pros & Developers. It's quick & easy.

using PHP & MySQL to Input XML data to database!

P: 1
Hi guys,

I need help. I am trying to extract data from XML file. I am using all the fucntions needed to parse the xml data every thing working fine, but what I am looking for is to take the string data in each element and enter it to database. for example

I have the following xml file "sitecontent.xml" .
--------------------------
<?xml version="1.0" encoding="UTF-8" ?>
- <content>
<link>http://www.web.com</link>
<title>Title name </title>
<description>This line will be description bla bla </description>
<keywords>This line will be all data ..............etc </keywords>
</content>
-------------------------
and my PHP code will be like this

----------------------
<?php
include ('dbconnet.php');
$parser=xml_parser_create();
xml_set_element_handler($parser,"start","stop");
xml_set_character_data_handler($parser,"char");
$fp=fopen("sitecontent.xml","r");
while ($data=fread($fp,4096))
{
xml_parse($parser,$data,feof($fp)) or
die (sprintf("XML Error: %s at line %d",
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser)));
}
function start($parser,$element_name, $attribs)
{
switch($element_name)
{
case "CONTENT":
echo "<p>-- CONTENT --</p><br/>";
break;
case "LINK":
echo "<B>Link:</B> ";
break;
case "TITLE":
echo "<B>Title:</B> ";
break;
case "DESCRIPTION":
echo "<B>Description:</B> ";
break;
case "KEYWORDS":
echo "<B>Keywords:</B> ";
Break;
}
}
function stop($parser,$element_name)
{
echo "<br /><br />";
}
function char($parser,$data)
{
echo $data;
}
xml_parser_free($parser);
----------------------------
The output is like this

-- CONTENT --


Link: http://www.web.com

Title: Title name

Description: This line will be description bla bla

Keywords: This line will be all data ..............etc


---------------
What I need to do is take link content which it is http://www.web.com insert it to my table where field name = link
same thing with the other "Title name " to field name Title , "This line will be descritoption bla bla " to discription filed and so on.

If you know any way of tools to do that I will be greaty apritiated.

Balean
Aug 1 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.