Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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
如何在XML中使用PHP的大括号{}?_Php_Xml_Xml Parsing - Fatal编程技术网

如何在XML中使用PHP的大括号{}?

如何在XML中使用PHP的大括号{}?,php,xml,xml-parsing,Php,Xml,Xml Parsing,我想在PHP和XML中做类似的事情 for($counter=0;<$Num;$counter++) { $Line=$counter+1; if($counter==0) { $data = '<?xml version=\'1.0\' encoding=\'UTF-8\'?> <request> <GRP ID="1">

我想在PHP和XML中做类似的事情

for($counter=0;<$Num;$counter++)
{
    $Line=$counter+1;
    if($counter==0)
    {
        $data =  '<?xml version=\'1.0\' encoding=\'UTF-8\'?>
                <request>
                <GRP ID="1">
                <FLD NAM="TRANSACTION" TYP="Char">'.$transaction.'</FLD>
                </GRP>
    }//I want to close this bleces of PHP which is located inside of XML


    <TAB DIM="1" ID="1" SIZE="1">
    <LIN NUM=' . $Line . '>
    <FLD NAM="ITEMREFERENCE">' . $itemCode[$counter] . '</FLD>
    </LIN>
    </TAB>
    </request>
}
如何在XML内部使用大括号?

PHP和XML是不同的实体。您不能在xml中关闭大括号并期望php代码能够理解它。我想你需要这样的东西:

$data = '<?xml version=\'1.0\' encoding=\'UTF-8\'?>
    <request>
        <GRP ID="1">
            <FLD NAM="TRANSACTION" TYP="Char">'.$transaction.'</FLD>
        </GRP>';

for ($counter=0;$counter<$Num;$counter++) {
    $Line=$counter+1;
    $data .= '
    <TAB DIM="1" ID="1" SIZE="1">
        <LIN NUM=' . $Line . '>
            <FLD NAM="ITEMREFERENCE">' . $itemCode[$counter] . '</FLD>
        </LIN>
    </TAB>';
}
$data .= '</request>';

echo $data;

使用SimpleXML来实现这一点可能看起来很长,但它避免了因构建字符串而产生的格式和其他问题

$doc = new SimpleXMLElement('<?xml version=\'1.0\' encoding=\'UTF-8\'?>
                <request />');
$grp = $doc->addChild("GRP");
$grp->addAttribute("ID", "1");
$fld = $grp->addChild("FLD", $transaction);
$fld->addAttribute("NAM", "TRANSACTION");
$fld->addAttribute("TYP", "Char");
for($counter=0;$counter<$Num;$counter++)    {
    $tab = $doc->addChild("TAB");
    $tab->addAttribute("DIM", "1");
    $tab->addAttribute("ID", "1");
    $tab->addAttribute("SIZE", "1");
    $lin = $tab->addChild("LIN");
    $lin->addAttribute("NUM", (string)($counter+1));
    $fld = $tab->addChild("FLD", $itemCode[$counter]);
    $fld->addAttribute("NAM", "ITEMREFERENCE");
}

echo $doc->asXML();

做什么?什么支架?你的实际问题/错误是什么?很难发现你在为所有的打字错误做什么你能清理一下吗?或者,您使用的实际PHP代码PHP和XML是不同的实体。您不能在xml中关闭大括号并期望php代码能够理解itFIX:1::for$counter=0$我想做的是使用循环,其中第一个循环将执行所有XML标记,但第二个循环或N个循环将只执行一个xmllines标记。