Jump to content

Recommended Posts

Here is my problem I am trying to import an xml file that is basically formatted like this.

 

<?xml version="1.0" encoding="UTF-8"?>
<AvailableBatch>
  <Date>09/23/2009</Date>
  <UDT>1253751301</UDT>
  <Type>Full</Type>
  <Available>
    <Sku>427890</Sku>
    <Part>1001BSG</Part>
    <Qty>38</Qty>
    <Time>20:18:11</Time>
  </Available>
  <Available>
    <Sku>427870</Sku>
    <Part>1003BSS</Part>
    <Qty>218</Qty>
    <Time>20:18:11</Time>
  </Available>

 

All i need to import are the values of Sku Part and Qty.

 

Here is what I have programmed so far but it doesn't seem to work for me.

 

<html>
<head>
	<link rel="shortcut icon" href="/favicon.ico" />
	<title>
		Title here i took it out =p
	</title>
</head>

<body>
<?php

$con = mysql_connect("localhost","root","tookitout=p");

if (!$con){
die("Connection Error:" . mysql_error());
}
$parser = xml_parser_create();

function start($parser, $element_name, $element_attrs)
{
	$result = mysql_query($query) or die(mysql_error());

	if(!$result) {
		echo mysql_error();
	} 
	else 
	{
			mysql_select_db("Products", $con);
			mysql_query("INSERT INTO products (id) VALUES ('')");
		switch($element_name)
			{
				case "SKU":
					'"));mysql_query("INSERT INTO products sku VALUES ("';
				break;
				case "PART":
					'"));mysql_query("INSERT INTO products item_id VALUES ("';
				break;
				case "QTY":
					'"));mysql_query("INSERT INTO products quanity VALUES ("';
				break;
					'"));mysql_query("INSERT INTO products time VALUES ("';
				case "TIME":
				break;
				default:
					'"))';
			}
		}
}

function stop($parser, $element_name)
	{
	}

function char($parser,$data)
  {
  echo $data;
  }

xml_set_element_handler($parser, "start", "stop");
xml_set_character_data_handler($parser, "char");

$fp=fopen("http://morris.morriscostumes.com/out/available_batchnynyy_001.xml","r");

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)));
  }

xml_parser_free($parser);
?>
	</body>
</html>

 

I've been looking around and there isn't a whole lot of people that can do this. So please help me.

The following is quick and dirty code that is specific to the data you showed. You need to put back in your actual mysql code (see the comments, two places) -

<?php
session_start(); 
$_SESSION['parts'] = array(); // cheat and share a global session variable between the different xml handler functions
?>
<html>
   <head>
      <link rel="shortcut icon" href="/favicon.ico" />
      <title>
         Title here i took it out =p
      </title>
   </head>
   
   <body>
<?php

// your mysql_connect() and mysql_select_db() goes here ...

$parser = xml_parser_create();

function start($parser, $element_name, $element_attrs)
   {
         switch($element_name)
            {
               case "SKU":
				// this is the start of a set of data
				$_SESSION['parts'] = array(); // create an empty set
               break;
               case "TIME":
				// this is the tag that is after the end of the stated set of data, complete and execute the query here -
				// code to build the whole query statement and your mysql_query() goes here ...
				print_r($_SESSION['parts']); // contains the three values for sku, part, qty
				echo "<br />";
               break;
               default:
            }
   }

function stop($parser, $element_name)
      {
      }
      
function char($parser,$data)
{
$data = trim($data);
if($data != ''){
	if(!isset($_SESSION['parts']['SKU'])){
		$_SESSION['parts']['SKU'] = $data;
	} elseif(!isset($_SESSION['parts']['PART'])){
		$_SESSION['parts']['PART'] = $data;
	} elseif (!isset($_SESSION['parts']['QTY'])){
		$_SESSION['parts']['QTY'] = $data;
	}
}
}

xml_set_element_handler($parser, "start", "stop");
xml_set_character_data_handler($parser, "char");

$fp=fopen("http://morris.morriscostumes.com/out/available_batchnynyy_001.xml","r");

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)));
  }

xml_parser_free($parser);
?>
      </body>
</html>

Thanks that helped out a lot I still have been unable to parse the XML into the database but I feel that I am on the brink of understanding the problem. I never thought of looking at it in a session array. You have been very helpful I would rep you but I'm not sure if I can or it exists on this smf forum.

I can't seem to get it into the database still. It throws no errors or exceptions. Here is what I got.

 

<?php
session_start(); 
$_SESSION['parts'] = array();
?>
<html>
   <head>
      <link rel="shortcut icon" href="/favicon.ico" />
      <title>
         Page Title Goes Here
      </title>
   </head>
   
	<body>
<?php

mysql_connect('connection', 'username', 'password');
mysql_select_db('products');

$parser = xml_parser_create();

function start($parser, $element_name, $element_attrs)
   {
         switch($element_name)
            {
               case "SKU":
               // this is the start of a set of data
               $_SESSION['parts'] = array(); // create an empty set
               break;
		   case "TIME";
		   mysql_query("INSERT INTO products (sku, part, qty) VALUES ('$data')");
		   echo "<p>Completed Successfully!</p>";
               break;
               default:
            }
   }

function stop($parser, $element_name)
      {
      }
      
function char($parser,$data)
{
   $data = trim($data);
   if($data != ''){
      if(!isset($_SESSION['parts']['SKU'])){
         $_SESSION['parts']['SKU'] = $data;
      } elseif(!isset($_SESSION['parts']['PART'])){
         $_SESSION['parts']['PART'] = $data;
      } elseif (!isset($_SESSION['parts']['QTY'])){
         $_SESSION['parts']['QTY'] = $data;
      }
   }
}

xml_set_element_handler($parser, "start", "stop");
xml_set_character_data_handler($parser, "char");

$fp=fopen("http://morris.morriscostumes.com/out/available_batchnynyy_001.xml","r");

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)));
  }

xml_parser_free($parser);
?>
      </body>
</html>

The three values are in the array, just put them into the query and execute it -

               case "TIME":
				// this is the tag that is after the end of the stated set of data, complete and execute the query here -
				// code to build your whole query statement and your mysql_query() goes here ...
				// print_r($_SESSION['parts']); // contains the three values for sku, part, qty
				$query = sprintf("INSERT INTO products (sku, part, qty) VALUES ('%s','%s',%d)",
					mysql_real_escape_string($_SESSION['parts']['SKU']),
					mysql_real_escape_string($_SESSION['parts']['PART']),
					(int)$_SESSION['parts']['QTY']);
				mysql_query($query);
               break;

So the switch is doing no good? if that correct.

 

but why?

 

if the word TIME is called then should up date..

 

if you of raped this around the query

 

mysql_query($query); added mysql_error()

 

would you off got the correct error statement....

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.