Displaying text extracted from XML using PHP simplexml I have this xml file and i am parsing it with php simplexml.

My XML file looks like this:

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;noticias&gt;
    &lt;noticia url="noticia-1"&gt;
        &lt;titulo&gt;título da notícia 1&lt;/titulo&gt;
        &lt;desc&gt;plain text.&lt;/desc&gt;
        &lt;texto&gt;plain text.&lt;/texto&gt;
        &lt;img&gt;&lt;/img&gt;
        &lt;in&gt;Publico&lt;/in&gt;
    &lt;/noticia&gt;
    ...
&lt;/noticias&gt;


In my php page i parse it like this:

$file = 'xml/noticias.xml';
if(file_exists($file)) {
    $xml = simplexml_load_file($file);
    foreach($items as $item) {
        $titulo = htmlentities($item-&gt;titulo, ENT_QUOTES, 'utf-8');
        $desc = htmlentities($item-&gt;desc, ENT_QUOTES, 'utf-8');
        $url = htmlentities($item['url'], ENT_QUOTES, 'utf-8');

        if(strlen($titulo) &lt; 3) {$titulo = 'T&amp;iacute;tulo em falta';} else {$titulo = $titulo;}
        if(strlen($desc) &lt; 3) {$desc = 'Descri&amp;ccedil;&amp;atilde;o em falta';} else {$desc = $desc;}
        if(strlen($url) &lt; 3) {$h3 = '&lt;h3&gt;'.$titulo.'&lt;/h3&gt;';} else {$h3 = '&lt;a href="noticia/'.$url.'"&gt;&lt;h3&gt;'.$titulo.'&lt;/h3&gt;&lt;/a&gt;';}

        ?&gt;
            &lt;div class="col-lg-3 col-md-6"&gt;
                &lt;div class="item"&gt;
                    &lt;div class="content"&gt;
                        &lt;?php echo $h3; ?&gt;
                        &lt;p&gt;&lt;?php echo $desc; ?&gt;&lt;/p&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;?php
    }
}
else {
    // do something... throw error message
}


Is this ok? i mean, i escape values when i get from xml. Is it ok to do it like this or should i escape values on echo? is there any danger to leave as i have?

other thing... i have the xml files protected with htaccess. right now, they can only be edited directly. no scripts to edit them.
My preference is to leave the html escaping until the last minute.

Here are some changes you could make to simplify your code.

Code should work but there is no link between $xml and $items, so unable to test.

&lt;?php

$file = 'xml/noticias.xml';

// guard clause exit early, instead of last
if (!file_exists($file)) {
    // do something... throw error message
    throw new \Exception('File does not exist');
}

$xml = simplexml_load_file($file);

foreach ($items as $item) {
    $titulo = $item-&gt;titulo;
    $desc = $item-&gt;desc;
    $url = $item['url'];

    // use mb_ function as you are dealing with unicode text, strlen will report incorrect length
    if (mb_strlen($titulo) &lt; 3) {
        $titulo = 'Título em falta';
    }
    // no need to reassign value to itself
    //else {
    //    $titulo = $titulo;
    //}

    if (mb_strlen($desc) &lt; 3) {
        $desc = 'Descrição em falta';
    }
//    else {
//        $desc = $desc;
//    }

    // not expecting url to be unicode
    if (strlen($url) &gt;= 3) {
        $url = "noticia/{$url}";
    } else {
        $url = null;
    }

    $h3 = $titulo;

//        $h3 = '&lt;h3&gt;' . $titulo . '&lt;/h3&gt;';
//    } else {
//        $h3 = '&lt;a href="noticia/' . $url . '"&gt;&lt;h3&gt;' . $titulo . '&lt;/h3&gt;&lt;/a&gt;';
//    }
    ?&gt;

    &lt;div class="col-lg-3 col-md-6"&gt;
        &lt;div class="item"&gt;
            &lt;div class="content"&gt;
                &lt;?php if ($url): ?&gt;
                    &lt;a href="&lt;?php echo $url; ?&gt;"&gt;
                &lt;?php endif; ?&gt;

                &lt;h3&gt;&lt;?php echo htmlentities($h3); ?&gt;&lt;/h3&gt;

                &lt;?php if ($url): ?&gt;
                    &lt;/a&gt;
                &lt;?php endif; ?&gt;

                &lt;p&gt;&lt;?php echo htmlentities($desc); ?&gt;&lt;/p&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;?php
}