PHP和XmlWriter更改缩进字符串

问题描述 投票:0回答:1

我正在使用laravel 5,我尝试使用XMLWRITER输出xml文件。是否可以将缩进字符串从“一个空格”更改为“制表”?

我的实际代码:

    $xml = new \XMLWriter();
    $xml->openMemory();
    $xml->setIndent(4);
    $xml->startDocument('1.0','UTF-8','yes');
    $xml->startElement('immeuble');
    $xml->writeAttribute('nature-immeuble', 'test');
    $xml->startElement('adresse');
    $xml->writeAttribute('localite', 'Copropriete');
    $xml->writeAttribute('libelle-pays', 'France');
    $xml->startElement('cadastre');
        $xml->writeAttribute('section', 'AA');
        $xml->writeAttribute('numero', '0000');
        $xml->endElement();
        $xml->endElement();
    foreach($lots as $lot) {
        $xml->startElement('lot');
        $xml->writeAttribute('numero', $lot->id);
        $xml->writeAttribute('type', 'appartement');
        $designation = getDesignation2($lot->id);
        $xml->writeAttribute('designation', $designation);
        $xml->writeAttribute('batiment', substr($lot->etageLots->first()->etage->batiment->nom, 0, 5));
        $xml->writeAttribute('etage', $lot->etageLots->first()->etage->coeff_id);
        if(is_null($lot->tantieme_force)){
            $tantieme = round($lot->tantieme_calcul,0);
        }else{
            $tantieme = $lot->tantieme_force;
        }
        $xml->writeAttribute('milliemes-generaux', $tantieme.$base);
        $xml->endElement();
    }
    $xml->endElement();
    $xml->endDocument();

    $content = $xml->outputMemory();
    $xml = null;

    return response($content)->header('Content-Type', 'text/xml');

它给了我正确的xml但是每行都缩进了一个空格,实际上我想要一个制表。

是否可以更改此设置?

我看到设置缩进字符串,但我没有找到一种方法来使用它与制表。

谢谢您帮忙。

php xml laravel xmlwriter
1个回答
1
投票

如果你想缩进制表,那就像这样使用它:

$xml->setIndent(true);
$xml->setIndentString("\t");
© www.soinside.com 2019 - 2024. All rights reserved.