|
2 | 2 |
|
3 | 3 | namespace Statamic\Importer\Sources;
|
4 | 4 |
|
| 5 | +use DOMDocument; |
5 | 6 | use Illuminate\Support\LazyCollection;
|
| 7 | +use XMLReader; |
6 | 8 |
|
7 | 9 | class Xml extends AbstractSource
|
8 | 10 | {
|
9 | 11 | public function getItems(string $path): LazyCollection
|
10 | 12 | {
|
11 |
| - $xml = simplexml_load_file($path); |
12 |
| - |
13 |
| - return LazyCollection::make(function () use ($xml) { |
14 |
| - foreach ($xml->channel->item as $item) { |
15 |
| - $array = []; |
| 13 | + return LazyCollection::make(function () use ($path) { |
| 14 | + $reader = new XMLReader; |
| 15 | + $reader->open($path); |
| 16 | + |
| 17 | + while ($reader->read()) { |
| 18 | + if ($reader->nodeType === XMLReader::ELEMENT && $reader->name === 'item') { |
| 19 | + $node = $reader->expand(); |
| 20 | + $array = []; |
| 21 | + |
| 22 | + $doc = new DOMDocument; |
| 23 | + $node = $doc->importNode($node, true); |
| 24 | + $doc->appendChild($node); |
| 25 | + $item = simplexml_import_dom($doc); |
| 26 | + |
| 27 | + foreach ($item as $key => $value) { |
| 28 | + $array[$key] = (string) $value; |
| 29 | + } |
16 | 30 |
|
17 |
| - foreach ($item as $key => $value) { |
18 |
| - $array[$key] = (string) $value; |
19 |
| - } |
| 31 | + foreach ($item->getDocNamespaces(true) as $namespace => $uri) { |
| 32 | + // Access namespaced elements using the namespace prefix |
| 33 | + foreach ($item->children($uri) as $key => $value) { |
| 34 | + $array[$namespace.':'.$key] = (string) $value; |
| 35 | + } |
20 | 36 |
|
21 |
| - foreach ($item->getDocNamespaces(true) as $namespace => $uri) { |
22 |
| - // Access namespaced elements using the namespace prefix |
23 |
| - foreach ($item->children($uri) as $key => $value) { |
24 |
| - $array[$namespace.':'.$key] = (string) $value; |
| 37 | + // If you want to access attributes in the namespaced elements |
| 38 | + foreach ($item->attributes($uri) as $key => $value) { |
| 39 | + $array[$namespace.':'.$key] = (string) $value; |
| 40 | + } |
25 | 41 | }
|
26 | 42 |
|
27 |
| - // If you want to access attributes in the namespaced elements |
28 |
| - foreach ($item->attributes($uri) as $key => $value) { |
29 |
| - $array[$namespace.':'.$key] = (string) $value; |
| 43 | + if (isset($array['wp:post_type']) && $array['wp:post_type'] === 'attachment') { |
| 44 | + continue; |
30 | 45 | }
|
31 |
| - } |
32 | 46 |
|
33 |
| - // WordPress: Filter out any `attachment` post types. |
34 |
| - if (isset($array['wp:post_type']) && $array['wp:post_type'] === 'attachment') { |
35 |
| - continue; |
| 47 | + yield $array; |
36 | 48 | }
|
37 |
| - |
38 |
| - yield $array; |
39 | 49 | }
|
| 50 | + |
| 51 | + $reader->close(); |
40 | 52 | });
|
41 | 53 | }
|
42 | 54 | }
|
0 commit comments