A PHP library for reading ONIX 3.0 files in both Short and Ref tag formats. Text elements in different formats and ONIX date formats are automatically parsed. Code list values are resolved to human-readable strings.
This library is under active development. Most fields are detected and parsed, but some still need work.
- PHP >= 8.3
- Symfony Serializer ^7.4
composer require ramlev/dom-onix-parser
$parser = new \Dso\Onix\Parser();
/** @var \Dso\Onix\Message\Message */
$message = $parser->parseString(file_get_contents('sample.xml'));
/** @var \Dso\Onix\Product\Product[] */
$products = $message->getProducts();All code lists from issue 61 are included. Codes in the ONIX file are automatically resolved to their human-readable values.
<Product>
<NotificationType>03</NotificationType>
</Product>$type = $product->getNotificationType();
$type->getCode(); // "03"
$type->getValue(); // "Notification confirmed on publication"
(string) $type; // "Notification confirmed on publication"Pass a language code to the parser constructor to get code list values in that language:
$parser = new \Dso\Onix\Parser('de');| Language | Code |
|---|---|
| English (default) | en |
| Spanish | es |
| German | de |
| French | fr |
| Italian | it |
| Norwegian | nb |
| Turkish | tr |
Code lists were scraped from the EDITEUR website. Some translations may be incomplete or inaccurate — pull requests are welcome.
Text fields are returned as Text objects and support multiple ONIX text formats (plain, HTML, XHTML, XML):
$text = $textContent->getText();
(string) $text; // raw content
$text->toPlain(); // strips HTML tags, converts <br> to newlines
$text->toHtml(); // returns HTML; wraps plain text in <p> tagsDate fields are returned as Date objects and support all ONIX date format codes (CodeList 55), including ranges, weeks, quarters and seasons:
$date = $publishingDate->getDate();
$date->format('Y-m-d'); // e.g. "2024-03-15"
$date->format('d. F Y'); // e.g. "15. March 2024"$detail = $product->getDescriptiveDetail();
// Loop all measures
foreach ($detail->getMeasures() as $measure) {
echo sprintf('%s: %s %s',
(string) $measure->getMeasureType(), // e.g. "Height"
$measure->getMeasurement(), // e.g. "210"
(string) $measure->getMeasureUnitCode() // e.g. "Centimeters"
);
}
// Shorthand helpers
$detail->getHeight();
$detail->getWidth();
$detail->getThickness();
$detail->getWeight();composer test
MIT — see LICENSE.