troubleshooting Question

please assist with XPATH and php file code

Avatar of jecommera
jecommeraFlag for United Kingdom of Great Britain and Northern Ireland asked on
PHPXML
1 Comment1 Solution329 ViewsLast Modified:
Hi,

Please find code below and generated (updated) XML file attached.

As you can see the format of the file data is;
<header>
<cateogry>
<product>
<variation-attribute> etc.

Currently when I run the code it will add a product to the end of the file at the end of file

However I want it to update with the rest of the products i.e. after <category> elements and before <variation-attribute>.

Can someone please advise how I should update my code to allow this.

thank you.

<?php // RAY_csv_to_db.php
echo "<pre>\n";  //READABILITY FOR var_dump()
 if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "C://uploads/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "uploads/" . $_FILES["file"]["name"];
      }}
      
error_reporting(E_ALL);
echo "<pre>\n";  //READABILITY FOR var_dump()



// CONNECTION AND SELECTION VARIABLES FOR THE DATABASE
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "pim";        // GET THESE FROM YOUR HOSTING COMPANY
$db_user = "root";
$db_word = "";



// OPEN A CONNECTION TO THE DATA BASE SERVER
// MAN PAGE: http://us2.php.net/manual/en/function.mysql-connect.php
if (!$db_connection = mysql_connect("$db_host", "$db_user", "$db_word"))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB CONNECTION: ";
    echo "<br/> $errmsg <br/>";
}

// SELECT THE MYSQL DATA BASE
// MAN PAGE: http://us2.php.net/manual/en/function.mysql-select-db.php
if (!$db_sel = mysql_select_db($db_name, $db_connection))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB SELECTION: ";
    echo "<br/> $errmsg <br/>";
    die('NO DATA BASE');
}
// IF WE GOT THIS FAR WE CAN DO QUERIES



// TEST DATA, SAVED FROM THE POST AT EE
$csv = "c://uploads/products.csv";
$fpo = fopen($csv, 'r');
if (!$fpo ) die('CRUMP');

// GET THE FIELD NAMES FROM THE TOP OF THE CSV FILE
$top = fgetcsv($fpo);
$cnt = count($top);

// SET UP KEY NAMES FOR USE IN OUR QUERY
$query_cols = implode(',', $top);

// SET A ROW COUNTER
$counter = 0;

// KEEP TRACK OF ROWS THAT HAVE THE WRONG NUMBER OF FIELDS
$errors = array();

// LOOP THROUGH THE CSV RECORDS PERFORMING CERTAIN TESTS
while (!feof($fpo))
{
    $counter++;

    // GET A RECORD
    $csvdata = fgetcsv($fpo);

    // SKIP OVER EMPTY ROWS
    if (empty($csvdata)) continue;

    // CHECK THE NUMBER OF FIELDS
    if ($cnt != count($csvdata))
    {
        $errors[] = $counter;
        continue;
    }

    // MAYBE ASSIGN KEYS TO THE ROW OF FIELDS - ACTIVATE THIS TO SEE THE ASSOCIATIVE ARRAY
    //  $csvdata = array_combine($top, $csvdata);
    //  var_dump($csvdata);

    // ESCAPE THE INFORMATION FOR USE IN THE QUERY
    foreach ($csvdata as $ptr => $value)
    {
        $csvdata[$ptr] = mysql_real_escape_string($value);
    }

    // SET UP VALUE FIELDS
    $query_data = "'" . implode("', '", $csvdata) . "'";

    // SET UP A QUERY
    $sql = "REPLACE INTO products ( $query_cols ) VALUES ( $query_data )";

    // RUN THE QUERY HERE....
    var_dump($sql);
$res = mysql_query($sql) or die("FAIL: $sql <br/>" . mysql_error() );
    
}


// SHOW THE NUMBER OF ROWS PROCESSED
echo "<br/>RECORDS PROCESSED $counter \n";

// SHOW THE NUMBERS OF THE ROWS WITH THE WRONG NUMBER OF FIELDS
if (count($errors))
{
    echo "<br/>ROWS WITH THE WRONG NUMBER OF FIELDS: \n";
    var_dump($errors);
}
        
$result = mysql_query("SELECT * FROM products");

while($row = mysql_fetch_array($result))
  {
    $xml_fname = "loadAndEdit.xml";
    $xml = new DOMDocument();
    $xml->preserveWhiteSpace = true;
    $xml->load($xml_fname);

    $xpath = new DOMXPath($xml);
    $xpath->registerNamespace('c', 'http://www.ourcompanynamespace.com/xml/istuff/stuff2/2010-10-31');

    $catalog = $xpath->query("//catalog"); 	
    $product = $xml->createElement('product');
    var_dump($catalog);
    $catalog->item(0)->appendChild($product);							
    $product->setAttribute('product-id', $row['product_id']);
    
	$ean = $xml->createElement('ean');									
    $product->appendChild($ean);
	$ean->appendChild($xml->createTextNode($row['ean']));	
	
	$upc = $xml->createElement('upc');	
	$product->appendChild($upc);

	$unit = $xml->createElement('unit');	
	$product->appendChild($unit);

	$min_order_quantity = $xml->createElement('min-order-quantity');
	$product->appendChild($min_order_quantity);
	$min_order_quantity->appendChild($xml->createTextNode(1));

	$step_quantity = $xml->createElement('step-quantity');	
	$product->appendChild($step_quantity);
	$step_quantity->appendChild($xml->createTextNode(1));

	$display_name = $xml->createElement('display-name');									
    $product->appendChild($display_name);
	$display_name->setAttribute('xml:lang', 'x-default');
	$display_name->appendChild($xml->createTextNode($row['product_name']));

	$short_description = $xml->createElement('short-description');									
    $product->appendChild($short_description);
	$short_description->setAttribute('xml:lang', 'x-default');
	$short_description->appendChild($xml->createTextNode($row['short_description']));

	$long_description = $xml->createElement('long-description');									
    $product->appendChild($long_description);
	$long_description->setAttribute('xml:lang', 'x-default');
	$long_description->appendChild($xml->createTextNode($row['long_description']));

	$online_flag = $xml->createElement('online-flag');									
    $product->appendChild($online_flag);
	$online_flag->appendChild($xml->createTextNode($row['online_flag']));

	$available_flag = $xml->createElement('available-flag');									
    $product->appendChild($available_flag);
	$available_flag->appendChild($xml->createTextNode($row['available_flag']));

	$searchable_flag = $xml->createElement('searchable-flag');									
    $product->appendChild($searchable_flag);
	$searchable_flag->appendChild($xml->createTextNode($row['searchable_flag']));

	$images = $xml->createElement('images');
	$product->appendChild($images);
	
	$image_group = $xml->createElement('image-group');
	$image_group->setAttribute('view-type', 'original');									
    $images->appendChild($image_group);
	
	$image = $xml->createElement('image');
	$image_group->appendChild($image);
	$image->setAttribute('path', 'original/image1');
	$image_group->appendChild($image);
	$image->setAttribute('path', 'original/image2');

	$tax_class_id = $xml->createElement('tax-class-id');									
    $product->appendChild($tax_class_id);
	$tax_class_id->appendChild($xml->createTextNode($row['tax_class_id']));

	$page_attributes = $xml->createElement('page-attributes');									
    $product->appendChild($page_attributes);
	
	$custom_attributes = $xml->createElement('custom-attributes');									
    $product->appendChild($custom_attributes);
	
	$custom_attribute = $xml->createElement('custom-attribute');									
    $custom_attributes->appendChild($custom_attribute);
	$custom_attribute->setAttribute('attribute-id', 'allowedBackOrder');
	$custom_attribute->appendChild($xml->createTextNode($row['allowedBackOrder']));

	$custom_attribute = $xml->createElement('custom-attribute');									
    $custom_attributes->appendChild($custom_attribute);
	$custom_attribute->setAttribute('attribute-id', 'allowedPreOrder');
	$custom_attribute->appendChild($xml->createTextNode($row['allowedPreOrder']));

	$custom_attribute = $xml->createElement('custom-attribute');									
    $custom_attributes->appendChild($custom_attribute);
	$custom_attribute->setAttribute('attribute-id', 'browseCategory');
	$custom_attribute->appendChild($xml->createTextNode($row['browseCategory']));

	$custom_attribute = $xml->createElement('custom-attribute');									
    $custom_attributes->appendChild($custom_attribute);
	$custom_attribute->setAttribute('attribute-id', 'browseCategory5');
	$custom_attribute->appendChild($xml->createTextNode($row['browseCategory5']));

	$custom_attribute = $xml->createElement('custom-attribute');									
    $custom_attributes->appendChild($custom_attribute);
	$custom_attribute->setAttribute('attribute-id', 'browseCategory6');
	$custom_attribute->appendChild($xml->createTextNode($row['browseCategory6']));

	$custom_attribute = $xml->createElement('custom-attribute');									
    $custom_attributes->appendChild($custom_attribute);
	$custom_attribute->setAttribute('attribute-id', 'costPrice');
	$custom_attribute->appendChild($xml->createTextNode($row['costPrice']));

	$custom_attribute = $xml->createElement('custom-attribute');									
    $custom_attributes->appendChild($custom_attribute);
	$custom_attribute->setAttribute('attribute-id', 'disableContentTab');
	$custom_attribute->appendChild($xml->createTextNode($row['disableContentTab']));

	$custom_attribute = $xml->createElement('custom-attribute');									
    $custom_attributes->appendChild($custom_attribute);
	$custom_attribute->setAttribute('attribute-id', 'highValue');
	$custom_attribute->appendChild($xml->createTextNode($row['highValue']));

	$custom_attribute = $xml->createElement('custom-attribute');									
    $custom_attributes->appendChild($custom_attribute);
	$custom_attribute->setAttribute('xml-lang', 'x-default');
	$custom_attribute->setAttribute('attribute-id', 'manufactureCountry');
	$custom_attribute->appendChild($xml->createTextNode($row['manufactureCountry']));

	$custom_attribute = $xml->createElement('custom-attribute');									
    $custom_attributes->appendChild($custom_attribute);
	$custom_attribute->setAttribute('attribute-id', 'materialRecycled');
	$custom_attribute->appendChild($xml->createTextNode($row['materialRecycled']));

	$custom_attribute = $xml->createElement('custom-attribute');									
    $custom_attributes->appendChild($custom_attribute);
	$custom_attribute->setAttribute('attribute-id', 'materialSustainable');
	$custom_attribute->appendChild($xml->createTextNode($row['materialSustainable']));

	$custom_attribute = $xml->createElement('custom-attribute');									
    $custom_attributes->appendChild($custom_attribute);
	$custom_attribute->setAttribute('attribute-id', 'proofDishwasher');
	$custom_attribute->appendChild($xml->createTextNode($row['proofDishwasher']));
	
	$custom_attribute = $xml->createElement('custom-attribute');									
    $custom_attributes->appendChild($custom_attribute);
	$custom_attribute->setAttribute('attribute-id', 'proofMicrowave');
	$custom_attribute->appendChild($xml->createTextNode($row['proofMicrowave']));

	$custom_attribute = $xml->createElement('custom-attribute');									
    $custom_attributes->appendChild($custom_attribute);
	$custom_attribute->setAttribute('attribute-id', 'refinementColor');
	$custom_attribute->appendChild($xml->createTextNode($row['refinementColor']));

	$custom_attribute = $xml->createElement('custom-attribute');									
    $custom_attributes->appendChild($custom_attribute);
	$custom_attribute->setAttribute('attribute-id', 'showCountdown');
	$custom_attribute->appendChild($xml->createTextNode($row['showCountdown']));

	$custom_attribute = $xml->createElement('custom-attribute');									
	$custom_attribute->setAttribute('attribute-id', 'smallParts');
	$custom_attribute->appendChild($xml->createTextNode($row['smallParts']));

	$custom_attribute = $xml->createElement('custom-attribute');									
    $custom_attributes->appendChild($custom_attribute);
	$custom_attribute->setAttribute('attribute-id', 'supplierName');
	$custom_attribute->appendChild($xml->createTextNode($row['supplierName']));

	$custom_attribute = $xml->createElement('custom-attribute');									
    $custom_attributes->appendChild($custom_attribute);
	$custom_attribute->setAttribute('attribute-id', 'supplierSKU');
	$custom_attribute->appendChild($xml->createTextNode($row['supplierSKU']));

	$classification_category = $xml->createElement('classification-category');									
    $product->appendChild($classification_category);
	$classification_category->appendChild($xml->createTextNode($row['classification_category']));

    $xml->formatOutput = true;
    $xml->save("C://uploads/".$xml_fname);

  }
  
 echo "XML File Created";

?>
loadAndEdit2.xml
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros