Jump to content
xisto Community
Sign in to follow this  
farsiscript

Read Xml And Import Into Database question

Recommended Posts

hi all ,
i have one big problem
i need to know , how i can read xml file and then save strings in xml in database
for example its my xml

<?xml version="1.0"?> <numbers> <arash>1</arash> <kiarash>2</kiarash> </numbers>
i want my soruce read this xml file and then save <arash> string into arash field in mysql database
for example in this source arash string is " 1" and kiarash string is "2"
my fields in mysql are standard and we have 2 field with names "arash" and "kiarash"
please help me , if you know some tut about this subject
thanks :lol:

Share this post


Link to post
Share on other sites

I suppose you could use an fopen("xml.xml" "r"); statement and then use some explodes to get the numbers...

<?phpif(!$handle = fopen("file.xml", "w")){echo "Could not open the file.";exit;}if (!is_readable("file.xml")){echo "The file is not readable!";exit;}$contents = fread($handle, filesize($filename));$getarash = explode($contents, "<arash>");$getarash2 = explode($arash[1], "</arash");$arash = $getarash2[0];fclose($handle);?>


But, it is easier to just update the xml file from the database through PHP automatically with a cron job or something...

<?phpif(!$handle = fopen("somefile.xml", "w")){echo "Can't open the file";}if (!is_writable("somefile.xml")){echo "The file is not writable!";}$xml .= <<<EOF<?xml version="1.0"?><numbers> EOF;$conn = mysql_connect('localhost, 'dbuser', 'dbpass');mysql_select_db('dbname');$query = @mysql_query("SELECT number FROM `arash`");$arash = @mysql_fetch_assoc($query);$xml .= <<<EOF<arash>{$arash['number']}</arash>EOF;//Etc for kiarash$xml .= <<<EOF</numbers>EOF;if (fputs($handle, $xml) === FALSE)  {	echo "Cannot write to xml file";	exit; }fclose($handle);mysql_close($conn);?>

Edited by jebriggsy (see edit history)

Share this post


Link to post
Share on other sites

For reading XML with php you need to use something like this (this example even ads XSL stylesheet to XML)

$xmldoc = new DOMDocument(); $xmldoc->loadXML('yourxmlfile.xml'); $xsldoc = new DOMDocument(); $xsldoc->loadXML('yourstylesheet.xsl'); $xslt = new XSLTProcessor(); $xslt->importStylesheet($xsldoc); echo $xslt->transformToXML($xmldoc);

Just a thing i was wondering, does Xisto have support for XSLTProcessor?!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.