• Sign in to Neowin Faster!

    Create an account on Neowin to contribute and support the site.

  • 0
Sign in to follow this  

Importing data from XML file to database using php

Question

seb86    0

I am attempting to import an xml file that gets updated every few hours with new content.

I have most of it working well until i get to a little problem.

This is an example of the XML file I have been given by a client.

<?xml version="1.0" encoding="ISO-8859-1"?>

<annonce>

<reference>123456789</reference>
<username>test123</username>
<address>42 Rue Alexandre Cabanel</address>
<village>Herault</village>
<area>Montpellier</area>
<section>Bord de Mer</section>
<postcode>34000</postcode>

<liste_photos>
<photo>http://photos.domain.com/99999/999991_1.jpg</photo>
<photo>http://photos.domain.com/99999/999991_2.jpg</photo>
<photo>http://photos.domain.com/99999/999991_3.jpg</photo>
<photo>http://photos.domain.com/99999/999991_4.jpg</photo>
<photo>http://photos.domain.com/99999/999991_5.jpg</photo>
<photo>http://photos.domain.com/99999/999991_6.jpg</photo>
<photo>http://photos.domain.com/99999/999991_7.jpg</photo>
<photo>http://photos.domain.com/99999/999991_8.jpg</photo>
<photo>http://photos.domain.com/99999/999991_9.jpg</photo>
<photo>http://photos.domain.com/99999/999991_10.jpg</photo>
</liste_photos>

</annonce>

I am required to import the information into a database. This is what I have written and works.

<?php
$file = "http://www.domain.com/example.xml";
// Fetch content from xml file.
$content = get_content($file);
// Break it into an array.... !
$content_x = explode("<annonce>", $content);
reset($content_x);
// Iterate through each item in the array. It's everything between <annonce> and </annonce>!
foreach($content_x as $item){
	/* Reference Number */
	$p1 = strpos($item, '<reference>');
	$p2 = strpos($item, '</reference>');
	$l1 = $p2 - $p1;
	$reference = substr($item, $p1, $l1); // pulls out everything between <reference>and</reference>
	$reference = str_replace("<reference>", "", $reference); // removes the remaining tag...
	// and so on with each item between <annonce> and </annonce>
	// Find Username
	$p1 = strpos($item, '<username>');
	$p2 = strpos($item, '</username>');
	$m1 = $p2 - $p1;
	$userID = substr($item, $p1, $m1);
	$userID = str_replace("<username>", "", $userID);
	/* Address */
	$p1 = strpos($item, '<address>');
	$p2 = strpos($item, '</address>');
	$m1 = $p2 - $p1;
	$address = substr($item, $p1, $m1);
	$address = str_replace("<address>", "", $address);
	// Village
	$p1 = strpos($item, '<village>');
	$p2 = strpos($item, '</village>');
	$m1 = $p2 - $p1;
	$village = substr($item, $p1, $m1);
	$village = str_replace("<village>", "", $village);
	// Area
	$p1 = strpos($item, '<area>');
	$p2 = strpos($item, '</area>');
	$m1 = $p2 - $p1;
	$department = substr($item, $p1, $m1);
	$department = str_replace("<area>", "", $department);
	// Postcode
	$p1 = strpos($item, '<postcode>');
	$p2 = strpos($item, '</postcode>');
	$m1 = $p2 - $p1;
	$postcode = substr($item, $p1, $m1);
	$postcode = str_replace("<postcode>", "", $postcode);

	$checkQuery = mysql_query("SELECT * FROM `property` WHERE `username`='{$username}' AND `reference_number`='{$reference}'");
	/* Check to see if property already exists in the database under the user in control of the property. */
	$countProps = mysql_num_rows($checkQuery);
	/* If property is new then data will be properly formatted and inserted into the database. */
	if($countProps < 1){
		$id = mysql_insert_id(); // Generate new id for property.
		$dateentered = date("Y-m-d H:i:s"); // Inserts date of entry.
		$address = ucwords(strtolower($address)); // Puts the string all lowercase then capitalizes the first letter of each word.
		$village = ucwords(strtolower($village)); // Puts the string all lowercase then capitalizes the first letter of each word.
		$department = ucwords(strtolower($department)); // Puts the string all lowercase then capitalizes the first letter of each word.
		/* Add Property from Feed. */
		$addProperty = "INSERT INTO properties (`property_id`, `username`, `reference_number`, `date_entered`, `address`, `village`, `department`, `postcode`) VALUES ('$id', '$username', '$reference', '$dateentered', '$address', '$village', '$department', '$postcode');";
		mysql_query($addProperty) or die("ERROR INSERTING PROPERTY DETAILS: ".mysql_error());

		$photos = get_content($file);
		$content_photos = explode("<liste_photos>", $photos);
		reset($content_photos); // Reset list of photos
		/* Fetch each photo. */
		foreach($content_photos as $key => $item){
			$p1 = strpos($item, '<photo>');
			$p2 = strpos($item, '</photo>');
			$m1 = $p2 - $p1;
			$photoFile = substr($item, $p1, $m1);
			$photoFile = str_replace("<photo>", "", $photoFile);
			// Fetch photo file size.
			$ch = curl_init($photoFile);
			curl_setopt($ch, CURLOPT_NOBODY, true);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($ch, CURLOPT_HEADER, true);
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
			$data = curl_exec($ch);
			curl_close($ch);
			if(preg_match('/Content-Length: (\d+)/', $data, $matches)){ $contentLength = (int)$matches[1]; }
			$photoSize = $contentLength; // Collects the filesize of the image.
			// Fetch photo width and height.
			$photoDetails = @getimagesize($photoFile); // Gets details about the photo.
			$width = $photoDetails[0];
			$height = $photoDetails[1];
			// Clean the photo file name.
			$photoFile = str_replace('http://photos.domain.com/', '', $photoFile);
			$photoFile = str_replace($userID.'/', '', $photoFile); // Removes the username directory.
			$photoFile = stripslashes($photoFile); /* Removes any back slashes in the file name */
			$photoFile = preg_replace('[^a-zA-Z0-9.]', '', $photoFile); /* Allows characters and numbers only. */
			$photoFile = strtolower($photoFile); /* Puts the file name all in lowercase. */
			// Add a new photo id number.
			$photoID = mysql_insert_id();
			/* Add Photos of the Property. */
			if($key > '1'){
				$addphoto = "INSERT INTO photos (`property_id`, `photo_id`, `username`, `filename`, `filesize`, `width`, `height`, `position`) VALUES ('$id', '$photoID', '$username', '$photoFile', '$photoSize', '$width', '$height', '$key')";
				$sqlphoto = mysql_query($addphoto) or die("ERROR INSERTING PROPERTY PHOTO: ".mysql_error());
			}
		}
	}
}
// Close the foreach loop
mysql_close(); // and close the database connection.
?>

The problem is when it comes to inserting each photo, it only does the first one and not the other 9.

I need the other 9 to insert as well. How do i achieve this?

The xml file export can not be changed according to the client.

Can any one help me with this problem.

Any help would be most appreciated thank you.

Share this post


Link to post
Share on other sites

21 answers to this question

Recommended Posts

  • 0
Gocom    11

I, personally, would just use a framework made for reading XML files or a parser.

Anyway, the problem is that you aren't looping thru the list of <photo>s. You are splitting the file from the start of the <liste_photos> block and then looping thru that, selecting just the first item of the actual list of <photo>s. Instead of exploding the <liste_photos>, after just getting the list only, explode with <photo> and then do almost all the same. For example:

$photos = explode('&lt;photo&gt;',$content_photos);
unset($photos[0]);
foreach($photos as $photo) {
    $photoFile = str_replace('&lt;/photo&gt;','',$photo);
    /* The rest [...] */
}

This is an example of the XML file I have been given by a client.

Start by escaping the data you use in the queries (mysql_real_escape_string()). One wrong character in the XML file and the whole thing breaks. You don't want to do that to the client.

// Fetch photo file size.

Make sure that the system can handle that. Repeatedly opening connections and checking huge chunks of data.

$content = get_content($file);

$photos = get_content($file);

Fetching is there twice. Just do it once. Reading is always slower than reusing already fetched data.

Share this post


Link to post
Share on other sites
  • 0
The_Decryptor    1,105

Yeah, that amount of string manipulation would be slower than proper XML parsing, and more fragile (PHP includes an XML parser, and there are 3rd party ones with different APIs)

The inbuilt one allows you to use XPath to get the nodes, that would reduce the actual XML parsing and reading code down to like 5-6 lines.

Share this post


Link to post
Share on other sites
  • 0
AnthonySterling    11

Here's a sample of how to iterate the XML using SimpleXML. ;)

&lt;?php
$xml = new SimpleXMLElement(
 'http://www.domain.com/example.xml',
 null,
 true
);

printf(
 '&lt;h3&gt;Photos for %s&lt;/h3&gt;' . "\n",
 $xml-&gt;username
);

foreach($xml-&gt;liste_photos-&gt;photo as $photo){
 printf(
	'&lt;img src="%s" alt="#" /&gt;' . "\n",
	$photo
 );
}

/*
 &lt;h3&gt;Photos for test123&lt;/h3&gt;
 &lt;img src="http://photos.domain.com/99999/999991_1.jpg" alt="#" /&gt;
 &lt;img src="http://photos.domain.com/99999/999991_2.jpg" alt="#" /&gt;
 &lt;img src="http://photos.domain.com/99999/999991_3.jpg" alt="#" /&gt;
 &lt;img src="http://photos.domain.com/99999/999991_4.jpg" alt="#" /&gt;
 &lt;img src="http://photos.domain.com/99999/999991_5.jpg" alt="#" /&gt;
 &lt;img src="http://photos.domain.com/99999/999991_6.jpg" alt="#" /&gt;
 &lt;img src="http://photos.domain.com/99999/999991_7.jpg" alt="#" /&gt;
 &lt;img src="http://photos.domain.com/99999/999991_8.jpg" alt="#" /&gt;
 &lt;img src="http://photos.domain.com/99999/999991_9.jpg" alt="#" /&gt;
 &lt;img src="http://photos.domain.com/99999/999991_10.jpg" alt="#" /&gt;
*/
?&gt;

Share this post


Link to post
Share on other sites
  • 0
seb86    0

I, personally, would just use a framework made for reading XML files or a parser.

Anyway, the problem is that you aren't looping thru the list of <photo>s. You are splitting the file from the start of the <liste_photos> block and then looping thru that, selecting just the first item of the actual list of <photo>s. Instead of exploding the <liste_photos>, after just getting the list only, explode with <photo> and then do almost all the same. For example:

$photos = explode('&lt;photo&gt;',$content_photos);
unset($photos[0]);
foreach($photos as $photo) {
    $photoFile = str_replace('&lt;/photo&gt;','',$photo);
    /* The rest [...] */
}

Start by escaping the data you use in the queries (mysql_real_escape_string()). One wrong character in the XML file and the whole thing breaks. You don't want to do that to the client.

Make sure that the system can handle that. Repeatedly opening connections and checking huge chunks of data.

Fetching is there twice. Just do it once. Reading is always slower than reusing already fetched data.

I have made many adjustments and they work better now thanks to you but now I have another little problem.

I only get the width and height of the last photo and not the others. Everything else works fine.

Here is my latest code.

$photos = explode("&lt;photo&gt;", $content);
	unset($photos[0]);
	/* Fetch each photo. */
	foreach($photos as $key =&gt; $photo){
		if($key == '10'){
			/* Clean content after the last photo from list of photos. */
			$photoFile = str_replace("&lt;/photo&gt;", "", $photo);
			$p1 = strpos($photo, '&lt;photo&gt;');
			$p2 = strpos($photo, '&lt;/photo&gt;');
			$m1 = $p2 - $p1;
			$photo = substr($photo, $p1, $m1);
			$photoFile = strip_tags($photo); /* Removes any HTML Tags. */
		}
		else{ $photoFile = str_replace("&lt;/photo&gt;", "", $photo); }
		// Fetch photo file size.
		$ch = curl_init($photoFile);
		curl_setopt($ch, CURLOPT_NOBODY, true);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HEADER, true);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		$data = curl_exec($ch);
		curl_close($ch);
		if(preg_match('/Content-Length: (\d+)/', $data, $matches)){ $contentLength = (int)$matches[1]; }
		$photoSize = $contentLength; // Collects the filesize of the image.
		// Fetch photo width and height.
		$photoDetails = @getimagesize($photoFile); // Gets details about the photo.
		$width = $photoDetails[0];
		$height = $photoDetails[1];
		// Clean the photo file name.
		$photoFile = str_replace('http://photos.domain.com/', '', $photoFile);
		$photoFile = str_replace($agenceID.'/', '', $photoFile); // Removes the username directory.
		$photoFile = stripslashes($photoFile); /* Removes any back slashes in the file name */
		$photoFile = preg_replace('[^a-zA-Z0-9.]', '', $photoFile); /* Allows characters and numbers only. */
		$photoFile = strtolower($photoFile); /* Puts the file name all in lowercase. */
		// Add a new photo id number.
		$photoID = '';
		/* Add Photos of the Property. */
		if($key &gt; '0' &amp;&amp; $key &lt; '11'){
			$addphoto = "INSERT INTO ".TBL_PHOTOS." (`property_id`, `photo_id`, `username`, `filename`, `filesize`, `width`, `height`, `position`) VALUES ('$pID', '$photoID', '".mysql_real_escape_string($username)."', '".mysql_real_escape_string($photoFile)."', '".mysql_real_escape_string($photoSize)."', '".mysql_real_escape_string($width)."', '".mysql_real_escape_string($height)."', '".mysql_real_escape_string($key)."')";
			$sqlphoto = mysql_query($addphoto) or die("ERROR INSERTING PROPERTY PHOTO: ".mysql_error());
		}
	}

Do you have any clue as to why that happens?

Share this post


Link to post
Share on other sites
  • 0
The_Decryptor    1,105

You only set $photoFile if $key is equal to 10.

Share this post


Link to post
Share on other sites
  • 0
seb86    0

You only set $photoFile if $key is equal to 10.

There is an additional line if you look.

else{ $photoFile = str_replace("&lt;/photo&gt;", "", $photo); }

Share this post


Link to post
Share on other sites
  • 0
The_Decryptor    1,105

Ah, I didn't see that, the code's really quite messy.

Why are you special casing Image 10? you're stripping any included tags for that image but not others, etc.

Share this post


Link to post
Share on other sites
  • 0
seb86    0

Ah, I didn't see that, the code's really quite messy.

Why are you special casing Image 10? you're stripping any included tags for that image but not others, etc.

Because in the XML file the client has two other tags outside the photo list that contains web links and when it comes to inserting the last photo into the database it continues down the xml list and adds the content of those tags aswell.

Thats why I had to do a clean up for photo 10 only. So any reason why the other 9 photos width and height are not going in?

&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;

&lt;annonce&gt;

&lt;reference&gt;123456789&lt;/reference&gt;
&lt;username&gt;test123&lt;/username&gt;
&lt;address&gt;42 Rue Alexandre Cabanel&lt;/address&gt;
&lt;village&gt;Herault&lt;/village&gt;
&lt;area&gt;Montpellier&lt;/area&gt;
&lt;section&gt;Bord de Mer&lt;/section&gt;
&lt;postcode&gt;34000&lt;/postcode&gt;

&lt;liste_photos&gt;
&lt;photo&gt;http://photos.domain.com/99999/999991_1.jpg&lt;/photo&gt;
&lt;photo&gt;http://photos.domain.com/99999/999991_2.jpg&lt;/photo&gt;
&lt;photo&gt;http://photos.domain.com/99999/999991_3.jpg&lt;/photo&gt;
&lt;photo&gt;http://photos.domain.com/99999/999991_4.jpg&lt;/photo&gt;
&lt;photo&gt;http://photos.domain.com/99999/999991_5.jpg&lt;/photo&gt;
&lt;photo&gt;http://photos.domain.com/99999/999991_6.jpg&lt;/photo&gt;
&lt;photo&gt;http://photos.domain.com/99999/999991_7.jpg&lt;/photo&gt;
&lt;photo&gt;http://photos.domain.com/99999/999991_8.jpg&lt;/photo&gt;
&lt;photo&gt;http://photos.domain.com/99999/999991_9.jpg&lt;/photo&gt;
&lt;photo&gt;http://photos.domain.com/99999/999991_10.jpg&lt;/photo&gt;
&lt;/liste_photos&gt;

&lt;url_1&gt;http://www.domain.com&lt;/url_1&gt;
&lt;url_2&gt;http://www.domain.com&lt;/url_2&gt;

&lt;/annonce&gt;

Share this post


Link to post
Share on other sites
  • 0
AnthonySterling    11

Please, please, please, stop 'parsing' the XML the way your are. You'll find your job a lot easier and these little issues you have, will disappear.

Share this post


Link to post
Share on other sites
  • 0
seb86    0

Ok i made some adjustments to what the client has told me to do. It turns out that I need to download a copy of the photos from the server.

So i read the photo url as before but instead of inserting a link to that photo into the database I need to copy each photo from the xml file.

This is the code I have currently written for the photo section:

	$photos = explode("&lt;photo&gt;", $content);
	unset($photos[0]);
	/* Fetch each photo. */
	foreach($photos as $key =&gt; $photo){
		if($key == '12'){
			/* Clean content after the last photo from list of photos. */
			$photoFile = str_replace("&lt;/photo&gt;", "", $photo);
			$p1 = strpos($photo, '&lt;photo&gt;');
			$p2 = strpos($photo, '&lt;/photo&gt;');
			$m1 = $p2 - $p1;
			$photo = substr($photo, $p1, $m1);
			$photoFile = strip_tags($photo); /* Removes any HTML Tags. */
		}
		else{ $photoFile = str_replace("&lt;/photo&gt;", "", $photo); }
		///////////////////////////////////////////////////////////////
		$imageTypes = array('jpeg', 'jpg', 'gif', 'png'); /* Identify the file extension in the file the user is uploading. */
		$position = strlen($photoFile) - (strpos($photoFile, '.')); /* Locates the last dot in the file name. */
		$extension = substr($photoFile, -4); /* Gets the extension at the end of the file name. */
		// Download destination.
		$download_destination = "photos/".$pID."/";
		// Create new folder to store photos for this product.
		if(!is_dir($download_destination)){
			mkdir($download_destination, 0777); /* Create folder if it does not exist. */
			chmod($download_destination, 0777); /* Make folder writable. */
			echo "&lt;b&gt;New Folder Created!&lt;/b&gt;&lt;br /&gt;\n";
		}
		else{
			chmod($download_destination, 0777); /* Make folder writable. */
			echo "&lt;b&gt;Folder already exists&lt;/b&gt;, setting file permissions to save photo in new directory.&lt;br /&gt;\n";
		}
		echo "The original location of the photo: ".$photoFile."&lt;br /&gt;\n";
		/* We will give an new unique name in a random string */
		$fileName = $pID."_".rand_str().".".strtolower($extension);
		$imageName = $fileName;
		echo "File name has been re-written to: ".$imageName."&lt;br /&gt;\n";
		/* The original photo goes to this directory. */
		$newphoto = $download_destination.''.$imageName;
		$clone = $download_destination.''.original.'_'.$imageName;
		echo "New Location: &lt;i&gt;".$newphoto."&lt;/i&gt;&lt;br /&gt;\n";
		// Download the photo from the server to the new location.
		$file = fopen($photoFile, 'r') or die("Can't open file");
		if(!copy($file, $newphoto)){
			$errors = error_get_last();
			echo "COPY ERROR: ".$errors['type']."&lt;br /&gt;\n".$errors['message'];
		}
		else{
			echo "Photo has been copied and renamed.&lt;br /&gt;\n";
			copy($newphoto, $clone);
			/*
			* Now we call the function that will create the thumbnails for each photo.
			* The function will get the parameters, the image name, the thumbnail name
			* and the width and height desired for the thumbnail.
			*/
			$image = "photos/".$pID."/".$imageName;
			$thumb = makeThumb($image,$thumb,tn_WIDTH,tn_HEIGHT,$pID);
			// Fetch photo file size.
			$fetchSize = filesize($image);
			echo "Filesize of the photo: ".$fetchSize."&lt;br /&gt;\n";
			// Fetch photo width and height.
			list($width, $height) = @getimagesize($image);
			echo "Width of Photo: ".$width."&lt;br /&gt;\n";
			echo "Height of Photo: ".$height."&lt;br /&gt;\n";
			// Checks if the photo is larger than the minimum required size of the photo height or width.
			if($height &gt; HEIGHT || $width &gt; WIDTH){
				$sized = makeThumb($fetchSize,$resized,WIDTH,HEIGHT,$pID);
			}
			else{
				$copy = $download_destination.''.$imageName; copy($newphoto, $copy);
			}
		}
		fclose($file); // Close file.
		// Add a new photo id number.
		$photoID = '';
		/* Add Photos of the Product. */
		if($key &gt; '0' &amp;&amp; $key &lt; '11'){
			$addphoto = "INSERT INTO ".TBL_PHOTOS." (`product_id`, `photo_id`, `username`, `filename`, `filesize`, `width`, `height`, `position`) VALUES ('$pID', '$photoID', '".mysql_real_escape_string($username)."', '".mysql_real_escape_string($imageName)."', '".mysql_real_escape_string($fetchSize)."', '".mysql_real_escape_string($width)."', '".mysql_real_escape_string($height)."', '".mysql_real_escape_string($key)."')";
			$sqlphoto = mysql_query($addphoto) or die("ERROR INSERTING PHOTO: ".mysql_error());
		}
	}

The problem i am having at the moment is for each photo, before I can copy it I need to open the photo file in read mode using fopen() but for some reason I can't get it to work.

I get an error saying can't find header 404 or something and I checked each photo url I have been given and they are all working.

Most of the code works but until i get this part working:

$file = fopen($photoFile, 'r') or die("Can't open file");

I can't move forward.

If any of you are able to help me, it would be most appreciated.

Thank you.

Share this post


Link to post
Share on other sites
  • 0
seb86    0

Ok I have attempted at parsing the xml file instead and this is what I have so far:

&lt;?php
$file = "http://www.domain.com/import.xml"; // XML file location.

//Initialize the XML parser
$parser=xml_parser_create();

//Function to use at the start of an element
function startElement($parser, $tagName, $attrs){
	global $insideitem, $tag;
	if($insideitem){
		$tag = $tagName;
	}
	elseif($tagName == "ANNONCE"){
		$insideitem = true;
	}
}

//Function to use at the end of an element
function endElement($parser, $tagName){
	global $insideitem, $tag;

	echo "&lt;br /&gt;";
}

//Function to use when finding character data
function characterData($parser, $data){
	global $insideitem, $tag;

	if($insideitem){
		switch($tag){
			case "ANNONCE":
			echo "-- Property --";
			break;
			case "AGENCE":
			$agence_name .= $data;
			break;
			case "AGENCE_TEL":
			$agence_telephone .= $data;
			break;
			case "AGENCE_EMAIL":
			$agence_email .= $data;
			break;
			case "AGENCE_ADRESSE":
			$agence_address .= $data;
			break;
			case "AGENCE_VILLE":
			$agence_village .= $data;
			break;
			case "AGENCE_CP":
			$agence_postcode .= $data;
			break;
			case "AGENCE_SITE":
			$agence_website .= $data;
			break;
			case "AGENCE_SIRET":
			$agence_siren .= $data;
			break;
			case "NEGO_NOM":
			$agent_name .= $data;
			break;
			case "NEGO_TEL":
			$agent_telephone .= $data;
			break;
			case "NEGO_EMAIL":
			$agent_email .= $data;
			break;
			case "REFERENCE":
			$reference .= $data;
			break;
			case "IDTYPE":
			$category .= $data;
			break;
			case "ADRESSE_REELLE":
			$address .= $data;
			break;
			case "CODE_POSTAL_REEL":
			$postcode .= $data;
			break;
			case "VILLE_REELE":
			$village .= $data;
			break;
			case "ZONE":
			$department .= $data;
			break;
			case "PRIX":
			$price .= $data;
			break;
			case "NB_CHAMBRE":
			$bedrooms .= $data;
			break;
			case "NB_SALLE_DEAU":
			$bathrooms .= $data;
			break;
			case "VUE":
			$view .= $data;
			break;
			case "NB_GARAGE":
			$garage .= $data;
			break;
			case "PISCINE":
			$pool .= $data;
			break;
			case "SURF_TERRAIN":
			$terrain .= $data;
			break;
			case "TAXE_HABITATION":
			$tax .= $data;
			break;
			case "TAXE_FONCIERE":
			$fees .= $data;
			break;
			case "TEXTE_FR":
			$descriptionFR .= $data;
			break;
			case "TEXTE_UK":
			$descriptionEN .= $data;
			break;
			case "TEXTE_DE":
			$descriptionDE .= $data;
			break;
			case "TEXTE_ES":
			$descriptionES .= $data;
			break;
			case "TEXTE_IT":
			$descriptionIT .= $data;
			break;
			case "TEXTE_NL":
			$descriptionNL .= $data;
			break;
			case "AGENCE_NUMAGE":
			$agenceID .= $data;
			break;
		}
	}
	return $data;
}

//Specify element handler
xml_set_element_handler($parser, "startElement", "endElement");

//Specify data handler
xml_set_character_data_handler($parser, "characterData");

//Open XML file
$fp=fopen($file,"r");

//Read data
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)));
}

//Free the XML parser
xml_parser_free($parser);
?&gt;

So far so good but now I don't know how to get each parse into my database. I have identified each parse so that it can be recognized and inserted into the database but I get nothing but blank when the code is processed.

I was wondering if there is something missing in my parsing code that returns the data into the database as I am unable to find any other answers on the web telling me different on what I need to do other than what I have done above.

Please if any one can help me, I would really appreciate it.

Thank you.

Share this post


Link to post
Share on other sites
  • 0
seb86    0

Ok I have got the functions all figured out now. I get the data into the database, photos copied from one server onto another and then resized.

Through out my tests everything is now working except one thing.

I can only insert one item from the XML file (item = annonce).

&lt;annonce&gt;
	&lt;reference&gt;123456789&lt;/reference&gt;
	&lt;username&gt;test123&lt;/username&gt;
	&lt;address&gt;42 Rue Alexandre Cabanel&lt;/address&gt;
	&lt;village&gt;Herault&lt;/village&gt;
	&lt;area&gt;Montpellier&lt;/area&gt;
	&lt;section&gt;Bord de Mer&lt;/section&gt;
	&lt;postcode&gt;34000&lt;/postcode&gt;
	&lt;liste_photos&gt;
		&lt;photo&gt;http://photos.domain.com/99999/999991_1.jpg&lt;/photo&gt;
		&lt;photo&gt;http://photos.domain.com/99999/999991_2.jpg&lt;/photo&gt;
		&lt;photo&gt;http://photos.domain.com/99999/999991_3.jpg&lt;/photo&gt;
		&lt;photo&gt;http://photos.domain.com/99999/999991_4.jpg&lt;/photo&gt;
		&lt;photo&gt;http://photos.domain.com/99999/999991_5.jpg&lt;/photo&gt;
		&lt;photo&gt;http://photos.domain.com/99999/999991_6.jpg&lt;/photo&gt;
		&lt;photo&gt;http://photos.domain.com/99999/999991_7.jpg&lt;/photo&gt;
		&lt;photo&gt;http://photos.domain.com/99999/999991_8.jpg&lt;/photo&gt;
		&lt;photo&gt;http://photos.domain.com/99999/999991_9.jpg&lt;/photo&gt;
		&lt;photo&gt;http://photos.domain.com/99999/999991_10.jpg&lt;/photo&gt;
	&lt;/liste_photos&gt;
&lt;/annonce&gt;
&lt;annonce&gt;
	&lt;reference&gt;987654321&lt;/reference&gt;
	&lt;username&gt;test456&lt;/username&gt;
	&lt;address&gt;42 Rue Alexandre Cabanel&lt;/address&gt;
	&lt;village&gt;Herault&lt;/village&gt;
	&lt;area&gt;Montpellier&lt;/area&gt;
	&lt;section&gt;Bord de Mer&lt;/section&gt;
	&lt;postcode&gt;34000&lt;/postcode&gt;
	&lt;liste_photos&gt;
		&lt;photo&gt;http://photos.domain.com/99999/999991_1.jpg&lt;/photo&gt;
		&lt;photo&gt;http://photos.domain.com/99999/999991_2.jpg&lt;/photo&gt;
		&lt;photo&gt;http://photos.domain.com/99999/999991_3.jpg&lt;/photo&gt;
		&lt;photo&gt;http://photos.domain.com/99999/999991_4.jpg&lt;/photo&gt;
		&lt;photo&gt;http://photos.domain.com/99999/999991_5.jpg&lt;/photo&gt;
		&lt;photo&gt;http://photos.domain.com/99999/999991_6.jpg&lt;/photo&gt;
		&lt;photo&gt;http://photos.domain.com/99999/999991_7.jpg&lt;/photo&gt;
		&lt;photo&gt;http://photos.domain.com/99999/999991_8.jpg&lt;/photo&gt;
		&lt;photo&gt;http://photos.domain.com/99999/999991_9.jpg&lt;/photo&gt;
		&lt;photo&gt;http://photos.domain.com/99999/999991_10.jpg&lt;/photo&gt;
	&lt;/liste_photos&gt;
&lt;/annonce&gt;

How do i tell my code to fetch all data for each item and not just the first?

Do i need to re-adjust this piece of code and how would I go about doing that?

// Break it into an array.... !
$content_x = explode("&lt;annonce&gt;", $content);
reset($content_x);

Share this post


Link to post
Share on other sites
  • 0
The_Decryptor    1,105

You need a top level element for them to be a child of, then you loop through the child elements of that new root element (if it exists)

&lt;announcements&gt;
    &lt;annonce&gt;
        ...
    &lt;/annonce&gt;
    &lt;annonce&gt;
        ...
    &lt;/annonce&gt;
    ...
&lt;/announcements&gt;

etc.

Share this post


Link to post
Share on other sites
  • 0
seb86    0

You need a top level element for them to be a child of, then you loop through the child elements of that new root element (if it exists)

&lt;announcements&gt;
    &lt;annonce&gt;
        ...
    &lt;/annonce&gt;
    &lt;annonce&gt;
        ...
    &lt;/annonce&gt;
    ...
&lt;/announcements&gt;

etc.

This is what the xml file reads altogether.

&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
&lt;adapt_xml&gt;
        &lt;annonce&gt;
                &lt;reference&gt;123456789&lt;/reference&gt;
                &lt;username&gt;test123&lt;/username&gt;
                &lt;address&gt;42 Rue Alexandre Cabanel&lt;/address&gt;
                &lt;village&gt;Herault&lt;/village&gt;
                &lt;area&gt;Montpellier&lt;/area&gt;
                &lt;section&gt;Bord de Mer&lt;/section&gt;
                &lt;postcode&gt;34000&lt;/postcode&gt;
                &lt;liste_photos&gt;
                        &lt;photo&gt;http://photos.domain.com/99999/999991_1.jpg&lt;/photo&gt;
                        &lt;photo&gt;http://photos.domain.com/99999/999991_2.jpg&lt;/photo&gt;
                        &lt;photo&gt;http://photos.domain.com/99999/999991_3.jpg&lt;/photo&gt;
                        &lt;photo&gt;http://photos.domain.com/99999/999991_4.jpg&lt;/photo&gt;
                        &lt;photo&gt;http://photos.domain.com/99999/999991_5.jpg&lt;/photo&gt;
                        &lt;photo&gt;http://photos.domain.com/99999/999991_6.jpg&lt;/photo&gt;
                        &lt;photo&gt;http://photos.domain.com/99999/999991_7.jpg&lt;/photo&gt;
                        &lt;photo&gt;http://photos.domain.com/99999/999991_8.jpg&lt;/photo&gt;
                        &lt;photo&gt;http://photos.domain.com/99999/999991_9.jpg&lt;/photo&gt;
                        &lt;photo&gt;http://photos.domain.com/99999/999991_10.jpg&lt;/photo&gt;
                &lt;/liste_photos&gt;
        &lt;/annonce&gt;
        &lt;annonce&gt;
                &lt;reference&gt;987654321&lt;/reference&gt;
                &lt;username&gt;test456&lt;/username&gt;
                &lt;address&gt;42 Rue Alexandre Cabanel&lt;/address&gt;
                &lt;village&gt;Herault&lt;/village&gt;
                &lt;area&gt;Montpellier&lt;/area&gt;
                &lt;section&gt;Bord de Mer&lt;/section&gt;
                &lt;postcode&gt;34000&lt;/postcode&gt;
                &lt;liste_photos&gt;
                        &lt;photo&gt;http://photos.domain.com/99999/999991_1.jpg&lt;/photo&gt;
                        &lt;photo&gt;http://photos.domain.com/99999/999991_2.jpg&lt;/photo&gt;
                        &lt;photo&gt;http://photos.domain.com/99999/999991_3.jpg&lt;/photo&gt;
                        &lt;photo&gt;http://photos.domain.com/99999/999991_4.jpg&lt;/photo&gt;
                        &lt;photo&gt;http://photos.domain.com/99999/999991_5.jpg&lt;/photo&gt;
                        &lt;photo&gt;http://photos.domain.com/99999/999991_6.jpg&lt;/photo&gt;
                        &lt;photo&gt;http://photos.domain.com/99999/999991_7.jpg&lt;/photo&gt;
                        &lt;photo&gt;http://photos.domain.com/99999/999991_8.jpg&lt;/photo&gt;
                        &lt;photo&gt;http://photos.domain.com/99999/999991_9.jpg&lt;/photo&gt;
                        &lt;photo&gt;http://photos.domain.com/99999/999991_10.jpg&lt;/photo&gt;
                &lt;/liste_photos&gt;
        &lt;/annonce&gt;
&lt;/adapt_xml&gt;

How do i code what you suggested I need to do for it to work?

Share this post


Link to post
Share on other sites
  • 0
AnthonySterling    11

This is what the xml file reads altogether.

<snip />

How do i code what you suggested I need to do for it to work?

Have we not already been through this Seb?

Share this post


Link to post
Share on other sites
  • 0
Kami-    178

Sorry this is not what I need. My client's file is exported as an xml file only.

The PHP is on your server along with the XSLT you create to match the layout of the XML you are provided.

All you need to obtain is an XML document/text string input from your client, which is then passed into the parser function along with the XSLT and you get the defined output.

Share this post


Link to post
Share on other sites
  • 0
seb86    0

I have succeeded in inserting each property from the XML feed. So far I have had no problems. Now I just need to find a suitable location for the script to run automatically every hour. Thank you guys for all your help and resources.

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  

  • Recently Browsing   0 members

    No registered users viewing this page.