Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Read Xml And Import Into Database, question
farsiscript
post Mar 27 2007, 12:05 PM
Post #1


Super Member
*********

Group: Members
Posts: 357
Joined: 8-April 06
Member No.: 21,487



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
CODE
<?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 wink.gif
Go to the top of the page
 
+Quote Post
jebriggsy
post Mar 29 2007, 12:50 PM
Post #2


Newbie [Level 1]
*

Group: Members
Posts: 14
Joined: 29-March 07
Member No.: 40,791



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

CODE
<?php
if(!$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...

CODE
<?php
if(!$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);


?>


This post has been edited by jebriggsy: Mar 29 2007, 12:54 PM
Go to the top of the page
 
+Quote Post
matak
post Mar 29 2007, 08:58 PM
Post #3


Super Member
*********

Group: Members
Posts: 413
Joined: 4-October 06
From: Psychedelic Realms
Member No.: 31,079



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

CODE
$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 trap17 have support for XSLTProcessor?!
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Chatbox / Guestbook(2)
  2. Mysql Edit(4)
  3. Writing To Database Problem With Mysql - Not Writing Forum Post(3)
  4. Help Importing Mysql Database Results(3)
  5. Personal Blog(4)
  6. Script That Deletes A Row In A Mysql Database Usin(1)
  7. Database Extraction Layer(1)
  8. Is Doing The Whole Database / Php Thing Worth It?(13)
  9. Can You Add Images Into A Mysql Database?(20)
  10. Login / Authetication System Using Database(4)
  11. Crontab Automated Database Backup(2)
  12. Showing Numbers Of Mysql Entries In A Database Table(7)
  13. Code To Install Mysql Database Table?(5)
  14. File In Database(1)
  15. Problem To Join Tables In Database(3)
  1. Php Search Engine Script For Mysql Database(11)
  2. Importing .csv Into Mysql Database(6)
  3. Editing Information In A Mysql Database And Deleting Rows(5)
  4. Download Database Backup(0)
  5. Auto Pruning An Sql Database With Php(4)
  6. Can Database Column Names Start With A Number?(1)
  7. Connecting Php Site To Database(6)
  8. How Do I Connect To Live Database With Php Script?(6)
  9. Ms-access Database Question(3)


 



- Lo-Fi Version Time is now: 11th October 2008 - 08:41 PM