From 04f1fe5385dd8853b62dfb136e7661f2906abb18 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Sun, 5 Aug 2007 15:55:02 +0000 Subject: [PATCH] #841 - cross-browser XML DOM methods for creation, traversal, reading, and writing - see the xml.html example for use git-svn-id: http://svn.openlayers.org/trunk/openlayers@3862 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- examples/xml.html | 145 ++++++++++++++++++ examples/xml/features.xml | 2 + lib/OpenLayers.js | 1 + lib/OpenLayers/Format/XML.js | 283 +++++++++++++++++++++++++++++++++++ tests/Format/test_XML.html | 194 ++++++++++++++++++++++++ tests/list-tests.html | 1 + 6 files changed, 626 insertions(+) create mode 100644 examples/xml.html create mode 100644 examples/xml/features.xml create mode 100644 lib/OpenLayers/Format/XML.js create mode 100644 tests/Format/test_XML.html diff --git a/examples/xml.html b/examples/xml.html new file mode 100644 index 0000000000..4260535264 --- /dev/null +++ b/examples/xml.html @@ -0,0 +1,145 @@ + + + + XML Parsing Example + + + + + + +

OpenLayers XML Example

+

OpenLayers has a very simple XML format class (OpenLayers.Format.XML) + that can be used to read/write XML docs. The methods available on the + XML format (or parser if you like) allow for reading and writing of the + various XML flavors used by the library - in particular the vector data + formats. It is by no means intended to be a full-fledged XML toolset. + Additional methods will be added only as needed elsewhere in the + library.

+

This page loads an XML document and demonstrates a few of the methods + available in the parser.

+

Status: XML document loading...

+

After the XML document loads, see the result of a few of the methods + below. Assume that you start with the following code: +
+ + var format = new OpenLayers.Format.XML(); + +

+ Sample methods + + Output: +
 
+ + diff --git a/examples/xml/features.xml b/examples/xml/features.xml new file mode 100644 index 0000000000..b213ce5dd3 --- /dev/null +++ b/examples/xml/features.xml @@ -0,0 +1,2 @@ + +-107.7912454726602 43.649560413854424-107.75539905577847 43.6677494686189260430N0910W27Aw2sw;WYB 0016999ABUREAU OF LAND MGMT0.0614.3Authorized310781O&g renewal lease - pdOil & gas06/5/1926Rlty rate - 5%HBPORGAS VENTURES LLC100.00.00.00.06/1/2006-107.75540341813374 43.65318043604783 -107.75540766903033 43.649560413854424 -107.76039213131902 43.64957232716459 -107.76537647481773 43.649584044882054 -107.76600694778301 43.649585553307226 -107.76600544447962 43.65320449790224 -107.76600393275089 43.65682260581091 -107.77035309969853 43.6568319555119 -107.77533746205971 43.65684246461631 -107.77533369030677 43.66046005010295 -107.78032119967183 43.66047517767307 -107.78114989067903 43.660477553258325 -107.7811491411714 43.66409732386495 -107.78530636850998 43.66411137468226 -107.78619730956676 43.664114220754314 -107.79029430779957 43.6641274142625 -107.7912454726602 43.66413046978637 -107.79124472581245 43.66774946861892 -107.79029254907311 43.667746432392896 -107.78530411910795 43.66773049422058 -107.7803154837038 43.66771429284182 -107.77532694645721 43.66769786251535 -107.77034201441859 43.66768723301139 -107.76599111151326 43.667677746482155 -107.76599928176243 43.66406177993355 -107.76600204937104 43.66044527933786 -107.76536482605789 43.660441720601554 -107.76095267723535 43.66043320984291 -107.76037976752744 43.6604312952967 -107.76038385503145 43.656811633534815 -107.75539905577847 43.65680054792165 -107.75540341813374 43.65318043604783-107.76038385503497 43.65314461898675-107.74044949722713 43.6604312953016360430N0910W34Anene;WYB 0017060ABUREAU OF LAND MGMT0.0190.0Authorized310781O&g renewal lease - pdOil & gas08/14/1929Rlty rate - 5%HBPORTEXACO EXPL & PROD INC100.00.00.00.06/1/2006-107.74605488318316 43.65994411135142 -107.74543221894442 43.659942507723265 -107.74543182097408 43.66039495347534 -107.74044949722713 43.66038434024628 -107.74045205662398 43.65676451042827 -107.74045468122058 43.65314461898675 -107.74543785843247 43.65315677493463 -107.74543483251206 43.656775865277204 -107.74792589467117 43.656782141688055 -107.7504169506792 43.65678836105594 -107.75290800688019 43.65679449548188 -107.75539905578172 43.65680054791882 -107.76038385503497 43.6568116335444 -107.76037976752876 43.66043129530163 -107.75590467181928 43.66042401057107 -107.75539470030401 43.66042058014666 -107.75539522492454 43.65996803160492 -107.75477258519014 43.65996648396323 -107.75414984843758 43.659964934969224 -107.75352723875065 43.65996338002915 -107.7529045032101 43.65996182230788 -107.7522817671415 43.65996026343829 -107.75165902657075 43.65995861118674 -107.75103641630865 43.65995704018709 -107.75041368089654 43.65995547101946 -107.74979106335141 43.659953806253434 -107.74916845036381 43.659952225696536 -107.74854571394238 43.659950645819315 -107.74792297797997 43.659949063070066 -107.74730023769644 43.65994738546058 -107.74667762361214 43.6599457929788 -107.74605488318316 43.65994411135142 \ No newline at end of file diff --git a/lib/OpenLayers.js b/lib/OpenLayers.js index 613bb23f7f..29b4139028 100644 --- a/lib/OpenLayers.js +++ b/lib/OpenLayers.js @@ -159,6 +159,7 @@ "OpenLayers/Layer/Vector.js", "OpenLayers/Layer/GML.js", "OpenLayers/Format.js", + "OpenLayers/Format/XML.js", "OpenLayers/Format/GML.js", "OpenLayers/Format/KML.js", "OpenLayers/Format/GeoRSS.js", diff --git a/lib/OpenLayers/Format/XML.js b/lib/OpenLayers/Format/XML.js new file mode 100644 index 0000000000..9f88e13400 --- /dev/null +++ b/lib/OpenLayers/Format/XML.js @@ -0,0 +1,283 @@ +/* Copyright (c) 2006 MetaCarta, Inc., published under a modified BSD license. + * See http://svn.openlayers.org/trunk/openlayers/repository-license.txt + * for the full text of the license. */ + +/** + * @requires OpenLayers/Format.js + * + * Class: OpenLayers.Format.XML + * Read and write XML. For cross-browser XML generation, use methods on an + * instance of the XML format class instead of on document. + * The DOM creation and traversing methods exposed here all mimic the + * W3C XML DOM methods. Create a new parser with the + * constructor. + * + * Inherits from: + * - + */ +OpenLayers.Format.XML = OpenLayers.Class.create(); +OpenLayers.Format.XML.prototype = + OpenLayers.Class.inherit(OpenLayers.Format, { + + /** + * Property: xmldom + * {XMLDom} If this browser uses ActiveX, this will be set to a XMLDOM + * object. It is not intended to be a browser sniffing property. + * Instead, the xmldom property is used instead of document + * where namespaced node creation methods are not supported. In all + * other browsers, this remains null. + */ + xmldom: null, + + /** + * Constructor: OpenLayers.Format.XML + * Construct an XML parser. The parser is used to read and write XML. + * Reading XML from a string returns a DOM element. Writing XML from + * a DOM element returns a string. + * + * Parameters: + * options - {Object} Optional object whose properties will be set on + * the object. + */ + initialize: function(options) { + if(window.ActiveXObject) { + this.xmldom = new ActiveXObject("Microsoft.XMLDOM"); + } + OpenLayers.Format.prototype.initialize.apply(this, [options]); + }, + + /** + * APIMethod: read + * Deserialize a XML string and return a DOM node. + * + * Parameters: + * text - {String} A XML string + + * Returns: + * {DOMElement} A DOM node + */ + read: function(text) { + var index = text.indexOf('<'); + if(index > 0) { + text = text.substring(index); + } + var node = OpenLayers.Util.Try( + function() { + var xmldom; + /** + * Since we want to be able to call this method on the prototype + * itself, this.xmldom may not exist even if in IE. + */ + if(window.ActiveXObject && !this.xmldom) { + xmldom = new ActiveXObject("Microsoft.XMLDOM"); + } else { + xmldom = this.xmldom; + } + xmldom.loadXML(text); + return xmldom; + }, + function() { + return new DOMParser().parseFromString(text, 'text/xml'); + }, + function() { + var req = new XMLHttpRequest(); + req.open("GET", "data:" + "text/xml" + + ";charset=utf-8," + encodeURIComponent(text), false); + if(req.overrideMimeType) { + req.overrideMimeType("text/xml"); + } + req.send(null); + return req.responseXML; + } + ); + return node; + }, + + /** + * APIMethod: write + * Serialize a DOM node into a XML string. + * + * Parameters: + * node - {DOMElement} A DOM node. + * + * Returns: + * {String} The XML string representation of the input node. + */ + write: function(node) { + var data; + if(this.xmldom) { + data = node.xml; + } else { + var serializer = new XMLSerializer(); + data = serializer.serializeToString(node); + } + return data; + }, + + /** + * APIMethod: createElementNS + * Create a new element with namespace. This node can be appended to + * another node with the standard node.appendChild method. For + * cross-browser support, this method must be used instead of + * document.createElementNS. + * + * Parameters: + * uri - {String} Namespace URI for the element. + * name - {String} The qualified name of the element (prefix:localname). + * + * Returns: + * {Element} A DOM element with namespace. + */ + createElementNS: function(uri, name) { + var element; + if(this.xmldom) { + element = this.xmldom.createNode(1, name, uri); + } else { + element = document.createElementNS(uri, name); + } + return element; + }, + + /** + * APIMethod: createTextNode + * Create a text node. This node can be appended to another node with + * the standard node.appendChild method. For cross-browser support, + * this method must be used instead of document.createTextNode. + * + * Parameters: + * text - {String} The text of the node. + * + * Returns: + * {DOMElement} A DOM text node. + */ + createTextNode: function(text) { + var node; + if(this.xmldom) { + node = this.xmldom.createTextNode(text); + } else { + node = document.createTextNode(text); + } + return node; + }, + + /** + * APIMethod: getElementsByTagNameNS + * Get a list of elements on a node given the namespace URI and local name. + * To return all nodes in a given namespace, use '*' for the name + * argument. To return all nodes of a given (local) name, regardless + * of namespace, use '*' for the uri argument. + * + * Parameters: + * node - {Element} Node on which to search for other nodes. + * uri - {String} Namespace URI. + * name - {String} Local name of the tag (without the prefix). + * + * Returns: + * {NodeList} A node list or array of elements. + */ + getElementsByTagNameNS: function(node, uri, name) { + var elements = []; + if(node.getElementsByTagNameNS) { + elements = node.getElementsByTagNameNS(uri, name); + } else { + var allNodes = node.getElementsByTagName("*"); + var potentialNode, fullName; + for(var i=0; i + + + + + + + \ No newline at end of file diff --git a/tests/list-tests.html b/tests/list-tests.html index c8fa8d7528..5ff8756d73 100644 --- a/tests/list-tests.html +++ b/tests/list-tests.html @@ -20,6 +20,7 @@
  • Geometry/test_Rectangle.html
  • Geometry/test_Surface.html
  • test_Format.html
  • +
  • Format/test_XML.html
  • Format/test_GeoRSS.html
  • Format/test_GML.html
  • Format/test_WKT.html