-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathXml.php
More file actions
69 lines (55 loc) · 1.85 KB
/
Copy pathXml.php
File metadata and controls
69 lines (55 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
namespace PHPCensor\Helper;
use DOMDocument;
use Exception;
use LibXMLError;
use PHPCensor\Helper\Xml\Utf8CleanFilter;
use SimpleXMLElement;
/**
* @package PHP Censor
* @subpackage Application
*
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
class Xml
{
/**
*
* @return SimpleXMLElement|null
*/
public static function loadFromFile($filePath)
{
\stream_filter_register('xml_utf8_clean', Utf8CleanFilter::class);
try {
$xml = \simplexml_load_file('php://filter/read=xml_utf8_clean/resource=' . $filePath);
} catch (\Throwable $ex) {
$xml = null;
}
if (!$xml) {
// from https://stackoverflow.com/questions/7766455/how-to-handle-invalid-unicode-with-simplexml/8092672#8092672
$oldUse = \libxml_use_internal_errors(true);
\libxml_clear_errors();
$dom = new DOMDocument("1.0", "UTF-8");
$dom->strictErrorChecking = false;
$dom->validateOnParse = false;
$dom->recover = true;
$dom->loadXML(\strtr(
\file_get_contents($filePath),
['"' => "'"] // " in attribute names may mislead the parser
));
/** @var LibXMLError $xmlError */
$xmlError = \libxml_get_last_error();
if ($xmlError) {
$warning = \sprintf('L%s C%s: %s', $xmlError->line, $xmlError->column, $xmlError->message);
print 'WARNING: ignored errors while reading phpunit result, '.$warning."\n";
}
if (!$dom->hasChildNodes()) {
new SimpleXMLElement('<empty/>');
}
$xml = \simplexml_import_dom($dom);
\libxml_clear_errors();
\libxml_use_internal_errors($oldUse);
}
return $xml;
}
}