Xml version php file
To create a XML using DOMDocument,basically, we need to create all the tags and attributes using the createElement() and createAttribute() methods and them create the XML structure with the appendChild() .
The example below includes tags, attributes, a CDATA section and a different namespace for the second tag:
$dom = new DOMDocument('1.0', 'utf-8'); $dom->preserveWhiteSpace = false; $dom->formatOutput = true; //create the main tags, without values $books = $dom->createElement('books'); $book_1 = $dom->createElement('book'); // create some tags with values $name_1 = $dom->createElement('name', 'PHP - An Introduction'); $price_1 = $dom->createElement('price', '$5.95'); $id_1 = $dom->createElement('id', '1'); //create and append an attribute $attr_1 = $dom->createAttribute('version'); $attr_1->value = '1.0'; //append the attribute $id_1->appendChild($attr_1); //create the second tag book with different namespace $namespace = 'www.example.com/libraryns/1.0'; //include the namespace prefix in the books tag $books->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:ns', $namespace); $book_2 = $dom->createElementNS($namespace,'ns:book'); $name_2 = $dom->createElementNS($namespace, 'ns:name'); //create a CDATA section (that is another DOMNode instance) and put it inside the name tag $name_cdata = $dom->createCDATASection('PHP - Advanced'); $name_2->appendChild($name_cdata); $price_2 = $dom->createElementNS($namespace, 'ns:price', '$25.00'); $id_2 = $dom->createElementNS($namespace, 'ns:id', '2'); //create the XML structure $books->appendChild($book_1); $book_1->appendChild($name_1); $book_1->appendChild($price_1); $book_1->appendChild($id_1); $books->appendChild($book_2); $book_2->appendChild($name_2); $book_2->appendChild($price_2); $book_2->appendChild($id_2); $dom->appendChild($books); //saveXML() method returns the XML in a String print_r ($dom->saveXML());
This will output the following XML:
xml version="1.0" encoding="utf-8"?> books xmlns:ns="www.example.com/libraryns/1.0"> book> name>PHP - An Introductionname> price>$5.95price> id version="1.0">1id> book> ns:book> ns:name> ns:name> ns:price>$25.00ns:price> ns:id>2ns:id> ns:book> books>
# Read a XML document with DOMDocument
Similarly to the SimpleXML, you can use DOMDocument to parse XML from a string or from a XML file
1. From a string
$doc = new DOMDocument(); $doc->loadXML($string);
2. From a file
$doc = new DOMDocument(); $doc->load('books.xml');// use the actual file path. Absolute or relative
Example of parsing
Considering the following XML:
xml version="1.0" encoding="UTF-8"?> books> book> name>PHP - An Introductionname> price>$5.95price> id>1id> book> book> name>PHP - Advancedname> price>$25.00price> id>2id> book> books>
This is a example code to parse it
$books = $doc->getElementsByTagName('book'); foreach ($books as $book) $title = $book->getElementsByTagName('name')->item(0)->nodeValue; $price = $book->getElementsByTagName('price')->item(0)->nodeValue; $id = $book->getElementsByTagName('id')->item(0)->nodeValue; print_r ("The title of the book $id is $title and it costs $price." . "\n"); >
The title of the book 1 is PHP — An Introduction and it costs $5.95. The title of the book 2 is PHP — Advanced and it costs $25.00.
# Create an XML file using XMLWriter
Instantiate a XMLWriter object:
Next open the file to which you want to write. For example, to write to /var/www/example.com/xml/output.xml , use:
$xml->openUri('file:///var/www/example.com/xml/output.xml');
To start the document (create the XML open tag):
xml version="1.0" encoding="UTF-8"?>
Now you can start writing elements:
This will generate the XML:
If you need something a little more complex than simply nodes with plain values, you can also «start» an element and add attributes to it before closing it:
$xml->startElement('foo'); $xml->writeAttribute('bar', 'baz'); $xml->writeCdata('Lorem ipsum'); $xml->endElement();
foo bar="baz">![CDATA[Lorem ipsum]]>/foo>
# Read a XML document with SimpleXML
You can parse XML from a string or from a XML file
1. From a string
$xml_obj = simplexml_load_string($string);
2. From a file
$xml_obj = simplexml_load_file('books.xml');
Example of parsing
Considering the following XML:
xml version="1.0" encoding="UTF-8"?> books> book> name>PHP - An Introductionname> price>$5.95price> id>1id> book> book> name>PHP - Advancedname> price>$25.00price> id>2id> book> books>
This is a example code to parse it
$xml = simplexml_load_string($xml_string); $books = $xml->book; foreach ($books as $book) $id = $book->id; $title = $book->name; $price = $book->price; print_r ("The title of the book $id is $title and it costs $price." . "\n"); >
The title of the book 1 is PHP — An Introduction and it costs $5.95.
The title of the book 2 is PHP — Advanced and it costs $25.00.
# Leveraging XML with PHP’s SimpleXML Library
SimpleXML is a powerful library which converts XML strings to an easy to use PHP object.
The following assumes an XML structure as below.
xml version="1.0" encoding="UTF-8"?> document> book> bookName>StackOverflow SimpleXML ExamplebookName> bookAuthor>PHP ProgrammerbookAuthor> book> book> bookName>Another SimpleXML ExamplebookName> bookAuthor>Stack Overflow CommunitybookAuthor> bookAuthor>PHP ProgrammerbookAuthor> bookAuthor>FooBarbookAuthor> book> document>
Read our data in to SimpleXML
To get started, we need to read our data into SimpleXML. We can do this in 3 different ways. Firstly, we can load our data from a DOM node.
$xmlElement = simplexml_import_dom($domNode);
$xmlElement = simplexml_load_file($filename);
$xmlString = 'xml version="1.0" encoding="UTF-8"?> document> book> bookName>StackOverflow SimpleXML ExamplebookName> bookAuthor>PHP ProgrammerbookAuthor> book> book> bookName>Another SimpleXML ExamplebookName> bookAuthor>Stack Overflow CommunitybookAuthor> bookAuthor>PHP ProgrammerbookAuthor> bookAuthor>FooBarbookAuthor> book> document>'; $xmlElement = simplexml_load_string($xmlString);
Whether you’ve picked to load from a DOM Element
(opens new window) , you are now left with a SimpleXMLElement variable called $xmlElement . Now, we can start to make use of our XML in PHP.
Accessing our SimpleXML Data
The simplest way to access data in our SimpleXMLElement object is to call the properties directly
(opens new window) . If we want to access our first bookName, StackOverflow SimpleXML Example , then we can access it as per below.
echo $xmlElement->book->bookName;
At this point, SimpleXML will assume that because we have not told it explicitly which book we want, that we want the first one. However, if we decide that we do not want the first one, rather that we want Another SimpleXML Example , then we can access it as per below.
echo $xmlElement->book[1]->bookName;
It is worth noting that using [0] works the same as not using it, so
Looping through our XML
There are many reasons you may wish to loop through XML
(opens new window) , such as that you have a number of items, books in our case, that we would like to display on a webpage. For this, we can use a foreach loop
foreach ( $xmlElement->book as $thisBook ) echo $thisBook->bookName >
$count = $xmlElement->count(); for ( $i=0; $i$count; $i++ ) echo $xmlElement->book[$i]->bookName; >
Handling Errors
Now we have come so far, it is important to realise that we are only humans, and will likely encounter an error eventually — especially if we are playing with different XML files all the time. And so, we will want to handle those errors.
Consider we created an XML file. You will notice that while this XML is much alike what we had earlier, the problem with this XML file is that the final closing tag is /doc instead of /document.
xml version="1.0" encoding="UTF-8"?> document> book> bookName>StackOverflow SimpleXML ExamplebookName> bookAuthor>PHP ProgrammerbookAuthor> book> book> bookName>Another SimpleXML ExamplebookName> bookAuthor>Stack Overflow CommunitybookAuthor> bookAuthor>PHP ProgrammerbookAuthor> bookAuthor>FooBarbookAuthor> book> doc>
Now, say, we load this into our PHP as $file.
libxml_use_internal_errors(true); $xmlElement = simplexml_load_file($file); if ( $xmlElement === false ) $errors = libxml_get_errors(); foreach ( $errors as $thisError ) switch ( $thisError->level ) case LIBXML_ERR_FATAL: echo "FATAL ERROR: "; break; case LIBXML_ERR_ERROR: echo "Non Fatal Error: "; break; case LIBXML_ERR_WARNING: echo "Warning: "; break; > echo $thisError->code . PHP_EOL . 'Message: ' . $thisError->message . PHP_EOL . 'Line: ' . $thisError->line . PHP_EOL . 'Column: ' . $thisError->column . PHP_EOL . 'File: ' . $thisError->file; > libxml_clear_errors(); > else echo 'Happy Days'; >
We will be greeted with the following
FATAL ERROR: 76 Message: Opening and ending tag mismatch: document line 2 and doc Line: 13 Column: 10 File: filepath/filename.xml
However as soon as we fix this problem, we are presented with «Happy Days».
Предопределённые константы
Перечисленные ниже константы определены данным модулем и могут быть доступны только в том случае, если PHP был собран с поддержкой этого модуля или же в том случае, если данный модуль был динамически загружен во время выполнения.
LIBXML_BIGLINES ( int ) Позволяет корректно сообщать о номерах строк больше 65535.
Замечание:
Доступно начиная с PHP 7.0.0 с Libxml >= 2.9.0
LIBXML_COMPACT ( int ) Активирует оптимизацию выделения памяти для небольших узлов. Это может повысить быстродействие приложения без внесения изменений в коде.
Замечание:
Доступна только в Libxml >= 2.6.21
LIBXML_DTDATTR ( int ) DTD-атрибуты по умолчанию LIBXML_DTDLOAD ( int ) Загрузка внешнего подмножества LIBXML_DTDVALID ( int ) Проверка корректности документа с помощью DTD
Включение проверки DTD может способствовать атакам XML External Entity (XXE).
LIBXML_HTML_NOIMPLIED ( int ) Устанавливает флаг HTML_PARSE_NOIMPLIED, который отключает автоматическое добавление недостающих html/body. элементов.
Замечание:
Доступно только в Libxml >= 2.7.7 (начиная с PHP >= 5.4.0)
LIBXML_HTML_NODEFDTD ( int ) Устанавливает флаг HTML_PARSE_NODEFDTD, который предотвращает добавление стандартного doctype, если он не был найден.
Замечание:
Доступно только в Libxml >= 2.7.8 (начиная с >= 5.4.0)
LIBXML_NOBLANKS ( int ) Удаление пустых узлов LIBXML_NOCDATA ( int ) Объединить CDATA как текстовые узлы LIBXML_NOEMPTYTAG ( int ) Разворачивать пустые теги (например,
в )
Замечание:
Эта настройка доступна на данный момент только в функциях DOMDocument::save и DOMDocument::saveXML.
Включение замещения объекта может облегчить атаки на внешний объект XML (XXE).
LIBXML_NOERROR ( int ) Запретить отчёты об ошибках LIBXML_NONET ( int ) Отключить доступ к сети во время загрузки документов LIBXML_NOWARNING ( int ) Запретить предупреждения LIBXML_NOXMLDECL ( int ) Убрать объявление XML при сохранении документа
Замечание:
Доступна только в Libxml >= 2.6.21
LIBXML_NSCLEAN ( int ) Удалить лишние объявления пространств имён LIBXML_PARSEHUGE ( int ) Установить флаг XML_PARSE_HUGE, который отключает жёстко заданный лимит в парсере. Это затрагивает как лимиты максимальной глубины документа или рекурсии элементов, так и лимиты размеров текстовых элементов.
Замечание:
Доступно только в Libxml >= 2.7.0 (начиная с PHP >= 5.3.2 и PHP >= 5.2.12)
LIBXML_PEDANTIC ( int ) Устанавливает флаг XML_PARSE_PEDANTIC, который включает педантичный отчёт об ошибках.
Замечание:
Доступно с PHP >= 5.4.0
LIBXML_XINCLUDE ( int ) Реализовать замещение XInclude LIBXML_ERR_ERROR ( int ) Устранимая ошибка LIBXML_ERR_FATAL ( int ) Фатальная ошибка LIBXML_ERR_NONE ( int ) Нет ошибок LIBXML_ERR_WARNING ( int ) Простое предупреждение LIBXML_VERSION ( int ) Версия libxml в виде 20605 или 20617 LIBXML_DOTTED_VERSION ( string ) Версия libxml в виде 2.6.5 или 2.6.17 LIBXML_SCHEMA_CREATE ( int ) Создаёт со значением по умолчанию или фиксированные узлы при проверке схемы XSD
Замечание:
Доступно только в Libxml >= 2.6.14 (в PHP >= 5.5.2)