Repository navigation
Expand file tree
/
Copy pathprocess.php
More file actions
49 lines (43 loc) · 1.46 KB
/
Copy pathprocess.php
File metadata and controls
49 lines (43 loc) · 1.46 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
<?php
function parseGivenLineBySeparator($line, $index, $separator){
return trim(preg_replace('/\s\s+/', '', explode($separator, $line)[$index]));
}
$inputFile = fopen("input/input.txt", "r") or die("Unable to open file!");
$lines = [];
$lineCount = 0;
while (!feof($inputFile)) {
$lines[] = fgets($inputFile);
$lineCount++;
}
$dom = new DOMDocument();
$dom->encoding = 'utf-8';
$dom->xmlVersion = '1.0';
$dom->formatOutput = true;
$outputFilePath = 'output/output.xml';
$order = $dom->createElement('order');
$header = $dom->createElement('header');
for ($index = 0; $index < count(explode(";", $lines[0])); $index++) {
$childNode = $dom->createElement(
parseGivenLineBySeparator($lines[0], $index, ";"),
parseGivenLineBySeparator($lines[1], $index, ";")
);
$header->appendChild($childNode);
}
$linesTag = $dom->createElement('lines');
for ($line = 3; $line < $lineCount; $line++) {
$lineTag = $dom->createElement('line');
for ($index = 0; $index < count(explode(";", $lines[2])); $index++) {
$childNode = $dom->createElement(
parseGivenLineBySeparator($lines[2], $index, ";"),
parseGivenLineBySeparator($lines[$line], $index, ";")
);
$lineTag->appendChild($childNode);
}
$linesTag->appendChild($lineTag);
}
$order->appendChild($header);
$order->appendChild($linesTag);
$dom->appendChild($order);
$dom->save($outputFilePath);
echo "$outputFilePath has been successfully created";
?>