1/* 2 * FCKeditor - The text editor for Internet - http://www.fckeditor.net 3 * Copyright (C) 2003-2007 Frederico Caldeira Knabben 4 * 5 * == BEGIN LICENSE == 6 * 7 * Licensed under the terms of any of the following licenses at your 8 * choice: 9 * 10 * - GNU General Public License Version 2 or later (the "GPL") 11 * http://www.gnu.org/licenses/gpl.html 12 * 13 * - GNU Lesser General Public License Version 2.1 or later (the "LGPL") 14 * http://www.gnu.org/licenses/lgpl.html 15 * 16 * - Mozilla Public License Version 1.1 or later (the "MPL") 17 * http://www.mozilla.org/MPL/MPL-1.1.html 18 * 19 * == END LICENSE == 20 * 21 * FCKXml Class: class to load and manipulate XML files. 22 * (IE specific implementation) 23 */ 24 25var FCKXml = function() 26{ 27 this.Error = false ; 28} 29 30FCKXml.GetAttribute = function( node, attName, defaultValue ) 31{ 32 var attNode = node.attributes.getNamedItem( attName ) ; 33 return attNode ? attNode.value : defaultValue ; 34} 35 36/** 37 * Transforms a XML element node in a JavaScript object. Attributes defined for 38 * the element will be available as properties, as long as child element 39 * nodes, but the later will generate arrays with property names prefixed with "$". 40 * 41 * For example, the following XML element: 42 * 43 * <SomeNode name="Test" key="2"> 44 * <MyChild id="10"> 45 * <OtherLevel name="Level 3" /> 46 * </MyChild> 47 * <MyChild id="25" /> 48 * <AnotherChild price="499" /> 49 * </SomeNode> 50 * 51 * ... results in the following object: 52 * 53 * { 54 * name : "Test", 55 * key : "2", 56 * $MyChild : 57 * [ 58 * { 59 * id : "10", 60 * $OtherLevel : 61 * { 62 * name : "Level 3" 63 * } 64 * }, 65 * { 66 * id : "25" 67 * } 68 * ], 69 * $AnotherChild : 70 * [ 71 * { 72 * price : "499" 73 * } 74 * ] 75 * } 76 */ 77FCKXml.TransformToObject = function( element ) 78{ 79 if ( !element ) 80 return null ; 81 82 var obj = {} ; 83 84 var attributes = element.attributes ; 85 for ( var i = 0 ; i < attributes.length ; i++ ) 86 { 87 var att = attributes[i] ; 88 obj[ att.name ] = att.value ; 89 } 90 91 var childNodes = element.childNodes ; 92 for ( i = 0 ; i < childNodes.length ; i++ ) 93 { 94 var child = childNodes[i] ; 95 96 if ( child.nodeType == 1 ) 97 { 98 var childName = '$' + child.nodeName ; 99 var childList = obj[ childName ] ; 100 if ( !childList ) 101 childList = obj[ childName ] = [] ; 102 103 childList.push( this.TransformToObject( child ) ) ; 104 } 105 } 106 107 return obj ; 108}