0
votes

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);
?>    
3
please add some code of your import function, we cannot help you without code - Fabio
i have mension my database connection code above, just edited. (i want to include 2 3 specific fields only so taken those fields only in connection) - Helping Hand..

3 Answers

1
votes

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

1
votes

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.

0
votes

you define your conection to mysql in $con you must also use mysql_query($sql, $con);