I read many useful answers here and the most of them helped me to create script for importing data from XML file. Problem is that this time i have additional part with images url's witch also needs to be updated into MySQL.
For now, i update MySQL with data from XML, after that use other script to download needed images. After website with XML feed changed they structure, i have problem to figure out how to update images url in array into MySQL row so i can download images after that.
Even i choose to download images without updating into MySQL, i also can't download them.
This is XML structure
<ArrayOfUnitDTO xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<UnitDTO>
<Category>Category name</Category>
<code>863</code>
<Status>Blocked</Status>
<RefNo>12345</RefNo>
<Images>
<Image>
<Title>Community</Title>
<ImageURL>http://imageaddress.com/images/watermark.aspx?imageurl=/uf/1015/GroupUpdate/unit/350/350_Image.jpg&width=640&group=1015&module=1&watermarktype=default&position=Center</ImageURL>
</Image>
<Image>
<Title>Local Area Image</Title>
<ImageURL>http://imageaddress.com/images/watermark.aspx?imageurl=/uf/1015/GroupUpdate/unit/631/631_Image.jpg&width=640&group=1015&module=1&watermarktype=default&position=Center</ImageURL>
</Image>
</Images>
</UnitDTO>
</ArrayOfUnitDTO>
I am i am using this
foreach ($listings ->UnitDTO as $listingInfo) //loop read xml
{
$RefNo = $listingInfo->RefNo;
$Category = $listingInfo->Category;
$code = $listingInfo->code;
$Status = $listingInfo->Status;
mysqli_query($link,"REPLACE INTO UnitDTO (`RefNo`, `Category`, `code`, `Status`) VALUES ('$RefNo', '$Category', '$code', '$Status',)") or die(mysqli_error($link));
}
No metter what i tried, i can't import images url into imageurl MySQL row in array.
to get image url from above xml you have to run one more foreach loop, try something like below code and use it as per your requirement.
foreach ($listings ->UnitDTO as $listingInfo){
$RefNo = $listingInfo->RefNo;
$Category = $listingInfo->Category;
$code = $listingInfo->code;
$Status = $listingInfo->Status;
foreach ($listingInfo->Images->Image as $image){
echo $image->ImageURL;
}
}
Related
I'm trying to get the data from an XML file into an array so that I can import it via 'Magmi'. Using the following code, I'm working with a 3.6GB XML file.
<?php
$z = new XMLReader;
$z->open('wpcatsub.xml');
$doc = new DOMDocument;
// move to the first <App /> node
while ($z->read() && $z->name !== 'App');
// now that we're at the right depth, hop to the next <App/> until the end of the tree
while ($z->name === 'App')
{
// either one should work
//$node = new SimpleXMLElement($z->readOuterXML());
$node = simplexml_import_dom($doc->importNode($z->expand(), true));
// now you can use $node without going insane about parsing
var_dump($node->element_1);
// go to next <product />
$z->next('App');
}
?>
When I load the PHP file, no errors appear -- the page is just blank. My XML data structure is below...
<App action="A" id="1">
<BaseVehicle id= "17491"/>
<Note><![CDATA[License Plate Lamp]]></Note>
<Qty>.000</Qty>
<PartType id= "10043"/>
<Part>W0133-1620896</Part>
<Product>
<PartNumber>W0133-1620896</PartNumber>
<BrandID>OES</BrandID>
<BrandDescription><![CDATA[Genuine]]></BrandDescription>
<WorldpacCategoryID>P9032</WorldpacCategoryID>
<Price>29.85</Price>
<ListPrice>33.17</ListPrice>
<Available>Y</Available>
<OEFlag>OEM</OEFlag>
<Weight>.10</Weight>
<Height>.7</Height>
<Width>4.4</Width>
<Length>4.4</Length>
<SellingIncrement>1</SellingIncrement>
<Popularity>D</Popularity>
<ImageURL><![CDATA[http://img.eautopartscatalog.com/live/W01331620896OES.JPG]]></ImageURL>
<ThumbURL><![CDATA[http://img.eautopartscatalog.com/live/thumb/W01331620896OES.JPG]]></ThumbURL>
</Product>
<ImageURL><![CDATA[http://img.eautopartscatalog.com/live/W01331620896OES.JPG]]></ImageURL>
<ThumbURL><![CDATA[http://img.eautopartscatalog.com/live/thumb/W01331620896OES.JPG]]></ThumbURL>
</App>
Is it stalling because of the size of the file? If so, isn't XMLReader supposed to work for large XML files? If nothing else, what other options would I have?
I suppose I could load the XML data into a database if needed and then use SELECT queries to build the array for the MAGMI import. Though I'm not sure how to import an XML file into a SQL database. If need be, I'll be happy to get guidance with that.
while i am inserting data into database using normal xml tree structure data is successfully inserted but while i am trying to insert data into database using different xml structure it is not gives me any error but at the same time fields are created but i can't visualize content in table to,
<?xml version="1.0"?>
<xml>
<draw>
<candelete>yes</candelete>
<forpayroll>no</forpayroll>
<name>hello</name>
</draw>
</xml>
above xml format which successfully insert data into mysql database
below xml format is not allowing me to insert data into mysql database
<ENVELOPE>
<HEADER>
<VERSION>1</VERSION>
<STATUS>1</STATUS>
</HEADER>
<BODY>
<DESC>
</DESC>
<DATA>
<TALLYMESSAGE>
<LEDGER NAME="Dena" RESERVEDNAME="" ID="2240" REQNAME="dena">
<PARENT TYPE="String">Bank Accounts</PARENT>
<TAXTYPE TYPE="String">Others</TAXTYPE>
<ISBILLWISEON TYPE="Logical">No</ISBILLWISEON>
<ISCOSTCENTRESON TYPE="Logical">No</ISCOSTCENTRESON>
<ISREVENUE TYPE="Logical">No</ISREVENUE>
<ISDEEMEDPOSITIVE TYPE="Logical">Yes</ISDEEMEDPOSITIVE>
<CANDELETE TYPE="Logical">Yes</CANDELETE>
<FORPAYROLL TYPE="Logical">No</FORPAYROLL>
<MASTERID TYPE="Number"> 2240</MASTERID>
<TNETBALANCE TYPE="Amount">0.00</TNETBALANCE>
<LANGUAGENAME.LIST>
<NAME.LIST TYPE="String">
<NAME>Dena</NAME>
</NAME.LIST>
<LANGUAGEID TYPE="Number">0</LANGUAGEID>
</LANGUAGENAME.LIST>
</LEDGER>
</TALLYMESSAGE>
</DATA>
</BODY>
</ENVELOPE>
below is my php insert connection code
<?php
$con = mysql_connect("localhost:3306","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("temp", $con);
if(!$xml=simplexml_load_file('./xml/data.xml')){
trigger_error('Error reading XML file',E_USER_ERROR);
}
foreach ($xml as $syn)
{
$candelete = $syn->candelete;
$forpayroll = $syn->forpayroll;
$name = $syn->name;
$sql = "INSERT INTO vtiger (candelete, forpayroll, name) VALUES ('$candelete','$forpayroll','$name')";
$query = mysql_query($sql);
if (!$query)
{
echo ('Error: ' . mysql_error());
}
else
{
echo "Record added";
}
}
mysql_close($con);
?>
I'd like you to look at your foreach loop to read xml:
foreach ($xml as $syn)
{
$candelete = $syn->candelete;
$forpayroll = $syn->forpayroll;
$name = $syn->name;
as you see here you decleare xml nodes wich are totally different in new xml, so you need to parse your document and get all relevant data to build the query. A good approch would be to loop among <TALLYMESSAGE> to retrieve all relevant data and store into variables wich you will be able to use for the new query.
You can use simplexml_load_file to do that.
Documentation: here
NOTE You query also must be changed according with new xml, you will need a new table with a correct schema.
UPDATED
You can still use the same way to parse file but you need to change your script and you will end up with more lines of code to parse the entire new xml. As you can see structure is totally different hand have many child nodes wich cannot be retrieved by using a simple foreach as you did for the first file wich didn't have any child nodes. That's why you really should use simplexml_load_file, less work
You need to think more about your foreach loop, bcause in your working xml file thats only one array so you can retrieve data using only one foreach loop, but your another xml file i think it contains more array so you need to create one or more foreach loop according to its structure.
you define your conection to mysql in $con
you must also use mysql_query($sql, $con);
I have a local server , where i am receiving xml files. The xml files are received just fine but i have problem on how to pass my XML file into my database.
I retrieve the xml file simple as that :
$xml_post = file_get_contents('php://input');
The xml files i receive looks like this :
<city>
<id>1081</id>
<name>athens</name>
<country>Greece</country>
<info>etc etc etc</info>
</city>
I have manually created a database with phpmyadmin with the exact same nodes so i can save everything correctly.
Now i want to insert what i am getting in the database with mysql_query.
Something like this :
mysql_query("INSERT INTO cities (id, city ,country , info)
VALUES (4 , 'athens' , 'greece' , 'etc etc etc'");
But which are the variables that i have stored my xml to use them as values in the statement above? How can i parse the xml file that i have to store all the nodes in variables and then pass them correctly to the database?
Try using simplexml_load_string()
Example usage:
<?php
$xml = '<city>
<id>1081</id>
<name>athens</name>
<country>Greece</country>
<info>etc etc etc</info>
</city>
';
$xml = simplexml_load_string($xml);
//1081
echo $xml->id;
//athens
echo $xml->name;
?>
If you have multiple nodes within your XML each node will be an array of objects:
$xml = '
<citys>
<city>
<id>1081</id>
<name>athens</name>
<country>Greece</country>
<info>etc etc etc</info>
</city>
<city>
<id>1082</id>
<name>somwhere else</name>
<country>Spain</country>
<info>etc etc etc</info>
</city>
</citys>
';
$xml = simplexml_load_string($xml);
//print_r($xml);
foreach($xml->city as $val){
echo $val->name;
}
//or
echo $xml->city[0]->name;
//athens
echo $xml->city[1]->name;
You would need to parse the XML into data readable by PHP, and then use that data in your SQL insert.
PHP SimpleXML Tutorial might be a place to start.
I have a XML (simplified) like this:
<article>
<title>My Article</title>
<image src="someurl.jpg" />
<image src="someotherurl.jpg" />
</article>
How do I select the <image> elements? They have the same name. To select the <title> i simply do this:
$xml = simplexml_load_file( "theurltomyxml.xml" );
$article = $xml->article;
$title = $article->title;
But how do I get the images? They have the same name! Just writing $article->image won't work.
I know this is an older question/answer but I had a similar issue and solved it by using the second solution by ajreal with a few adjustments of my own. I had a series of top level nodes (the xml was not formatted properly and didn't split the elements into parent nodes - out of my control). So I used a for loop that counts the elements then used ajreal's solution to echo back the contents I wanted with the iteration of $i.
My use was a bit different than above so I've tried to change it to make it more relevant to your images issue. Anyone please let me know if I made a mistake.
$campaigns = $xml->children();
for($i=0;$i<=$campaigns->count();$i++){
echo $campaigns[$i]->article->title . $campaigns[$i]->article->image[0];
}
You can do this :-
foreach ($xml->xpath("/article/image") as $img)
{
...
}
Or (is list of image node, so normal way of access array is workable)
$xml->image[0];
$xml->image[1];
I have flash photo gallery with simple PHP uploader.
In uploader.php file I have added code which store last 20 added photos into xml file.
The script for saving data in xml file I have already done, and xml structure looks like this:
<images>
<image thumb="/content/photos/tn_PICTURE1.jpg" image="/content/photos/PICTURE1.jpg" name="PICTURE1"/>
<image thumb="/content/photos/tn_PICTURE2.jpg" image="/content/photos/PICTURE2.jpg" name="PICTURE2"/>
<image thumb="/content/photos/tn_PICTURE3.jpg" image="/content/photos/PICTURE3.jpg" name="PICTURE3"/>
...
<image thumb="/content/photos/tn_PICTURE20.jpg" image="/content/photos/PICTURE20.jpg" name="PICTURE20"/>
</images>
The part of uploader.PHP file:
$domtree = new DOMDocument('1.0', 'UTF-8');
$domtree->load("new_files.xml");
$root = $domtree->documentElement;
$currentImage = $domtree->createElement("image");
$currentImage = $root->appendChild($currentImage);
$thumb = $domtree->createAttribute("thumb");
$currentImage->appendChild($thumb);
$thumbValue = $domtree->createTextNode($thumbnail);
$thumb->appendChild($thumbValue);
$imagelink = $domtree->createAttribute("image");
$currentImage->appendChild($imagelink);
$imageValue = $domtree->createTextNode($fullpath);
$imagelink->appendChild($imageValue);
$imagename = $domtree->createAttribute("name");
$currentImage->appendChild($imagename);
$imagenameValue = $domtree->createTextNode($imageName);
$imagename->appendChild($imagenameValue);
$domtree->save("new_files.xml");
What I want to do?
When is more than 20 pictures in xml file, then I want that script will automaticaly deleting last one (oldest), and put the newest one as first. So the result will looks like this:
<images>
<image thumb="/content/photos/tn_PICTURE21.jpg" image="/content/photos/PICTURE21.jpg" name="PICTURE21"/>
<image thumb="/content/photos/tn_PICTURE1.jpg" image="/content/photos/PICTURE1.jpg" name="PICTURE1"/>
<image thumb="/content/photos/tn_PICTURE2.jpg" image="/content/photos/PICTURE2.jpg" name="PICTURE2"/>
...
<image thumb="/content/photos/tn_PICTURE19.jpg" image="/content/photos/PICTURE19.jpg" name="PICTURE19"/>
</images>
Any suggestions?
Ps. Sorry for my bad english :)
Regards, Artur.
Why not use the SimpleXML object? It is far easier to use if you are just dealing with XML.
After that, all you need to do is load up the XML file and do something like this:
$uploadedFilesCount = count($uploadedFiledArray);
$currentIndex = 0;
while($totalIndex < $uploadedFilesCount){
foreach($YourSweetXMLObject->children() as $child){
$child->attributes()->thumbs = $uploadedFiledArray[$currentIndex]['thumbs'];
$child->attributes()->image = $uploadedFiledArray[$currentIndex]['image'];
$child->attributes()->name = $uploadedFiledArray[$currentIndex]['name'];
$totalIndex++;
}
}
The only problem is, if you upload again, it will start from the top of your XML object. If you want to be able to start from a different point in your XML file, you are going to need to store more information.
Hope that helps.