Hi, I use this script to extract and insert the desired variables to mysql.It does the job.

$xml = new SimpleXMLElement('file.xml');
foreach($xml->loanaccount as $mess){
    $account_number= mysql_real_escape_string($mess->account_number);
    $main= mysql_real_escape_string($mess->main);
    $type= mysql_real_escape_string($mess->{'application-array'}->{'application-type'}),
    $date= mysql_real_escape_string($mess->{'application-array'}->{'application-date'}),
//insert into databse                     
    mysql_query("INSERT INTO account (account_number, main) VALUES ('$account_number', '$main')")
or die(mysql_error());
    echo "inserted into mysql<br /><br />";
//show updated records            
    printf ("Records inserted: %d\n", mysql_affected_rows());  
}

The problem I have is that the xml can contain several application-array and I need to insert the application-date and application-type from the most recent application-array.

This is what the xml looks like.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<account-information report-date="2012-12-31T23:59:59.000+01:00" xmlns="http://www.XXX.SE/XXX">
    <account>
        <account-number>00000000</account-number>
        <status>XXX</status>
        <application-array>
            <application-type>NEW</application-type>
            <application-date>XXX</application-date>
        </application-array>
        <application-array>
            <application-type>TOP</application-type>
            <application-date>XXX</application-date>
        </application-array>
        <account-information-type>
            <first-name-main></first-name-main>
        </account-information-type>
    </account>
</account-information>

So how do I change my foreach statement to capture the most recent application-type and date from the application array.

Thanks
Adamski

Member Avatar for diafol
$xml = simplexml_load_file('me.xml');
$x = 0;
foreach($xml->account as $mess){
    $date = '';
    foreach($mess->{'application-array'} as $aa){
        if((string) $aa->{'application-date'} > $date){
            $output[$x] = array($aa->{'application-date'}, $aa->{'application-type'});  
            $date = $aa->{'application-date'};  
        }
    }
    $x++;   
}
echo "<pre>";
print_r($output);
echo "</pre>";

With this sort of XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<account-information report-date="2012-12-31T23:59:59.000+01:00" xmlns="http://www.XXX.SE/XXX">
    <account>
        <account-number>00000000</account-number>
        <status>BORN</status>
        <application-array>
            <application-type>NEW</application-type>
            <application-date>2013-09-18</application-date>
        </application-array>
        <application-array>
            <application-type>TOP</application-type>
            <application-date>2014-09-18</application-date>
        </application-array>
        <account-information-type>
            <first-name-main></first-name-main>
        </account-information-type>
    </account>
     <account>
        <account-number>00000001</account-number>
        <status>DEAD</status>
        <application-array>
            <application-type>NEWEST</application-type>
            <application-date>2005-09-18</application-date>
        </application-array>
        <application-array>
            <application-type>TIPS</application-type>
            <application-date>2010-09-18</application-date>
        </application-array>
        <account-information-type>
            <first-name-main></first-name-main>
        </account-information-type>
    </account>
</account-information>

Gives:

Array
(
    [0] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [0] => 2014-09-18
                )

            [1] => SimpleXMLElement Object
                (
                    [0] => TOP
                )

        )

    [1] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [0] => 2010-09-18
                )

            [1] => SimpleXMLElement Object
                (
                    [0] => TIPS
                )

        )

)

Which is what I was expecting. Not sure if that's what you want though.

That's what I'm looking for! Now I tried to accomadate it to insert it into mysql db but nothing happends, no errors or nothing. And if I look att the printed output it only shows one (last) account from the xml but even that line doesn't get inserted into my db.

Maybe I'm going about it the wrong way but here's what I tried:

$x = 0;
foreach($xml->account as $mess){
 $date = '';
 $output = array();
  $account_number= $mess->{'account-number'};
  $firstname= $mess->{'information-type'}->{'first-name-main'};
  $lastname= $mess->{'information-type'}->{'last-name-main'};
  $email= $mess->{'information-type'}->{'email-main'};
        foreach($mess->{'application-array'} as $aa){
            if((string) $aa->{'application-date'} > $date){
                $output[$x] = '('.$account_number.',"'.mysql_real_escape_string($firstname).'","'.mysql_real_escape_string($lastname).'","'.mysql_real_escape_string($email).'", "'.mysql_real_escape_string($aa->{'application-type'}).'",'. $aa->{'application-date'}.')';  
                $date = $aa->{'application-date'};  
        }
    }
    $x++;
}  
$sql = 'INSERT INTO ignore delay account (`account_number`, `first_name`, `last_name`, `email`, `application-type`, `application-date`) VALUES '.implode(',', $output)or die(mysql_error());

mysql_query($sql);  

echo "<pre>";
print_r($sql);
echo "</pre>";/* 

Thanks
/Adam

Member Avatar for diafol
foreach($xml->account as $mess){
 $date = '';
 $output = array();

to

$output = array();
foreach($xml->account as $mess){
 $date = '';
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.