-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate_xml.php
98 lines (73 loc) · 1.61 KB
/
validate_xml.php
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
/**
* DOM
*/
function test_dom($xml_file)
{
$doc = new DOMDocument();
$valid = @$doc->loadXML(file_get_contents($xml_file));
unset($doc);
return $valid;
}
/**
* SimpleXML
*/
function test_simplexml($xml_file)
{
$sxe = @simplexml_load_file($xml_file);
$valid = is_object($sxe)? true: false;
unset($sxe);
return $valid;
}
/**
* XMLReader
*/
function test_xmlreader($xml_file)
{
$reader = new XMLReader();
$reader->open($xml_file, null, 1<<19);
libxml_clear_errors();
$use_internal_errors = libxml_use_internal_errors(true);
while (@$reader->read()) {}
$errors = libxml_get_errors();
$valid = count($errors)? false: true;
libxml_use_internal_errors($use_internal_errors);
libxml_clear_errors();
$reader->close();
return $valid;
}
function test($function, $xml_file)
{
switch ($function)
{
case 'dom':
$result = test_dom($xml_file);
break;
case 'simplexml':
$result = test_simplexml($xml_file);
break;
case 'xmlreader':
$result = test_xmlreader($xml_file);
break;
}
return $result;
}
if (defined('STDIN'))
{
$time = microtime(true);
$function = $argv[1];
$xml_file = $argv[2];
$valid = test($function, $xml_file);
$valid = ($valid? 'true': 'false');
$memory_peak = memory_get_peak_usage(true);
$time = microtime(true) - $time;
$to_file = <<<TOFILE
Function: $function
XML File: $xml_file
Is valid file: $valid
Time (seconds): $time
Memory Peak (bytes): $memory_peak
--------------------
TOFILE;
file_put_contents('benchmark_validate.txt', $to_file, FILE_APPEND);
}