2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
7 * @fileOverview Defines the {@link CKEDITOR.xml} class, which represents a
\r
8 * loaded XML document.
\r
13 CKEDITOR.plugins.add( 'xml', {});
\r
16 * Represents a loaded XML document.
\r
18 * @param {object|string} xmlObjectOrData A native XML (DOM document) object or
\r
19 * a string containing the XML definition to be loaded.
\r
21 * var xml = <b>new CKEDITOR.xml( '<books><book title="My Book" /></books>' )</b>;
\r
23 CKEDITOR.xml = function( xmlObjectOrData )
\r
27 if ( typeof xmlObjectOrData == 'object' )
\r
28 baseXml = xmlObjectOrData;
\r
31 var data = ( xmlObjectOrData || '' ).replace( / /g, '\xA0' );
\r
32 if ( window.DOMParser )
\r
33 baseXml = (new DOMParser()).parseFromString( data, 'text/xml' );
\r
34 else if ( window.ActiveXObject )
\r
36 try { baseXml = new ActiveXObject( 'MSXML2.DOMDocument' ); }
\r
39 try { baseXml = new ActiveXObject( 'Microsoft.XmlDom' ); } catch(e) {}
\r
44 baseXml.async = false;
\r
45 baseXml.resolveExternals = false;
\r
46 baseXml.validateOnParse = false;
\r
47 baseXml.loadXML( data );
\r
53 * The native XML (DOM document) used by the class instance.
\r
57 this.baseXml = baseXml;
\r
60 CKEDITOR.xml.prototype =
\r
63 * Get a single node from the XML document, based on a XPath query.
\r
64 * @param {String} xpath The XPath query to execute.
\r
65 * @param {Object} [contextNode] The XML DOM node to be used as the context
\r
66 * for the XPath query. The document root is used by default.
\r
67 * @returns {Object} A XML node element or null if the query has no results.
\r
69 * // Create the XML instance.
\r
70 * var xml = new CKEDITOR.xml( '<list><item id="test1" /><item id="test2" /></list>' );
\r
71 * // Get the first <item> node.
\r
72 * var itemNode = <b>xml.selectSingleNode( 'list/item' )</b>;
\r
74 * alert( itemNode.nodeName );
\r
76 selectSingleNode : function( xpath, contextNode )
\r
78 var baseXml = this.baseXml;
\r
80 if ( contextNode || ( contextNode = baseXml ) )
\r
82 if ( CKEDITOR.env.ie || contextNode.selectSingleNode ) // IE
\r
83 return contextNode.selectSingleNode( xpath );
\r
84 else if ( baseXml.evaluate ) // Others
\r
86 var result = baseXml.evaluate( xpath, contextNode, null, 9, null);
\r
87 return ( result && result.singleNodeValue ) || null;
\r
95 * Gets a list node from the XML document, based on a XPath query.
\r
96 * @param {String} xpath The XPath query to execute.
\r
97 * @param {Object} [contextNode] The XML DOM node to be used as the context
\r
98 * for the XPath query. The document root is used by default.
\r
99 * @returns {ArrayLike} An array containing all matched nodes. The array will
\r
100 * be empty if the query has no results.
\r
102 * // Create the XML instance.
\r
103 * var xml = new CKEDITOR.xml( '<list><item id="test1" /><item id="test2" /></list>' );
\r
104 * // Get the first <item> node.
\r
105 * var itemNodes = xml.selectSingleNode( 'list/item' );
\r
106 * // Alert "item" twice, one for each <item>.
\r
107 * for ( var i = 0 ; i < itemNodes.length ; i++ )
\r
108 * alert( itemNodes[i].nodeName );
\r
110 selectNodes : function( xpath, contextNode )
\r
112 var baseXml = this.baseXml,
\r
115 if ( contextNode || ( contextNode = baseXml ) )
\r
117 if ( CKEDITOR.env.ie || contextNode.selectNodes ) // IE
\r
118 return contextNode.selectNodes( xpath );
\r
119 else if ( baseXml.evaluate ) // Others
\r
121 var result = baseXml.evaluate( xpath, contextNode, null, 5, null);
\r
126 while ( ( node = result.iterateNext() ) )
\r
127 nodes.push( node );
\r
136 * Gets the string representation of hte inner contents of a XML node,
\r
137 * based on a XPath query.
\r
138 * @param {String} xpath The XPath query to execute.
\r
139 * @param {Object} [contextNode] The XML DOM node to be used as the context
\r
140 * for the XPath query. The document root is used by default.
\r
141 * @returns {String} The textual representation of the inner contents of
\r
142 * the node or null if the query has no results.
\r
144 * // Create the XML instance.
\r
145 * var xml = new CKEDITOR.xml( '<list><item id="test1" /><item id="test2" /></list>' );
\r
146 * // Alert "<item id="test1" /><item id="test2" />".
\r
147 * alert( xml.getInnerXml( 'list' ) );
\r
149 getInnerXml : function( xpath, contextNode )
\r
151 var node = this.selectSingleNode( xpath, contextNode ),
\r
155 node = node.firstChild;
\r
158 if ( node.xml ) // IE
\r
159 xml.push( node.xml );
\r
160 else if ( window.XMLSerializer ) // Others
\r
161 xml.push( ( new XMLSerializer() ).serializeToString( node ) );
\r
163 node = node.nextSibling;
\r
167 return xml.length ? xml.join( '' ) : null;
\r