PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
<?
// first two functions are generic XML processing functions
// they can handle any XML tree, converting the data to
// a output array, it's better to keep things in serialized
// arrays than processing the slow XML data again and again!
function process_xml ( $in )
{
$xml = xml_parser_create ( 'ISO-8859-1' );
xml_parser_set_option ( $xml, XML_OPTION_SKIP_WHITE, 1 );
xml_parse_into_struct ( $xml, $in, $value, $name );
xml_parser_free ( $xml );
$x = 0;
$part = array ();
$part[$value[$x]['tag']] = get_extended ( $value, $x );
return ( $part );
}
function get_extended ( $next, &$i )
{
$node = array();
$j = 0;
if ( isset ( $next[$i]['value'] ) && $next[$i]['value'] )
{
array_push ( $node, $next[$i]['value'] );
}
$old = '';
while ( ++$i < count ( $next ) )
{
switch ( $next[$i]['type'] )
{
case 'cdata' :
array_push ( $node, $next[$i]['value'] );
break;
case 'complete' :
$node[$next[$i]['tag']] = $next[$i]['value'];
break;
case 'open' :
$j++;
if ( $old <> $next[$i]['tag'] )
{
$j = 0;
$old = $next[$i]['tag'];
}
$node[$next[$i]['tag']][$j] = get_extended ( $next, $i );
break;
case 'close' :
return ( $node );
}
}
}
// this is the data to insert in the databse
// carrier name, path and image logo name
// if you don't want to insert the image paths just remove
// it from the data array...
$data = array ( 'CARRIERDESCRIPTION', 'CARRIERLOGO' );
// the root that hold the data you want -> array()
$root = 'child';
// the child element that holds the array of data to grab -> array()
$child = 'attrs';
// builds and holds the database insert array
$hold = array ();
// get the xml file, URL or local path and name
$file = file_get_contents ( 'data.xml' );
// feed it to the XML parser!
$out = process_xml ( $file );
// process the data...
for ( $i = 0; $i < sizeof ( $out ); $i++ )
{
$next = $out[$i][$root];
for ( $h = 0; $h < sizeof ( $next ); $h++ )
{
$temp = array ();
foreach ( $data AS $v )
{
$temp[] = addslashes ( trim ( $next[$h][$child][$v] ) );
}
}
$hold[] = "( '" . implode ( "', '", $temp ) . "' )";
}
// just to show what would be inserted
// we do it this way so we do only (1) insert, no loops!
echo implode ( ', ', $hold );
/* uncomment this to do the real insert
mysql_connect ( 'localhost', 'admin', 'pass' );
mysql_select_db ( 'database_name' );
$sql = "INSERT INTO carrier VALUES " . implode ( ', ', $hold );
mysql_query ( $sql );
*/
?>
<? for ( $i = 0; $i < sizeof ( $out ); $i++ )
{
$next = $out[$i][$root];
for ( $h = 0; $h < sizeof ( $next ); $h++ )
{
$temp = array ();
foreach ( $data AS $v )
{
$temp[] = addslashes ( trim ( $next[$h][$child][$v] ) );
}
}
$hold[] = "( '" . implode( "', '", $temp ) . "' )";
}
// just to show what would be inserted
// we do it this way so we do only (1) insert, no loops!
echo implode( ', ', $hold ); ?>
is not making arrays... can anyone help me with this? I thought by using $temp[] makes an array???
// first two functions are generic XML processing functions
// they can handle any XML tree, converting the data to
// a output array, it's better to keep things in serialized
// arrays than processing the slow XML data again and again!
function process_xml ( $in )
{
$xml = xml_parser_create ( 'ISO-8859-1' );
xml_parser_set_option ( $xml, XML_OPTION_SKIP_WHITE, 1 );
xml_parse_into_struct ( $xml, $in, $value, $name );
xml_parser_free ( $xml );
$x = 0;
$part = array ();
$part[$value[$x]['tag']] = get_extended ( $value, $x );
return ( $part );
}
function get_extended ( $next, &$i )
{
$node = array();
$j = 0;
if ( isset ( $next[$i]['value'] ) && $next[$i]['value'] )
{
array_push ( $node, $next[$i]['value'] );
}
$old = '';
while ( ++$i < count ( $next ) )
{
switch ( $next[$i]['type'] )
{
case 'cdata' :
array_push ( $node, $next[$i]['value'] );
break;
case 'complete' :
$node[$next[$i]['tag']] = $next[$i]['value'];
break;
case 'open' :
$j++;
if ( $old <> $next[$i]['tag'] )
{
$j = 0;
$old = $next[$i]['tag'];
}
$node[$next[$i]['tag']][$j] = get_extended ( $next, $i );
break;
case 'close' :
return ( $node );
}
}
}
// this is the data to insert in the databse
// carrier name, path and image logo name
// if you don't want to insert the image paths just remove
// it from the data array...
$data = array ( 'CARRIERDESCRIPTION', 'CARRIERLOGO' );
// the root that hold the data you want -> array()
$root = 'child';
// the child element that holds the array of data to grab -> array()
$child = 'attrs';
// builds and holds the database insert array
$hold = array ();
// get the xml file, URL or local path and name
$file = file_get_contents ( 'cdf.xml' );
// feed it to the XML parser!
$out = process_xml ( $file );
// process the data...
for ( $i = 0; $i < sizeof ( $out ); $i++ )
{
$next = $out[$i][$root];
for ( $h = 0; $h < sizeof ( $next ); $h++ )
{
$temp = array ();
if(is_array($data))
{
foreach ( $data AS $v )
{
$temp[] = addslashes ( trim ( $next[$h][$child][$v] ) );
}
} else
{
echo "no data in Data array";
}
}
if(is_array($temp))
{
$hold[] = "( '" . implode( "', '", $temp ) . "' )";
}else
{
echo "no data in Temp array";
}
}
// just to show what would be inserted
// we do it this way so we do only (1) insert, no loops!
echo implode( ', ', $hold );