PHP XML parsing question
Posted: Tue Feb 21, 2006 6:26 am
Hi there,
Further to a previous post, I'm just simplying my question here. I am coding a PHP news system which you can upload an XML to via a webform and then extract to a MySQL database. Getting the file via a form is fine as will be writing to the database when I've got that far. The bit I can't do is parse the XML file. It looks a bit like this:
So assuming a user has attached this to a webform and clicked 'submit', how do I read out these values? I have written a script, but it doesn't work very well - I think it gets confused as the <body> tag can contain HTML. Here is my current script:
Any help or comments on this would be most appreciated. Many thanks.
Further to a previous post, I'm just simplying my question here. I am coding a PHP news system which you can upload an XML to via a webform and then extract to a MySQL database. Getting the file via a form is fine as will be writing to the database when I've got that far. The bit I can't do is parse the XML file. It looks a bit like this:
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XML Spy v4.4 U (http://www.xmlspy.com) -->
<?xml-stylesheet type="text/xsl" href="C:\Documents and Settings\News.xsl"?>
<News>
<NewsItem id="" date="">
<Title></Title>
<Where></Where>
<When></When>
<Introduction></Introduction>
<Body></Body>
</NewsItem>
<NewsItem id="" date="">
<Title></Title>
<Where></Where>
<When></When>
<Introduction></Introduction>
<Body></Body>
</NewsItem>
</News>Code: Select all
$fileatt = $_FILES['xmlfile']['tmp_name'];
$fileatt_type = $_FILES['xmlfile']['type'];
$fileatt_name = $_FILES['xmlfile']['name'];
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
preg_match_all ("/<NEWS>.*<\/NEWS>/Uis", $data, $matches);
$matches[0][0]=str_replace("<Body>",'<BODY><![CDATA[',$matches[0][0]);
$matches[0][0]=str_replace("</Body>",']]></BODY>',$matches[0][0]);
$matches[0][0] = preg_replace("/(\r\n|\n|\r)/", "", $matches[0][0]);
// Open the file and erase the contents if any
$fp = fopen("temp.xml", "w");
// Write the data to the file
fwrite($fp, $matches[0][0]);
// Close the file
fclose($fp);
if (!($fp=@fopen("temp.xml", "r"))) die ("Couldn't open XML.");
$usercount=0;
$userdata=array();
$state='';
function startElementHandler ($parser,$name,$attrib){
global $usercount;
global $userdata;
global $state;
switch ($name) {
case $name=="NewsItem" : {
$userdata[$usercount]["id"] = $attrib["id"];
$userdata[$usercount]["date"] = $attrib["date"];
break;
}
default : {$state=$name;break;}
}
}
function endElementHandler ($parser,$name){
global $usercount;
global $userdata;
global $state;
$state='';
if($name=="NEWSITEM") {$usercount++;}
}
function characterDataHandler ($parser, $data) {
global $usercount;
global $userdata;
global $state;
if (!$state) {return;}
if ($state=="TITLE") { $userdata[$usercount]["Title"] = $data;}
if ($state=="WHERE") { $userdata[$usercount]["Where"] = $data;}
if ($state=="WHEN") { $userdata[$usercount]["When"] = $data;}
if ($state=="INTRODUCTION") { $userdata[$usercount]["Introduction"] = $data;}
if ($state=="BODY") { $userdata[$usercount]["Body"] = $data;}
if ($state=="ABOUT") { $userdata[$usercount]["About"] = $data;}
}
if (!($xml_parser = xml_parser_create())) die("Couldn't create parser.");
xml_set_element_handler( $xml_parser, "startElementHandler", "endElementHandler");
xml_set_character_data_handler( $xml_parser, "characterDataHandler");
while( $data = fread($fp, filesize("temp.xml"))){
if(!xml_parse($xml_parser, $data, feof($fp))) {
break;}}
xml_parser_free($xml_parser);
for ($i=0;$i<$usercount; $i++)
{
echo "ID: ".$userdata[$i]["id"]." Date: ".ucfirst($userdata[$i]["date"])."<br><br>";
if ($userdata[$i]["Title"]) {echo "<h1>".$userdata[$i]["Title"]."</h1>";}
if ($userdata[$i]["Where"]) {echo "Where: ".$userdata[$i]["Where"]."<br>";}
if ($userdata[$i]["When"]) {echo "When: ".$userdata[$i]["When"]."<br>";}
if ($userdata[$i]["Introduction"]) {echo $userdata[$i]["Introduction"]."<br>";}
if ($userdata[$i]["Body"]) {echo $userdata[$i]["Body"]."<br>";}
if ($userdata[$i]["About"]) {echo $userdata[$i]["About"]."<br>";}
}Any help or comments on this would be most appreciated. Many thanks.