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