O
O
OlegBas19962018-09-29 16:17:49
PHP
OlegBas1996, 2018-09-29 16:17:49

XML file corruption when adding a new element?

Hello! When adding a new element to an existing xml file, the first two characters of the beginning of the xml file disappear, as a result, the xml file header looks like
ml version="1.0" encoding="UTF-8"?>
a should be
<?xml version="1.0" encoding="UTF-8"?>
The problem is observed after executing the following code

$baseLink = "https://pres.ru.com/article/short/";
                    //Получаем ID добавленного объекта
                   // $id =  $model->id;
                    $id = 23;
                    //Склеиваем базовую часть ссылки с ID добавленного объекта
                    $linkNews = $baseLink.$id;
                    //Путь к  карте сайта
                    $pathSiteMap = Yii::getAlias("@app")."\sitemap.xml";
                    //Открываем файл карты  в режиме редактирования
      	$dom = new \DOMDocument();
      	$dom->load($pathSiteMap);
      	$urlset = $dom->getElementsByTagName("urlset");
      	$urlset = $urlset->item(0);
      	$url = $urlset->appendChild($dom->createElement("url"));

       
      	$loc = $url->appendChild($dom->createElement("loc"));
      	$lastmod = $url->appendChild($dom->createElement("lastmod"));
      	$priority = $url->appendChild($dom->createElement("priority"));
      	$loc->appendChild($dom->createTextNode($linkNews));
      	$lastmod->appendChild($dom->createTextNode(date("Y-m-d H:i:s")));
      	$priority->appendChild($dom->createTextNode("0.6"));
      	$dom->save($pathSiteMap);

the same with SimpleXML

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question