
July 3rd, 2002, 03:19 AM
|
 |
Gerbil
|
|
Join Date: Oct 2001
Location: In a Rotastak
|
|
Read the XML file into arrays then do sql statements. There may be another way (probably) but that way serves me best. Something like:
PHP Code:
$file = "newsXML.xml";
$newsitem = array();
$id = ""; $text = "";
function startElement($parser, $name, $attribs) {
global $id;
if ($name == 'NEWSITEM') {
$id = $attribs['ID'];
}
}
function endElement($parser, $name) {
global $id;
global $text;
global $newsitem;
if ($name == 'HEADLINE') {
$newsitem[$id][0] = $text;
$text = "";
}
if ($name == 'NEWSTEXT') {
$newsitem[$id][1] = $text;
$text = "";
}
}
function characterData($parser, $data) {
global $text;
$text .= $data;
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen($file, "r");
while ($data = fread($fp, 4096)) {
xml_parse($xml_parser, $data, feof($fp));
}
xml_parser_free($xml_parser);
That would give you an array of:
headline : $newsitem[id][0]
text of news item : $newsitem[id][1]
Then do a foreach()
to put it into a database.
|