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