1: <?php
2:
3: /**
4: * This file contains the XML reader class.
5: *
6: * @package Core
7: * @subpackage XML
8: * @author Dominik Ziegler
9: * @copyright four for business AG <www.4fb.de>
10: * @license http://www.contenido.org/license/LIZENZ.txt
11: * @link http://www.4fb.de
12: * @link http://www.contenido.org
13: */
14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: /**
18: * XML reader class
19: *
20: * @package Core
21: * @subpackage XML
22: */
23: class cXmlReader extends cXmlBase {
24:
25: /**
26: * Loads a XML document from file and initializes a corresponding DOMXPath
27: * instance.
28: *
29: * @param string $filename
30: * path to the XML document
31: * @throws cException
32: * if file could not be loaded
33: * @return bool
34: * load state (true = successfully loaded, false = not found or loaded)
35: */
36: public function load($filename) {
37:
38: if (cFileHandler::exists($filename) === false) {
39: return false;
40: }
41:
42: // Load document via object method to avoid warning in PHP strict mode.
43: $doc = new DOMDocument();
44: if (false === $doc->load($filename)) {
45: throw new cException('Could not load file "' . $filename . '"');
46: }
47:
48:
49: $this->setDomDocument($doc);
50:
51: return $this->_dom instanceof DOMDocument;
52: }
53:
54: /**
55: * Loads a XML document from file and initializes a corresponding DOMXPath
56: * instance.
57: *
58: * @param string $sFilename
59: * path to the XML document
60: * @throws cException
61: * if XML could not be loaded
62: * @return bool
63: * load state (true = successfully loaded, false = not found or loaded)
64: */
65: public function loadXML($sXml) {
66: // Load document via object method to avoid warning in PHP strict mode.
67: $oDoc = new DOMDocument();
68: if (false === $oDoc->loadXML($sXml)) {
69: throw new cException('could not load XML');
70: }
71:
72: $this->_dom = $oDoc;
73: $this->_initXpathInstance();
74:
75: return $this->_dom instanceof DOMDocument;
76: }
77:
78: /**
79: * Returns a DOMNodeList for a given XPath expression.
80: *
81: * @param string $path
82: * xpath string
83: * @throws cException if there is no xpath
84: * @return DOMNodeList
85: */
86: public function getXpathNodeList($path) {
87: if ($this->_xpath === NULL) {
88: throw new cException('Can not execute XPath string: DOMXpath instance not found.');
89: }
90:
91: return $this->_xpath->query(parent::resolvePath($path));
92: }
93:
94: /**
95: * Returns the element of an DOMNodeList read out by a xpath string.
96: *
97: * @param string $path
98: * xpath string
99: * @param int $nodeKey [optional, default: 0]
100: * node key
101: * @return DOMNode
102: */
103: public function getXpathNode($path, $nodeKey = 0) {
104: $path = parent::getLevelXpath($path, $nodeKey);
105:
106: $domNodeList = $this->getXpathNodeList($path);
107: return $domNodeList->item(0);
108: }
109:
110: /**
111: * Returns the value of an DOMNode read out by a xpath string.
112: *
113: * @param string $path
114: * xpath string
115: * @param int $nodeKey [optional, default: 0]
116: * node key
117: * @return string
118: * value of DOMNode
119: */
120: public function getXpathValue($path, $nodeKey = 0) {
121:
122: $domNode = $this->getXpathNode($path, $nodeKey);
123: return $this->_decode($domNode->nodeValue);
124: }
125:
126: /**
127: * Returns the amount of nodes in a given XPath string.
128: *
129: * @param string $path
130: * XPath string
131: * @return int
132: * amount of nodes in node list
133: */
134: public function countXpathNodes($path) {
135: $domNodeList = $this->getXpathNodeList($path);
136:
137: if (isset($domNodeList->length)) {
138: $length = (int) $domNodeList->length;
139: } else {
140: $length = 0;
141: }
142:
143: return $length;
144: }
145:
146: /**
147: * Decodes the value if XML document has not UTF-8 encoding.
148: *
149: * @param string $value
150: * value to decode
151: * @return string
152: * decoded value
153: */
154: protected function _decode($value) {
155:
156: if ($this->getEncoding() != 'UTF-8') {
157: $value = utf8_decode($value);
158: }
159:
160: return $value;
161: }
162:
163: }
164: