doctorbill
asked on
xml and php
I am trying to import the attached xml file into a mysql database using php
The problem is that there are records that are repeated in the file
Can someone please give me some pointers please
Purchase-Order-FI2-4098098.xml
The problem is that there are records that are repeated in the file
Can someone please give me some pointers please
Purchase-Order-FI2-4098098.xml
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This is the data I need inserting:
[body] => SimpleXMLElement Object
(
[ORDER] => SimpleXMLElement Object
(
[ORDERHEAD] => SimpleXMLElement Object
(
[@attributes] => Array
(
[ORDERTYPE] => PUO
[FUNCCODE] => FIO
)
[ORDERDESC] => Purchase Order
[FUNCDESC] => Firm Order
[CURRENCY] => SimpleXMLElement Object
(
[@attributes] => Array
(
[CURRTYPE] => ORD
[CURRCODE] => GBP
)
[CURRTYPEDESC] => Order Currency
[CURRCODESC] => United Kingdom Pounds
)
)
[REFERENCE] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[REFTYPE] => CUR
)
[REFDESC] => Customers Reference
[REFCODE] => STKPO434968
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[REFTYPE] => CCN
)
[REFDESC] => Customers Customer Name
[REFCODE] => Mrs V. M BAKER
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[REFTYPE] => ACR
)
[REFDESC] => Alternate Customer Reference
[REFCODE] => 001A002130
)
)
My table is just consisting of col1,col2 etc...
It is called orders_db
database: postorders
Sorry to appear so vague but this is totally new to me. If I can have a pointer as to the structure of the php code to do this I could then work on it. Is it possible to put the code (above) together to show me how it all works as a unit ?
[body] => SimpleXMLElement Object
(
[ORDER] => SimpleXMLElement Object
(
[ORDERHEAD] => SimpleXMLElement Object
(
[@attributes] => Array
(
[ORDERTYPE] => PUO
[FUNCCODE] => FIO
)
[ORDERDESC] => Purchase Order
[FUNCDESC] => Firm Order
[CURRENCY] => SimpleXMLElement Object
(
[@attributes] => Array
(
[CURRTYPE] => ORD
[CURRCODE] => GBP
)
[CURRTYPEDESC] => Order Currency
[CURRCODESC] => United Kingdom Pounds
)
)
[REFERENCE] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[REFTYPE] => CUR
)
[REFDESC] => Customers Reference
[REFCODE] => STKPO434968
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[REFTYPE] => CCN
)
[REFDESC] => Customers Customer Name
[REFCODE] => Mrs V. M BAKER
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[REFTYPE] => ACR
)
[REFDESC] => Alternate Customer Reference
[REFCODE] => 001A002130
)
)
My table is just consisting of col1,col2 etc...
It is called orders_db
database: postorders
Sorry to appear so vague but this is totally new to me. If I can have a pointer as to the structure of the php code to do this I could then work on it. Is it possible to put the code (above) together to show me how it all works as a unit ?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ray - thanks so much for the code - it really explains exactly what is going on
Final question:
If my database has a name "dbname" and columns called "col1", "col2" etc ..., what would the insert php code look like to take the above echoed variables into the databse
Final question:
If my database has a name "dbname" and columns called "col1", "col2" etc ..., what would the insert php code look like to take the above echoed variables into the databse
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
basically how to use a sql INSERT statement to put the variables into a database
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Last question I promise:
How would the $r variables be inserted as repeats into the table on line 22-23?
How would the $r variables be inserted as repeats into the table on line 22-23?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This is my attempt to get 2 variables into the database (sorry - I know it is one more question):
-------------------------- -----
<?php
//Connect to the database
$dbh = new PDO("mysql:host=localhost; dbname=1ca llorders", "name", "password");
error_reporting(E_ALL);
echo '<pre>';
// ACQUIRE THE XML AND MAKE AN OBJECT
$url = 'http://localhost/comwebsites/1callorders/site/xmlimport/Purchase-Order-FI2-4098098.xml';
$xml = file_get_contents($url);
$obj = simplexml_load_string($xml );
// SHOW HOW TO ACCESS SOME PARTS OF THE OBJECT
$x = (string)$obj->header->deli very->mess age->messa geID;
echo "messageID: $x";
echo PHP_EOL;
$x = (string)$obj->header->mani fest->docu ment->name ;
echo "name: $x";
echo PHP_EOL;
// SHOW HOW TO USE AN ITERATOR TO ACCESS REPEATED PARTS OF THE OBJECT
foreach ($obj->body->ORDER->REFERE NCE as $r)
{
echo ' REFTYPE: ' . $r['REFTYPE'];
echo ' REFCODE: ' . $r->REFCODE;
echo PHP_EOL;
}
foreach ($obj->body->ORDER->SUPPLI ER->ADDRES S->NAME as $n)
{
echo "NAME: $n";
echo PHP_EOL;
}
// USE AN ITERATOR TO ACCESS A NESTED OBJECT
foreach ($obj->body->ORDER->ORDERL INE as $o)
{
// TEASE OUT PARTS OF THE NESTED OBJECT
$p = (string)$o->PRODUCT->DESCR IPTION;
echo "PRODUCT DESCRIPTION: $p";
echo PHP_EOL;
}
// ACTIVATE THIS TO SHOW THE ENTIRE OBJECT
// print_r($obj);
//prepare the query
$stmt = $dbh->prepare("INSERT INTO retailers (ret_id, ret_name) VALUES (:$x, :$p)");
$stmt->execute();
?>
Result:
I just get this printed to the page:
messageID:
name:
--------------------------
<?php
//Connect to the database
$dbh = new PDO("mysql:host=localhost;
error_reporting(E_ALL);
echo '<pre>';
// ACQUIRE THE XML AND MAKE AN OBJECT
$url = 'http://localhost/comwebsites/1callorders/site/xmlimport/Purchase-Order-FI2-4098098.xml';
$xml = file_get_contents($url);
$obj = simplexml_load_string($xml
// SHOW HOW TO ACCESS SOME PARTS OF THE OBJECT
$x = (string)$obj->header->deli
echo "messageID: $x";
echo PHP_EOL;
$x = (string)$obj->header->mani
echo "name: $x";
echo PHP_EOL;
// SHOW HOW TO USE AN ITERATOR TO ACCESS REPEATED PARTS OF THE OBJECT
foreach ($obj->body->ORDER->REFERE
{
echo ' REFTYPE: ' . $r['REFTYPE'];
echo ' REFCODE: ' . $r->REFCODE;
echo PHP_EOL;
}
foreach ($obj->body->ORDER->SUPPLI
{
echo "NAME: $n";
echo PHP_EOL;
}
// USE AN ITERATOR TO ACCESS A NESTED OBJECT
foreach ($obj->body->ORDER->ORDERL
{
// TEASE OUT PARTS OF THE NESTED OBJECT
$p = (string)$o->PRODUCT->DESCR
echo "PRODUCT DESCRIPTION: $p";
echo PHP_EOL;
}
// ACTIVATE THIS TO SHOW THE ENTIRE OBJECT
// print_r($obj);
//prepare the query
$stmt = $dbh->prepare("INSERT INTO retailers (ret_id, ret_name) VALUES (:$x, :$p)");
$stmt->execute();
?>
Result:
I just get this printed to the page:
messageID:
name:
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
that works !!!
I have tried this for an extra variable but it gives an error:
Name: P-ORD-EXPDesc: PHP Warning: PDOStatement::execute() [function.PDOStatement-exe cute]: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in I:\CommercialWebsites\1cal lorders\si te\xmlimpo rt.php on line 20
Extra variable I am trying to add:
<ORDERDESC>Purchase Order</ORDERDESC>
---------
<?php
$dbh = new PDO("mysql:host=localhost; dbname=1ca llorders", "user", "password");
$xml = file_get_contents('xmlimpo rt/Purchas e_Order_FI 2-4098098. xml');
$obj = simplexml_load_string($xml );
$name = (string)$obj->header->mani fest->docu ment->name ;
$orderdesc = (string)$obj->header->mani fest->docu ment->orde rdesc;
echo "Name: " . $name;
echo "Desc: " . $orderdesc;
//prepare the query with a named parameter called 'name'
$stmt = $dbh->prepare("INSERT INTO retailers (ret_name, ret_add) VALUES (:name, :orderdesc)");
//bind the named parameter to a PHP variable (called $name)
$stmt->bindParam('name', $name);
$stmt->bindParam('desc', $orderdesc);
//execute the query
$stmt->execute();
?>
I have tried this for an extra variable but it gives an error:
Name: P-ORD-EXPDesc: PHP Warning: PDOStatement::execute() [function.PDOStatement-exe
Extra variable I am trying to add:
<ORDERDESC>Purchase Order</ORDERDESC>
---------
<?php
$dbh = new PDO("mysql:host=localhost;
$xml = file_get_contents('xmlimpo
$obj = simplexml_load_string($xml
$name = (string)$obj->header->mani
$orderdesc = (string)$obj->header->mani
echo "Name: " . $name;
echo "Desc: " . $orderdesc;
//prepare the query with a named parameter called 'name'
$stmt = $dbh->prepare("INSERT INTO retailers (ret_name, ret_add) VALUES (:name, :orderdesc)");
//bind the named parameter to a PHP variable (called $name)
$stmt->bindParam('name', $name);
$stmt->bindParam('desc', $orderdesc);
//execute the query
$stmt->execute();
?>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks to all - I really do appreciate the time you have all spent teaching me in this exercise.
ASKER
Ray,
I cannot access the article url above:
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
I get access denied
I cannot access the article url above:
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
I get access denied
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Excellent learning experience
ASKER
I am in the process of setting this up
How does the php code take the xml data and place it in the database? Can you show me using a dummy script so I get the just of the workings