Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何循环遍历xml表行并通过属性名访问列值_Php_Xml_Laravel - Fatal编程技术网

Php 如何循环遍历xml表行并通过属性名访问列值

Php 如何循环遍历xml表行并通过属性名访问列值,php,xml,laravel,Php,Xml,Laravel,这是我的XML文件中数据的摘录 <?xml version="1.0" encoding="windows-1052" ?> <atp xmlns:atp="http://xml.alamte.com/atp"> <transaction> <table name="POS:Plus"> <row id="6">

这是我的XML文件中数据的摘录

<?xml version="1.0" encoding="windows-1052" ?>
<atp xmlns:atp="http://xml.alamte.com/atp">
  <transaction> 
    <table name="POS:Plus">
      <row id="6">
        <col name="LongDescription">TeTera Pinot </col>
        <col name="TillDescription">TeTera Pinot</col>
        <col name="TouchscreenDescription">TeTera Peroni</col>
      </row>
     </table>
  </transaction>
</atp>

提特拉比诺
提特拉比诺
佩罗尼河豚
代码

$data=json_decode(json_encode(simplexml_load_字符串(preg_replace('/[\x00-\x1F\x7F-\xFF]/','',Storage::get('xmls/imports/'.$filename))),TRUE);
if(isset($data['transaction']['table'])){
$tables=$data['transaction']['table'];
foreach($check\u table形式的表格){
如果(设置($check_table['@attributes']['name'])和&$check_table['@attributes']['name']=='STOCKPRO:PLUs')
$stock\u plus\u table=$check\u table;
if(设置($check_table['@attributes']['name'])&&&$check_table['@attributes']['name']=='POS:Plus')
$pos_plus_table=$check_table;
如果(设置($check_table['@attributes']['name'])&&&$check_table['@attributes']['name']=='POS:PLUPrices')
$pos_plu_prices_table=$check_table;
}
if(isset($pos_plu_prices_table)&&isset($pos_plu_prices_table['row'])){
$plu_prods=$pos_plu_prices_表['row'];
//使用POS:PLUPrices表进行价格更新
foreach($plu_prod作为$plu_prod){
如果(isset($plu_prod['col']))
$plu_prod=$plu_prod['col'];
其他的
$plu_prod=$plu_prods['col'];
$plu=$plu prod[0];
$price_level=$plu_prod[3];
$price=(float)preg_-replace('/[^0-9\.]/ui',''$plu_-prod[4]);
//检查我们的数据库中是否存在plu
if(Products::where('plu',$plu)->first()&($price_level==0 | |$price_level==1)){
$product=Products::where('plu',$plu)->first();
if(isset($stock\U plus\U table)){
$plu_child_prods=$stock_plus_table['row'];
//使用STOCKPRO:PLUs表进行子产品库存更新
foreach($plu_child_prods as$enter_item_2){
$enter_item_2_store=$enter_item_2;
如果(isset($enter_item_2['col']))
$enter_item_2=$enter_item_2['col'];
其他的
$enter_item_2=$plu_child_prods['col'];
//检查此项目是否为当前plu的子plu
如果($enter_item_2[27]==$plu){
$product_st=Products::where('plu',$enter_item_2_store['@attributes']['id'])->first();
如果($product){
$product\u st->parent=$product->id;
$product_st->pack=(int)$enter_item_2[28];
$product_st->save();
}
如果($product_st&&(int)$enter_item_2[28]!=0){
$product\U st->stock=地板($product->stock/(int)$enter\U item\U 2[28]);
$product_st->save();
}
}
}
}
//更新plu的价格
$pro_price=ProductPrices::where('product_id',$product->id)->orderBy('id','Desc')->first();
如果($pro_价格){
如果($price_level==0){
$pro_price->luc=$price;
}否则如果($price_level==1){
$pro_price->price=$price;
}
$pro_价格->保存();
}
$trini_update_products.=$product->id.',';
$trini_update_count++;
}否则{
//检查plu在我们的数据库中是否不可用
如果(!Products::where('plu',$plu)->first()){
$deleted=“True”;
$group='';
$subcategory='';
//在数据库中创建新的plu
$product=新产品();
if(isset($pos_+表格)){
$plu_match_prods=$pos_plus_table['row'];
//使用POS:Plus表获取产品名称信息
foreach($plu_match_prods as$enter_item_1){
如果(isset($enter_item_1['@attributes']['id'])&&$enter_item_1['@attributes']['id']==$plu){
$touch_screen_description=$enter_item_1[2];
$group=$enter_item_1[5];
$deleted=$enter_item_1[7];
$product->plu=$plu;
$product->name=$touch\u screen\u description;
$subcategory=SubCategories::where('product_group',$group)->orderBy('id','Desc')->first();
$product->category_id=$SUBSCATEGORY['category_id'];
$product->subcategory_id=$subcategory['id'];
}                                           
}   
}
if(isset($stock\U plus\U table)){
$plu_match2_prods=$stock_plus_table['row'];
//在STOCKPRO上工作:股票信息的PLUs表
foreach($plu_match2_prods as$enter_item_2){
如果(设置($enter_item_2['@attributes']['id'])和&$enter_item_2['@attributes']['id']==$plu){
$enter_item_2=$enter_item_2['col'];
//$barcode=$enter_item_2[1];
$stock=$enter_item_2[14]
$data = json_decode(json_encode(simplexml_load_string(preg_replace('/[\x00-\x1F\x7F-\xFF]/', '', Storage::get('xmls/imports/' . $filename)))), TRUE);

if (isset($data['transaction']['table'])) {
    $tables = $data['transaction']['table'];

    foreach($tables as $check_table){
        if(isset($check_table['@attributes']['name']) && $check_table['@attributes']['name'] == 'STOCKPRO:PLUs')
        $stock_plus_table = $check_table;
        if(isset($check_table['@attributes']['name']) && $check_table['@attributes']['name'] == 'POS:Plus')
        $pos_plus_table = $check_table;
        if(isset($check_table['@attributes']['name']) && $check_table['@attributes']['name'] == 'POS:PLUPrices')
        $pos_plu_prices_table = $check_table;
    }


    if (isset($pos_plu_prices_table) && isset($pos_plu_prices_table['row'])) {
        $plu_prods = $pos_plu_prices_table['row'];

        //working on POS:PLUPrices table for price updation
        foreach ($plu_prods as $plu_prod) {
            if(isset($plu_prod['col']))
                $plu_prod = $plu_prod['col'];
            else
                $plu_prod = $plu_prods['col'];
                $plu = $plu_prod[0];
                $price_level = $plu_prod[3];
                $price = (float) preg_replace('/[^0-9\.]/ui', '', $plu_prod[4]);
               

            //checking if plu exists in our database
            if (Products::where('plu', $plu)->first() && ($price_level == 0 || $price_level == 1)) {

                $product = Products::where('plu', $plu)->first();

                if (isset($stock_plus_table)) {
                  $plu_child_prods = $stock_plus_table['row'];

                    //working on STOCKPRO:PLUs table for child products stock updation
                    foreach ($plu_child_prods as $enter_item_2) {
                        $enter_item_2_store = $enter_item_2;
                        if(isset($enter_item_2['col']))
                        $enter_item_2 = $enter_item_2['col'];
                        else
                        $enter_item_2 = $plu_child_prods['col'];

                        //checking if this item is child plu of the current plu
                        if ($enter_item_2[27] == $plu) {
                            $product_st = Products::where('plu', $enter_item_2_store['@attributes']['id'])->first();
                            if ($product_st) {
                                $product_st->parent = $product->id;
                                $product_st->pack = (int) $enter_item_2[28];
                                $product_st->save();
                            }

                            if ($product_st && (int) $enter_item_2[28] != 0) {
                                $product_st->stock = floor($product->stock / (int) $enter_item_2[28]);
                                $product_st->save();
                            }
                        }
                    }
                }

                //updating prices of plu
                $pro_price = ProductPrices::where('product_id', $product->id)->orderBy('id', 'Desc')->first();

                if ($pro_price) {
                    if ($price_level == 0) {
                        $pro_price->luc = $price;
                    } else if ($price_level == 1) {
                        $pro_price->price = $price;
                    }
                    $pro_price->save();
                }
                $trini_update_products .= $product->id . ',';
                $trini_update_count++;

            } else {

                //checking if plu is not availbale in our database
                if (!Products::where('plu', $plu)->first()) {

                    $deleted = "True";
                    $group = '';
                    $subcategory = '';

                    //creating new plu in database
                    $product = new Products();
                    
                    if (isset($pos_plus_table)) {
                        $plu_match_prods = $pos_plus_table['row'];

                        //working on POS:Plus table for product name information
                        foreach ($plu_match_prods as $enter_item_1) {
                        
                            if (isset($enter_item_1['@attributes']['id']) && $enter_item_1['@attributes']['id'] == $plu) {  
                
                                $touch_screen_description = $enter_item_1[2];
                                $group = $enter_item_1[5];
                                $deleted = $enter_item_1[7];
                                $product->plu = $plu;
                                $product->name = $touch_screen_description;
                                $subcategory = SubCategories::where('product_group', $group)->orderBy('id', 'Desc')->first();
                                $product->category_id = $subcategory['category_id'];
                                $product->subcategory_id = $subcategory['id'];                                                   
                                
                            }                                           
                        }   
                    }
                



                    if (isset($stock_plus_table)) {
                        $plu_match2_prods = $stock_plus_table['row'];

                        //working on STOCKPRO:PLUs table for stock information
                        foreach ($plu_match2_prods as $enter_item_2) {
                            if (isset($enter_item_2['@attributes']['id']) && $enter_item_2['@attributes']['id'] == $plu) {
                                $enter_item_2 = $enter_item_2['col'];
                                //$barcode = $enter_item_2[1];
                                $stock = $enter_item_2[14];
                                $product->stock = $stock < 0 ? 0 : $stock;
                            }
                        }
                    }

                    //checking if triniteq have deleted this plu from their side
                    if ($deleted != "True") {
                        $product->status = '0';
                        $product->xml = 1;
                        $product->plu = $plu;
                        $product->file = $filename;
                        $product->category_id = $subcategory['category_id'];
                        $product->subcategory_id = $subcategory['id'];
                        $product->save();
                        $pro_price = new ProductPrices();
                        $pro_price->product_id = $product->id;
                        if ($price_level == 0) {
                            $pro_price->luc = $price;
                        } else if ($price_level == 1) {
                            $pro_price->price = $price;
                        }

                        $pro_price->save();

                        //taking record of all plu affected by this cron folder updation
                        $trini_update_products .= $product->id . ',';
                        $trini_update_count++;
                    }
                }
            }

        }

        //taking record of all files processed in this cron
        $trini_update_files .= $filename . ',';
    }

    return redirect()->back()->with('msg', "$trini_update_count inventories Updated Successfully");
} else {
    return redirect()->back()->withErrors(['Invalid XML format']);
}

}
$document = new DOMDocument();
$document->loadXML($xml);
$xpath = new DOMXpath($document);

// iterate the 'POS:Plus' table element(s)
foreach ($xpath->evaluate('/atp/transaction/table[@name="POS:Plus"]') as $table) {
    $records = [];
    $tableName = $table->getAttribute('name');
    // for each row child element
    foreach ($xpath->evaluate('row', $table) as $row) {
        // fetch row values
        $records[] = [
          'Id' => $row->getAttribute('id'),
          'LongDescription' => $xpath->evaluate('string(col[@name="LongDescription"])', $row),
          'TillDescription' => $xpath->evaluate('string(col[@name="TillDescription"])', $row),
          'TouchscreenDescription' => $xpath->evaluate('string(col[@name="TouchscreenDescription"])', $row),
        ];
    }
    var_dump($tableName, $records);
}
// next table
foreach ($xpath->evaluate('/atp/transaction/table[@name="POS:PLUPrices"]') as $table) {
    // ...
}
// ...
string(8) "POS:Plus"
array(1) {
  [0]=>
  array(4) {
    ["Id"]=>
    string(1) "6"
    ["LongDescription"]=>
    string(13) "TeTera Pinot "
    ["TillDescription"]=>
    string(12) "TeTera Pinot"
    ["TouchscreenDescription"]=>
    string(13) "TeTera Peroni"
  }
}
$xml = <<<'XML'
<?xml version="1.0" ?>
<atp xmlns="http://xml.alamte.com/atp">
  <transaction> 
    <table name="POS:Plus">...</table>
  </transaction>
</atp>
XML;

$document = new DOMDocument();
$document->loadXML($xml);
$xpath = new DOMXpath($document);
$xpath->registerNamespace('atp', 'http://xml.alamte.com/atp');

// iterate the 'POS:Plus' table elements
foreach ($xpath->evaluate('/atp:atp/atp:transaction/atp:table[@name="POS:Plus"]') as $table) {
    $tableName = $table->getAttribute('name');
    var_dump($tableName);
}
string(8) "POS:Plus"