这是我的xml
<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<elenco_clienti>
<cliente>
<cod>00001</cod>
<name>xxxxxxxxxxxxxx</name>
<address>yyyyyyyyyyyyyyy</address>
</cliente>
<cliente>
<cod>00003</cod>
<name>mmmmmmmmmmmmmmm</name>
<address>iiiiiiiiiiii</address>
</cliente>
</elenco_clienti>
这是我的控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Orchestra\Parser\Xml\Facade as XmlParser;
use App\Clienti;
use Illuminate\Container\Container;
use Orchestra\Parser\Xml\Document as OrchestraDocument;
use Orchestra\Parser\Xml\Reader as OrchestraReader;
class XmlController extends Controller
{
public function importFilexml(Request $request){
if($request->hasFile('sample_file')){
$path = $request->file('sample_file')->getRealPath();
//$xml = XmlParser::extract($path);
//print_r($xml);
$app = new Container;
$document = new OrchestraDocument($app);
$reader = new OrchestraReader($document);
$xml = $reader->load($path);
$conto = count($xml);
$user = $xml->parse([
'cod' => ['uses' => 'cliente.cod'],
'name' => ['uses' => 'cliente.name'],
'address' => ['uses' => 'cliente.address'],
]);
print_r($user);
if(!empty($user)){
DB::table('clientis')->insert($user);
dd('Insert Recorded successfully.');
}
}
dd('Request data does not have any files to import.');
}
}
问题在于:只有第一个客户的价值存储在变量中。我无法访问所有xml文件。 为什么? xml对象不包含所有xml文件,我不查看项属性
这是我的对象
Orchestra\Parser\Xml\Document Object
(
[app:protected] => Illuminate\Container\Container Object
(
[resolved:protected] => Array ( )
[bindings:protected] => Array ( )
[methodBindings:protected] => Array ( )
[instances:protected] => Array ( )
[aliases:protected] => Array ( )
[abstractAliases:protected] => Array ( )
[extenders:protected] => Array ( )
[tags:protected] => Array ( )
[buildStack:protected] => Array ( )
[with:protected] => Array ( )
[contextual] => Array ( )
[reboundCallbacks:protected] => Array ( )
[globalResolvingCallbacks:protected] => Array ( )
[globalAfterResolvingCallbacks:protected] => Array ( )
[resolvingCallbacks:protected] => Array ( )
[afterResolvingCallbacks:protected] => Array ( ) )
[namespaces:protected] =>
[content:protected] => SimpleXMLElement Object
(
[cliente] => Array (
[0] => SimpleXMLElement Object (
[cod] => 00001
[name] => My name
[address] => My address
[1] => SimpleXMLElement Object ( ....
为什么受到保护? 我该如何访问这个对象? 我试着用foreach($ xml-&gt; content-&gt; cliente as $ item =&gt; $ value){ 但是不行 任何建议请
答案 0 :(得分:0)
正如@Amarnasan所说,您的代码不包含任何循环来遍历所有 cliente 。要正确解析该XML,一个示例控制器将是(使用最后版本的Xmlparser library,截至2018年2月):
use Orchestra\Parser\Xml\Facade as XmlParser;
class XmlController extends Controller
{
public function importFilexml(Request $request) {
if($request->hasFile('sample_file')) {
$xml = XmlParser::load($request->file('sample_file')->getRealPath());
$elenco_clienti = $xml -> parse([
'cod' => ['uses' => 'elenco_clienti.cliente[cod]'],
'name' => ['uses' => 'elenco_clienti.cliente[name]'],
'address' => ['uses' => 'elenco_clienti.cliente[address]']
]);
...
// DB inserts and other stuff
}
}
}
因此,您可以正确解析所有 cliente 的信息:)