5274 |
17 Mar 10 |
nicklas |
/* $Id $ |
5274 |
17 Mar 10 |
nicklas |
2 |
------------------------------------------------------------------ |
5274 |
17 Mar 10 |
nicklas |
Copyright (C) 2010 Nicklas Nordborg |
5274 |
17 Mar 10 |
nicklas |
4 |
|
5274 |
17 Mar 10 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
5274 |
17 Mar 10 |
nicklas |
Available at http://base.thep.lu.se/ |
5274 |
17 Mar 10 |
nicklas |
7 |
|
5274 |
17 Mar 10 |
nicklas |
BASE is free software; you can redistribute it and/or |
5274 |
17 Mar 10 |
nicklas |
modify it under the terms of the GNU General Public License |
5274 |
17 Mar 10 |
nicklas |
as published by the Free Software Foundation; either version 3 |
5274 |
17 Mar 10 |
nicklas |
of the License, or (at your option) any later version. |
5274 |
17 Mar 10 |
nicklas |
12 |
|
5274 |
17 Mar 10 |
nicklas |
BASE is distributed in the hope that it will be useful, |
5274 |
17 Mar 10 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
5274 |
17 Mar 10 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5274 |
17 Mar 10 |
nicklas |
GNU General Public License for more details. |
5274 |
17 Mar 10 |
nicklas |
17 |
|
5274 |
17 Mar 10 |
nicklas |
You should have received a copy of the GNU General Public License |
5274 |
17 Mar 10 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
5274 |
17 Mar 10 |
nicklas |
20 |
------------------------------------------------------------------ |
5274 |
17 Mar 10 |
nicklas |
21 |
|
5274 |
17 Mar 10 |
nicklas |
JavaScript functions for XML documents |
5274 |
17 Mar 10 |
nicklas |
23 |
|
5274 |
17 Mar 10 |
nicklas |
@author Nicklas |
5274 |
17 Mar 10 |
nicklas |
@version 2.15 |
5274 |
17 Mar 10 |
nicklas |
26 |
*/ |
7419 |
03 Nov 17 |
nicklas |
'use strict'; |
7419 |
03 Nov 17 |
nicklas |
28 |
|
6400 |
27 Jan 14 |
nicklas |
var Xml = function() |
5274 |
17 Mar 10 |
nicklas |
30 |
{ |
6400 |
27 Jan 14 |
nicklas |
var xml = {}; |
6400 |
27 Jan 14 |
nicklas |
32 |
|
5274 |
17 Mar 10 |
nicklas |
33 |
/** |
5274 |
17 Mar 10 |
nicklas |
Create a new empty XML document |
5274 |
17 Mar 10 |
nicklas |
@param rootTag The root tag in the xml document |
5274 |
17 Mar 10 |
nicklas |
@return A Document object |
5274 |
17 Mar 10 |
nicklas |
37 |
*/ |
6400 |
27 Jan 14 |
nicklas |
xml.createDocument = function(rootTag) |
5274 |
17 Mar 10 |
nicklas |
39 |
{ |
6400 |
27 Jan 14 |
nicklas |
var doc = document.implementation.createDocument(null, rootTag, null); |
5281 |
23 Mar 10 |
nicklas |
return doc; |
5274 |
17 Mar 10 |
nicklas |
42 |
} |
5274 |
17 Mar 10 |
nicklas |
43 |
|
5274 |
17 Mar 10 |
nicklas |
44 |
/** |
5274 |
17 Mar 10 |
nicklas |
Add a tag as a child node in the document. |
5274 |
17 Mar 10 |
nicklas |
@param node The parent node |
5274 |
17 Mar 10 |
nicklas |
@param name The child tag name |
5274 |
17 Mar 10 |
nicklas |
@param value The text value of the tag (or null if not given) |
5274 |
17 Mar 10 |
nicklas |
@return The new node object |
5274 |
17 Mar 10 |
nicklas |
50 |
*/ |
6400 |
27 Jan 14 |
nicklas |
xml.addTag = function(node, name, value) |
5274 |
17 Mar 10 |
nicklas |
52 |
{ |
5274 |
17 Mar 10 |
nicklas |
var childNode = node.ownerDocument.createElement(name); |
5274 |
17 Mar 10 |
nicklas |
node.appendChild(childNode); |
5274 |
17 Mar 10 |
nicklas |
if (value) |
5274 |
17 Mar 10 |
nicklas |
56 |
{ |
5274 |
17 Mar 10 |
nicklas |
var textNode = node.ownerDocument.createTextNode(value); |
5274 |
17 Mar 10 |
nicklas |
childNode.appendChild(textNode); |
5274 |
17 Mar 10 |
nicklas |
59 |
} |
5274 |
17 Mar 10 |
nicklas |
return childNode; |
5274 |
17 Mar 10 |
nicklas |
61 |
} |
5274 |
17 Mar 10 |
nicklas |
62 |
|
5274 |
17 Mar 10 |
nicklas |
63 |
/** |
5274 |
17 Mar 10 |
nicklas |
Set an attribute for a node. |
5274 |
17 Mar 10 |
nicklas |
@param node The node |
5274 |
17 Mar 10 |
nicklas |
@param name The name of the attribute |
5274 |
17 Mar 10 |
nicklas |
@param value The value of the attribute |
5274 |
17 Mar 10 |
nicklas |
68 |
*/ |
6400 |
27 Jan 14 |
nicklas |
xml.setAttribute = function(node, name, value) |
5274 |
17 Mar 10 |
nicklas |
70 |
{ |
5274 |
17 Mar 10 |
nicklas |
node.setAttribute(name, value); |
5274 |
17 Mar 10 |
nicklas |
72 |
} |
5274 |
17 Mar 10 |
nicklas |
73 |
|
5274 |
17 Mar 10 |
nicklas |
74 |
/** |
5274 |
17 Mar 10 |
nicklas |
Convert an XML Document object to it's string representation. |
5274 |
17 Mar 10 |
nicklas |
@param document The document object |
5274 |
17 Mar 10 |
nicklas |
@return An xml string |
5274 |
17 Mar 10 |
nicklas |
78 |
*/ |
6400 |
27 Jan 14 |
nicklas |
xml.toString = function(document) |
5274 |
17 Mar 10 |
nicklas |
80 |
{ |
6400 |
27 Jan 14 |
nicklas |
var s = new XMLSerializer().serializeToString(document); |
6400 |
27 Jan 14 |
nicklas |
return s; |
5274 |
17 Mar 10 |
nicklas |
83 |
} |
6400 |
27 Jan 14 |
nicklas |
84 |
|
6400 |
27 Jan 14 |
nicklas |
return xml; |
6400 |
27 Jan 14 |
nicklas |
86 |
}(); |
5274 |
17 Mar 10 |
nicklas |
87 |
|
6400 |
27 Jan 14 |
nicklas |
88 |
|