开发者

loop in simplexml load file

I am using the following code to parse the XML data to MySQL table and it is working as expected.

<?php
$sxe = simplexml_load_file("$myfile");
foreach($sxe->AUTHAD as $sales) {

$authad="insert into test.authad values ('".mysql_real_escape_string($sales->LOCALDATE)."','".mysql_real_escape_string($sales->LOCALTIME)."','".mysql_real_escape_string($sales->TLOGID)."','" ...

?>

The problem is that when I get a new xml file with different format, I can not use the above insert into statement and have to change the parameters manually. For e.g.

$authadv_new="insert into test.authad values ('".mysql_real_escape_string($sales->NEW1)."','".mysql_real_escape_string($sales->NEW2)."','".mysql_real_escape_string($sales->NEW3)."','" ...

Is th开发者_运维问答ere any way to automate this? I mean the PHP code should be able to anticipate the parameters and generate the mysql_real_escape_string($sales->NEW1) to NEW10 values using a loop.


While satrun77's example works fine, you can use slightly less code and stick to the advantages of the simplexml object by using its "children()" method:

$sxe = simplexml_load_string($xml);
foreach($sxe->AUTHAD as $sales) {
        foreach ($sales->children() as $child) {
                $children[]="'".mysql_real_escape_string($child)."'";
        }
        $authad="insert into test.authad values (".implode($children,",").");";
}


Maybe something like this using a dynamic variable $var?

<?php
$values = array();
for($i = 1; $i < 11; $i++) {
    $var = "NEW".$i;
    $values[] = "'".mysql_real_escape_string($sales->$var)."'";
}
$string = join(",", $values);


I hope this is what you are looking for?

$string = '<?xml version="1.0" encoding="UTF-8" ?> 
<root>
   <AUTHAD>
      <NEW1>New 1</NEW1>
      <NEW2>New 2</NEW2>
      <NEW3>New 3</NEW3>
      <NEW4>New 4</NEW4>
      <NEW5>New 5</NEW5>
      <NEW6>New 6</NEW6>
   </AUTHAD>
   <AUTHAD>
      <NEW1>New 11</NEW1>
      <NEW2>New 22</NEW2>
      <NEW3>New 33</NEW3>
   </AUTHAD>
</root>
'; 

$sxe = simplexml_load_string($string);
foreach($sxe->AUTHAD as $sales) {
    if (count($sales) > 0) {
        $values = array();
        foreach($sales as $key => $sale) {
            $values[] = "'" . mysql_real_escape_string($sale) . "'";
        }
        $authadv_new="insert into test.authad values (" . join(', ', $values) . ")";
        echo $authadv_new.'<br >';
    }
}


You're looking for get_object_vars() to iterate over the object members:

<?php

mysql_connect( 'localhost', 'root', 'moin' );

$sxe = simplexml_load_file( $argv[1] );
var_dump( $sxe );

$sql = "insert into test.authad values (";

foreach($sxe->AUTHAD as $sales) {
    $members = get_object_vars( $sales );
    $values = array();
    foreach ( $members as $name => $value ) {
        echo "$name : $value\n";
        $values[] = "'" . mysql_real_escape_string( $value ) . "'";
    }
    $sql .= join( $values, ', ' );
}

$sql .= ")\n";
echo $sql;

The input is this file:

<sxe>
    <AUTHAD>
        <eins>one</eins>
        <zwei>two':-)</zwei>
        <drei>three;"'</drei>
    </AUTHAD>
</sxe>

I'm getting the following result here:

insert into test.authad values ('one', 'two\':-)', 'three;\"\'')


If you're not concerned about the order of data between each XML you can just implode the object rather than pick out the specific values each time. Doing an implode also allows you to avoid the loop.

INSERT INTO test.authad VALUES('".implode("','", mysql_real_escape_string($sxe->AUTHAD))."')

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜