如何修复XML Laravel 5.7中的foreach错误

时间:2019-03-15 09:10:53

标签: xml laravel laravel-5

我想在laravel 5.7中创建xml数据,并使用邮递员对其进行了测试。对于我正在使用json的数据。

这是我在json中的数据:

[
    {"fa_type_cd":"string","name":"Sting","user_value":"Sting","sort_seq":"1","type_cd":"string"},
    {"fa_type_cd":"string","name":"String","user_value":"String","sort_seq":"1","type_cd":"String"}
]

这是我在控制器中用于创建xml的功能:

public function createXml(Request $request){
    $data = $request->input('data');
    $decode_data = json_decode($data);

    $xmlrequest = '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:c1c="http://splwg.com/C1CustomerContactMaintenance.xsd">
       <SOAP-ENV:Header xmlns:wsse="http://www.w3.org/2001/XMLSchema-instance">
        <wsse:Security>
            <wsse:UsernameToken>       
                <wsse:Username>Username</wsse:Username>
                <wsse:Password Type="PasswordText">Password</wsse:Password>        
            </wsse:UsernameToken>    
        </wsse:Security>  
       </SOAP-ENV:Header>
        <SOAP-ENV:Body>
            <C1FieldActivityMaintenance dateTimeTagFormat="CdxDateTime"  transactionType ="UPDATE" >
                <C1FieldActivityMaintenanceService>
                    <C1FieldActivityMaintenanceHeader
                        FieldActivityID="111" 

                    /><C1FieldActivityMaintenanceDetails 
                        FieldActivityID="111" 
                        ActivityType="DE"  
                        > 
                        <FASteps>
                            <FAStepsHeader
                                FieldActivityID="111" 
                                />

                        </FASteps>
                        <FACharacteristics>
                            <FACharacteristicsHeader
                                FieldActivityID="111" 
                                />
                            '
                                foreach ($decode_data as $key => $value) {
                                    '<FACharacteristicsRow
                                        rowAction="Change" 
                                        FieldActivityID="111" 
                                        CharacteristicType="'.$value->type_cd.'" 
                                        Sequence="'.$value->sort_seq.'"
                                        SearchCharacteristicValue="'.$value->user_value.'" 
                                        CharacteristicValue="'.$value->user_value.'">
                                    </FACharacteristicsRow>'
                                } 
                            '
                        </FACharacteristics>
                        <FaRem>
                            <FaRemHeader
                                FieldActivityID="111" 
                                />

                        </FaRem>
                        <FaLog>
                            <FaLogHeader
                                FieldActivityID="111" 
                                />
                            <FaLogRow
                                rowAction="Add" 
                                FieldActivityID="111" 
                                Sequence="2"
                                DateTime="2018-10-06 02:50:00" 
                                Details="" 
                                User="Users2" 
                                LogType="MSG" 
                                >
                            </FaLogRow>
                        </FaLog>
                    </C1FieldActivityMaintenanceDetails>
                </C1FieldActivityMaintenanceService>
            </C1FieldActivityMaintenance>
        </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>';

    print_r($xmlrequest);
}

我想在xml中解码后再存储json数据,但是当我在邮递员中运行它时,出现了这样的错误:

Symfony\Component\Debug\Exception\FatalThrowableError: syntax error, unexpected &#039;foreach&#039; (T_FOREACH)

如何解决此错误?

2 个答案:

答案 0 :(得分:1)

您不能只在字符串中间使用foreach()。如果您想正确执行此操作,请执行以下操作:

public function createXml(Request $request){
    $data = $request->input('data');
    $decode_data = json_decode($data);

    $xmlrequest = '
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:c1c="http://splwg.com/C1CustomerContactMaintenance.xsd">
       <SOAP-ENV:Header xmlns:wsse="http://www.w3.org/2001/XMLSchema-instance">
        <wsse:Security>
            <wsse:UsernameToken>       
                <wsse:Username>Username</wsse:Username>
                <wsse:Password Type="PasswordText">Password</wsse:Password>        
            </wsse:UsernameToken>    
        </wsse:Security>  
       </SOAP-ENV:Header>
        <SOAP-ENV:Body>
            <C1FieldActivityMaintenance dateTimeTagFormat="CdxDateTime"  transactionType ="UPDATE" >
                <C1FieldActivityMaintenanceService>
                    <C1FieldActivityMaintenanceHeader
                        FieldActivityID="111" 

                    /><C1FieldActivityMaintenanceDetails 
                        FieldActivityID="111" 
                        ActivityType="DE"  
                        > 
                        <FASteps>
                            <FAStepsHeader
                                FieldActivityID="111" 
                                />

                        </FASteps>
                        <FACharacteristics>
                            <FACharacteristicsHeader
                                FieldActivityID="111" 
                                />
    ';

    foreach ($decode_data as $key => $value) {
        $xmlrequest .='<FACharacteristicsRow
            rowAction="Change" 
            FieldActivityID="111" 
            CharacteristicType="'.$value->type_cd.'" 
            Sequence="'.$value->sort_seq.'"
            SearchCharacteristicValue="'.$value->user_value.'" 
            CharacteristicValue="'.$value->user_value.'">
        </FACharacteristicsRow>'
    } 

    $xmlrequest .= '
                        </FACharacteristics>
                        <FaRem>
                            <FaRemHeader
                                FieldActivityID="111" 
                                />

                        </FaRem>
                        <FaLog>
                            <FaLogHeader
                                FieldActivityID="111" 
                                />
                            <FaLogRow
                                rowAction="Add" 
                                FieldActivityID="111" 
                                Sequence="2"
                                DateTime="2018-10-06 02:50:00" 
                                Details="" 
                                User="Users2" 
                                LogType="MSG" 
                                >
                            </FaLogRow>
                        </FaLog>
                    </C1FieldActivityMaintenanceDetails>
                </C1FieldActivityMaintenanceService>
            </C1FieldActivityMaintenance>
        </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
    ';

    print_r($xmlrequest);
}

答案 1 :(得分:1)

简单答案

您不能在单引号之间使用foreach。您应该首先创建一个变量,然后在xml中引用它。

$foreachResult = "";

foreach ($decode_data as $key => $value) {
    $foreachResult += <--your xml code-->;
}

然后

<FACharacteristicsHeader
   FieldActivityID="111"/>' . $foreachResult . '/>

首选方式:

很难理解和理解php文件中的xml代码。您可以使用细枝在Laravel中渲染xml。

示例:Rendering xml with twig in laravel(寻找最佳答案)