PluginProbe ʕ •ᴥ•ʔ
WP All Import – Drag & Drop Import for CSV, XML, Excel & Google Sheets / 3.0
WP All Import – Drag & Drop Import for CSV, XML, Excel & Google Sheets v3.0
4.1.1 3.9.5 3.9.6 4.0.0 4.0.1 4.1.0 trunk 2.12 2.13 2.14 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 3.4.7 3.4.8 3.4.9 3.5.0 3.5.1 3.5.2 3.5.3 3.5.4 3.5.5 3.5.6 3.5.7 3.5.8 3.5.9 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.6.9 3.7.0 3.7.1 3.7.2 3.7.3 3.7.3-beta-1.0 3.7.4 3.7.4-beta-1.0 3.7.5 3.7.6 3.7.7 3.7.8 3.7.9 3.8.0 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4
wp-all-import / libraries / XmlImportParser.php
wp-all-import / libraries Last commit date
ast 13 years ago cache 13 years ago XmlImportConfig.php 13 years ago XmlImportCsvParse.php 13 years ago XmlImportException.php 13 years ago XmlImportParser.php 13 years ago XmlImportReaderInterface.php 13 years ago XmlImportStringReader.php 13 years ago XmlImportTemplate.php 13 years ago XmlImportTemplateCodeGenerator.php 13 years ago XmlImportTemplateParser.php 13 years ago XmlImportTemplateScanner.php 13 years ago XmlImportToken.php 13 years ago pclzip.lib.php 13 years ago
XmlImportParser.php
116 lines
1 <?php
2 /**
3 * @author Olexandr Zanichkovsky <olexandr.zanichkovsky@zophiatech.com>
4 * @author Pavel Kulbakin <p.kulbakin@gmail.com>
5 * @package General
6 */
7
8 require_once dirname(__FILE__) . '/XmlImportTemplate.php';
9
10 /**
11 * Is used to parse XML using specified template and root node
12 */
13 class XmlImportParser {
14 /**
15 * SimpleXmlElement instance for the xml file
16 *
17 * @var SimpleXMLElement
18 */
19 protected $xml;
20
21 /**
22 * XPath expression for selecting the root node of the record
23 *
24 * @var string
25 */
26 protected $rootNodeXPath;
27
28 /**
29 * Path to cached template
30 *
31 * @var string
32 */
33 protected $cachedTemplate;
34
35 /**
36 * Creates new parser instance
37 *
38 * @param string $xml
39 * @param string $rootNodeXPath XPath for the record root node
40 * @param string $cachedTemplate path to cached template
41 * @param bool $isURL whether xml is URL or path
42 */
43 public function __construct($xml, $rootNodeXPath, $cachedTemplate, $isURL = false)
44 {
45 if ($isURL) {
46 $xml = file_get_contents($xml);
47 }
48
49 // FIX: remove default namespace because libxml xpath implementation doesn't handle it properly
50 $xml and $xml = preg_replace('%xmlns\s*=\s*([\'"]).*\1%sU', '', $xml);
51
52 libxml_use_internal_errors(true);
53 try{
54 $this->xml = new SimpleXMLElement($xml);
55 } catch (Exception $e){
56 try{
57 $this->xml = new SimpleXMLElement(utf8_encode($xml));
58 } catch (Exception $e){
59 throw new XmlImportException($e->getMessage());
60 }
61 }
62
63 $this->rootNodeXPath = $rootNodeXPath;
64 $this->cachedTemplate = $cachedTemplate;
65 }
66
67 /**
68 * Gets the parser results for all records or
69 * the part of them if $start and $count parameters are passed
70 *
71 * @param array[optional] $records Sequence numbers of records to import (first record corresponds to 1)
72 * @return array
73 */
74 public function parse($records = array())
75 {
76 empty($records) or is_array($records) or $records = array($records);
77
78 $result = array();
79
80 $rootNodes = $this->xml->xpath($this->rootNodeXPath);
81 if ($rootNodes === false)
82 throw new XmlImportException('Invalid root node XPath \'' . $this->rootNodeXPath . '\' specified');
83
84 for ($i = 0; $i < count($rootNodes); $i++) {
85 if (empty($records) or in_array($i + 1, $records)) {
86 $rootNode = $rootNodes[$i];
87 $template = new XmlImportTemplate($rootNode, $this->cachedTemplate);
88 $result[] = $template->parse();
89 }
90 }
91
92 return $result;
93 }
94
95 /**
96 * Creates new parser instance for text template specified
97 *
98 * @param string $xml
99 * @param string $rootNodeXPath XPath for the record root node
100 * @param string $template template
101 * @param string &$file path to the cached template
102 * @return XmlImportParser
103 */
104 public static function factory($xml, $rootNodeXPath, $template, &$file = NULL)
105 {
106
107 $scanner = new XmlImportTemplateScanner();
108 $tokens = $scanner->scan(new XmlImportStringReader($template));
109 $t_parser = new XmlImportTemplateParser($tokens);
110 $tree = $t_parser->parse();
111 $codegenerator = new XmlImportTemplateCodeGenerator($tree);
112 $file = $codegenerator->generate();
113
114 return new self($xml, $rootNodeXPath, $file);
115 }
116 }