1313namespace Derafu \Xml ;
1414
1515use Derafu \Xml \Contract \XmlDocumentInterface ;
16- use Derafu \Xml \Exception \XmlException ;
16+ use Derafu \Xml \Exception \XmlParseException ;
17+ use Derafu \Xml \Exception \XmlQueryException ;
1718use DOMDocument ;
1819use DOMElement ;
1920use DOMNode ;
@@ -95,39 +96,11 @@ public function loadXml(string $source, int $options = 0): bool
9596 {
9697 // If there is no XML string in the source, then an exception is thrown.
9798 if (empty ($ source )) {
98- throw new XmlException (
99+ throw new XmlParseException (
99100 'The XML content that you want to load is empty. '
100101 );
101102 }
102103
103- // Convert the XML if necessary.
104- preg_match (
105- '/<\?xml\s+version="([^"]+)"\s+encoding="([^"]+)"\?>/ ' ,
106- $ source ,
107- $ matches
108- );
109- //$version = $matches[1] ?? $this->xmlVersion;
110- $ encoding = strtoupper ($ matches [2 ] ?? $ this ->encoding );
111- if ($ encoding === 'UTF-8 ' && $ this ->encoding === 'ISO-8859-1 ' ) {
112- $ source = mb_convert_encoding ($ source , 'ISO-8859-1 ' , 'UTF-8 ' );
113- $ source = str_replace (
114- ' encoding="UTF-8"?> ' ,
115- ' encoding="ISO-8859-1"?> ' ,
116- $ source
117- );
118- }
119-
120- // If the XML that will be loaded does not start with the XML tag, it
121- // is added. This is 100% necessary because if it comes in a different
122- // encoding to UTF-8 (the most normal) and does not come with this tag
123- // opening the XML, it will complain that the encoding is missing when
124- // loading.
125- if (!str_starts_with ($ source , '<?xml ' )) {
126- $ source = '<?xml version="1.0" encoding=" ' . $ encoding . '"?> '
127- . "\n" . $ source
128- ;
129- }
130-
131104 // Get the current state of libxml and change it before loading the XML
132105 // to get the errors in a variable if something fails.
133106 $ useInternalErrors = libxml_use_internal_errors (true );
@@ -141,7 +114,7 @@ public function loadXml(string $source, int $options = 0): bool
141114 libxml_use_internal_errors ($ useInternalErrors );
142115
143116 if (!$ status ) {
144- throw new XmlException ('Error al cargar el XML. ' , $ errors );
117+ throw new XmlParseException ('Error al cargar el XML. ' , $ errors );
145118 }
146119
147120 // Return the status of the XML loading.
@@ -183,7 +156,7 @@ public function C14NWithIso88591Encoding(?string $xpath = null): string
183156 if ($ xpath ) {
184157 $ node = $ this ->getNodes ($ xpath )->item (0 );
185158 if (!$ node ) {
186- throw new XmlException (sprintf (
159+ throw new XmlQueryException (sprintf (
187160 'No fue posible obtener el nodo con el XPath %s. ' ,
188161 $ xpath
189162 ));
0 commit comments