Php dom object to xml

Работа с XML средствами PHP

Давайте начнем с самого простого: загрузки и выгрузки XML-документа с использованием DOM. Основной класс, который мы будем использовать, это DOMDocument. Через него можно создавать новые документы, сохранять их, а также загружать существующие.

 // Создаём XML-документ версии 1.0 с кодировкой utf-8 $dom = new DOMDocument('1.0', 'utf-8'); // Текст XML-документа $strXML = 'ПервыйВторой'; // Загружаем XML-документ из строки $dom->loadXML($strXML); /* // Загружаем XML-документ из файла $dom->load('doc.xml'); // Убираем лишние пробелы и отступы $dom->preserveWhiteSpace = false; */ // Сохраняем XML-документ как строку и выводим в браузер $xml = $dom->saveXML(); echo htmlspecialchars($xml); // Сохраняем XML-документ в файл $dom->save('doc.xml'); ?>

Как получить список дочерних узлов

 // Создаём XML-документ $dom = new DOMDocument('1.0', 'utf-8'); // Текст XML-документа $strXML = 'ПервыйВторой'; // Загружаем XML-документ из строки $dom->loadXML($strXML); // Получаем корневой элемент $root = $dom->documentElement; // Получаем список дочерних узлов $childs = $root->childNodes; for ($i = 0; $i  $childs->length; $i++)  $child = $childs->item($i); // Выводим информацию об узле echo $child->nodeName . ' : ' . $child->nodeValue . '
'
; > ?>

Свойство nodeName содержит название элемента (в данном случае, item ), а nodeValue содержимое элемента (в данном случае, « Первый » и « Второй »).

Работа с атрибутами узлов

Давайте посмотрим, как получить значение атрибута узла, устанавить новое значение, создать и удалить атрибут:

 // Создаём XML-документ $dom = new DOMDocument('1.0', 'utf-8'); // Текст XML-документа $strXML = 'ПервыйВторойТретий'; // Загружаем XML-документ из строки $dom->loadXML($strXML); // Получаем корневой элемент $root = $dom->documentElement; // Первый дочерний элемент $first = $root->firstChild; // Следующий сестринский элемент $second = $first->nextSibling; // Последний дочерний элемент $third = $root->lastChild; // Выводим информацию о первом дочернем элементе echo 'Элемент ' . $first->nodeName . ', значение «' . $first->nodeValue . '», атрибут id'); // Устанавливаем атрибут id для второго дочернего элемента $second->setAttribute('id', 2); // Устанавливаем атрибут id для третьего дочернего элемента $attr = $dom->createAttribute('id'); $attr->value = 3; $third->appendChild($attr); // Удаляем атрибут id для третего дочернего элемента if ( $third->hasAttribute('id') ) $third->removeAttribute('id'); ?>

Создание XML-документа

 // Создаём XML-документ $dom = new DOMDocument('1.0', 'utf-8'); // Создаём корневой элемент $root = $dom->createElement('users'); $dom->appendChild($root); $users = array( array('id' => 1, 'name' => 'Николай Петров', 'email' => 'petrov.n@mail.ru'), array('id' => 2, 'name' => 'Петр Иванов', 'email' => 'ivanov.p@mail.ru'), array('id' => 3, 'name' => 'Сергей Смирнов', 'email' => 'smirnov.s@mail.ru') ); foreach ( $users as $value )  // Создаём узел $user = $dom->createElement('user'); // Добавляем дочерний элемент для $root->appendChild($user); // Устанавливаем атрибут id для узла $user->setAttribute('id', $value['id']); // Создаём узел с текстом внутри $name = $dom->createElement('name', $value['name']); // Добавляем дочерний элемент для $user->appendChild($name); // Создаём узел $email = $dom->createElement('email'); // Создаем текстовой узел $text = $dom->createTextNode($value['email']); // Создаем текстовой узел внутри конструкции // $text = $dom->createCDATASection($value['email']); // Добавляем текстовой узел для $email->appendChild($text); // Добавляем дочерний элемент для $user->appendChild($email); > // Сохраняем полученный XML-документ в файл $dom->save('users.xml'); ?>

Содержимое файла users.xml:

  Николай Петров petrov.n@mail.ru  Петр Иванов ivanov.p@mail.ru  Сергей Смирнов smirnov.s@mail.ru  

Извлечение данных из xml-документа

Давайте теперь извлечем данные из документа users.xml обратно в массив:

 // Создаём XML-документ $dom = new DOMDocument('1.0', 'utf-8'); // Загружаем XML-документ из файла $dom->load('users.xml'); // Получаем корневой элемент $root = $dom->documentElement; // Получаем дочерние элементы корневого элемента $users = $root->childNodes; // Перебираем полученные элементы foreach ($users as $user)  // Получаем значение атрибута id узла $id = $user->getAttribute('id'); // Получаем дочерние элементы узла $info = $user->childNodes; // Получаем значение узла $name = $info->item(0)->nodeValue; // Получаем значение узла $email = $info->item(1)->nodeValue; // Записываем данные в массив $array[] = array('id' => $id, 'name' => $name, 'email' => $email); > print_r($array); ?>
Array ( [0] => Array ( [id] => 1 [name] => Николай Петров [email] => petrov.n@mail.ru ) [1] => Array ( [id] => 2 [name] => Петр Иванов [email] => ivanov.p@mail.ru ) [2] => Array ( [id] => 3 [name] => Сергей Смирнов [email] => smirnov.s@mail.ru ) )

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

  • 1С:Предприятие (31)
  • API (29)
  • Bash (43)
  • CLI (99)
  • CMS (139)
  • CSS (50)
  • Frontend (75)
  • HTML (66)
  • JavaScript (150)
  • Laravel (72)
  • Linux (146)
  • MySQL (76)
  • PHP (125)
  • React.js (66)
  • SSH (27)
  • Ubuntu (68)
  • Web-разработка (509)
  • WordPress (73)
  • Yii2 (69)
  • БазаДанных (95)
  • Битрикс (66)
  • Блог (29)
  • Верстка (43)
  • ИнтернетМагаз… (84)
  • КаталогТоваров (87)
  • Класс (30)
  • Клиент (27)
  • Ключ (28)
  • Команда (68)
  • Компонент (60)
  • Конфигурация (62)
  • Корзина (32)
  • ЛокальнаяСеть (28)
  • Модуль (34)
  • Навигация (31)
  • Настройка (140)
  • ПанельУправле… (29)
  • Плагин (33)
  • Пользователь (26)
  • Практика (99)
  • Сервер (74)
  • Событие (27)
  • Теория (105)
  • Установка (66)
  • Файл (47)
  • Форма (58)
  • Фреймворк (192)
  • Функция (36)
  • ШаблонСайта (68)

Источник

DOMDocument::loadXML

Returns true on success or false on failure. If called statically, returns a DOMDocument or false on failure.

Errors/Exceptions

If an empty string is passed as the source , a warning will be generated. This warning is not generated by libxml and cannot be handled using libxml’s error handling functions.

Prior to PHP 8.0.0 this method could be called statically, but would issue an E_DEPRECATED error. As of PHP 8.0.0 calling this method statically throws an Error exception

Examples

Example #1 Creating a Document

See Also

  • DOMDocument::load() — Load XML from a file
  • DOMDocument::save() — Dumps the internal XML tree back into a file
  • DOMDocument::saveXML() — Dumps the internal XML tree back into a string

User Contributed Notes 12 notes

Always remember that with the default parameters this function doesn’t handle well large files, i.e. if a text node is longer than 10Mb it can raise an exception stating:

DOMDocument::loadXML(): internal error Extra content at the end of the document in Entity

even though the XML is fine.

The cause is a definition in parserInternals.h of lixml:
#define XML_MAX_TEXT_LENGTH 10000000

To allow the function to process larger files, pass the LIBXML_PARSEHUGE as an option and it will work just fine:

A call to loadXML() will overwrite the XML declaration previously created in the constructor of DOMDocument. This can cause encoding problems if there is no XML declaration in the loaded XML and you don’t have control over the source (e.g. if the XML is coming from a webservice). To fix this, set encoding AFTER loading the XML using the ‘encoding’ class property of DOMDocument. Example:

test.php:
$xmlDoc = new DOMDocument(«1.0», «utf-8»); // Parameters here are overwritten anyway when using loadXML(), and are not really relevant
$testXML = file_get_contents(«test.xml»);
$xmlDoc->loadXML($testXML);
// Print the contents to a file or in a log function to get the output, using $xmlDoc->saveXML()

test.php:
$xmlDoc = new DOMDocument(«1.0», «utf-8»); // Parameters here are overwritten anyway when using loadXML(), and are not really relevant
$testXML = file_get_contents(«test.xml»);
$xmlDoc->loadXML($testXML);
$xmlDoc->encoding = «utf-8»;
// Print the contents to a file or in a log function to get the output, using $xmlDoc->saveXML()

loadXml reports an error instead of throwing an exception when the xml is not well formed. This is annoying if you are trying to to loadXml() in a try. catch statement. Apparently its a feature, not a bug, because this conforms to a spefication.

If you want to catch an exception instead of generating a report, you could do something like

function XmlLoader ( $strXml )
set_error_handler ( ‘HandleXmlError’ );
$dom = new DOMDocument ();
$dom -> loadXml ( $strXml );
restore_error_handler ();
return $dom ;
>

?>

Returning false in function HandleXmlError() causes a fallback to the default error handler.

earth at anonymous dot com,

preserveWhiteSpace property needs to be set to false for formatOutput to work properly, for some reason.

$dom = new DOMDocument;
$dom->preserveWhiteSpace = false;
$dom->loadXML($xmlStr);
.
$element->appendChild(. );
.
$dom->formatOutput = true;
$xmlStr = $dom->saveXML();
echo $xmlStr;

This would format the output nicely.

For some reason, when you set DOMDocument’s property ‘recover’ to true, using ‘@’ to mask errors thrown by loadXml() won’t work.

function maskErrors() <>
set_error_handler(‘maskErrors’);
$dom->loadXml($xml);
restore_error_handler();

You could also simply do this: error_reporting(0); and then set back error_reporting to its original state.

The documentation states that loadXML can be called statically, but this is misleading. This feature seems to be a special case hack and its use seems to be discouraged according to http://bugs.php.net/bug.php?id=41398.

Calling the method statically will fail with an error if the code runs with E_STRICT error reporting enabled.

The documentation should be changed to make it clear that a static call is against recommended practice and won’t work with E_STRICT.

Note that loadXML crops off beginning and trailing whitespace and linebreaks.

When using loadXML and appendChild to add a chunk of XML to an existing document, you may want to force a linebreak between the end of the XML chunk and the next line (usually a close tag) in the output file:

$childDocument = new DOMDocument;
$childDocument>preserveWhiteSpace = true;
$childDocument->loadXML(..XML-Chunk..);
$mNewNode = $mainDOcument->importNode($childDocument->documentElement, true);
$ParentNode->appendChild($mNewNode);
$ParentNode->appendChild($mainDocument->createTextNode(«\\n «);

Although it is said that DOM should not be used to make ‘pretty’ XML output, it is something I struggled with to get something that was readable for testing. Another solution is to use the createDocumentFragment()->appendXML(..XML-Chunk..) instead, which seems not to trim off linebreaks like DOMDocument->loadXML() does.

When using loadXML() to parse a string that contains entity references (e.g.,  ), be sure that those entity references are properly declared through the use of a DOCTYPE declaration; otherwise, loadXML() will not be able to interpret the string.

$dd1 = new DOMDocument ();
$dd1 -> loadXML ( $str );

echo $dd1 -> saveXML ();
?>

Given the above code, PHP will issue a Warning about the entity ‘nbsp’ not being properly declared. Also, the call to saveXML() will return nothing but a trimmed-down version of the original processing instruction. everything else is gone, and all because of the undeclared entity.

$dd2 = new DOMDocument ();
$dd2 -> loadXML ( $str );

echo $dd2 -> saveXML ();
?>

Since the ‘nbsp’ entity is defined in the DOCTYPE, PHP no longer issues that Warning; the string is now well-formed, and loadXML() understands it perfectly.

You can also use references to external DTDs in the same way (e.g., «http://www.w3.org/TR/html4/strict.dtd»>), which is particularly important if you need to do this for many different documents with many different possible entities.

Also, as a sidenote. entity references created by createEntityReference() do not need this kind of explicit declaration.

Источник

Читайте также:  Utf8 to ascii php
Оцените статью