Exceptions
All the exceptions of the library extend Derafu\Xml\Exception\XmlException, so one catch takes all of them.
The Exceptions
| Exception | When it is thrown |
|---|---|
XmlException |
A document without document element where it is needed, a validation that fails, a schema that is not found, and failures of PHP while the XML is generated (serializing, canonicalizing). |
XmlParseException |
loadXml() with an XML that is empty or is not well formed. |
XmlQueryException |
An XPath expression that is not valid or that fails, a read of XPathReader that finds no node or finds a value that is not of the type, and C14NEncoded() with an XPath that finds no node. |
XmlEncoderException |
An array that can not be converted to XML. |
XmlParseException, XmlQueryException and XmlEncoderException extend XmlException.
Array that Can Not Be Converted
XmlEncoder::encode() throws XmlEncoderException when:
- The array has more than one element at its first level, or a list: an XML has only one root element.
- A name of an element or attribute is not text (a number is the position of a value in a list, not a name).
- A value is not text: a string, a number, a boolean or an object that can be a string are valid. An array can not be the value of an attribute.
@valueis at the first level of the array, outside of any element.- A comment has two hyphens in a row or ends with a hyphen.
What the Exceptions Have
use Derafu\Xml\Exception\XmlException;
try {
$document->loadXml('<r><a></r>');
} catch (XmlException $e) {
$e->getMessage(); // "Failed to load the XML: Line 1, column 11: Opening and ending tag mismatch: a (line 1) and r"
$e->getErrors(); // A list of XmlError, one for each error of libxml.
$e->getXmlDocument(); // The document that caused the exception, if there is one.
}
getErrors()gives the errors that explain the exception, as a list ofXmlError: the ones of libxml when an XML is loaded, canonicalized or validated. It is an empty list if there are none.getXmlDocument()gives theXmlDocumentthat caused the exception, ornull.XmlQueryExceptionalso hasgetXpathExpression(): the XPath that was asked. In a read ofXPathReaderit is the full XPath of the node, and the exception also has the cause when it was a type that failed (getPrevious()).
When there are errors, the message has them, so the message says what failed. When an XML is loaded or canonicalized the message has the error that caused the failure: the first fatal one (the others that libxml reports after it are consequences of it), or else the first error. When an XML is validated the message has all the errors:
The XML validation failed using the schema schema.xsd: Line 1, column 0: Element 'wrong': No matching global declaration available for the validation root.
The Errors of libxml
An XmlError is an error that libxml found, with its data:
foreach ($e->getErrors() as $error) {
$error->level; // XmlErrorLevel::Warning, XmlErrorLevel::Error or XmlErrorLevel::Fatal.
$error->code; // The code of the error in libxml, for example 76.
$error->line; // The line of the XML, 0 if it has none.
$error->column; // The column of the line, 0 if it has none.
$error->message; // The original text of libxml.
(string) $error; // "Line 1, column 11: Opening and ending tag mismatch: a (line 1) and r"
}
An XmlError is translatable like the exceptions: $error->trans($translator) says it in the language of the translator. The text is made from the code of the error: the library has the translation of each error of libxml that it can say when it loads, canonicalizes or validates an XML (parsing, namespaces and XSD schemas). An error that is not known, or that libxml says in a way that is not known, keeps the original text of libxml. A warning says so (Warning, line 1, column 4: ...), and an error without a line (for example one of the schema) has no location.
The names of elements and attributes are shown without the namespace that libxml writes between braces ({urn:example}). The errors are in the exception for whoever wants to show them in another way: the code identifies the error better than its text.
Translating the Messages
The exceptions are translatable (they are the ones of the derafu/translation library): their messages are in English, and the library has their translation to Spanish. To have the message in another language, use trans() with a translator:
use Derafu\Translation\TranslatorFactory;
use Derafu\Xml\Translation\XmlTranslationResourceProvider;
$translator = TranslatorFactory::create('es', [], [new XmlTranslationResourceProvider()]);
try {
$document->loadXml('');
} catch (XmlException $e) {
echo $e->getMessage(); // "The XML content that you want to load is empty."
echo $e->trans($translator); // "El contenido XML que se quiere cargar está vacío."
}
The translations are in resources/translations/errors+intl-icu.es.php. In an application with the Symfony container, resources/config/xml-services.yaml of the library registers XmlTranslationResourceProvider with the tag derafu_translation.resource_provider, so its translations are loaded with the ones of the other libraries.
The errors in getErrors() are translated too: see The Errors of libxml.