OpenLayers OpenLayers

Ticket #841: xml.patch

File xml.patch, 35.7 kB (added by tschaub, 1 year ago)

cross-browser xml tools

  • examples/xml/features.xml

    old new  
     1<?xml version="1.0" encoding="UTF-8"?> 
     2<wfs:FeatureCollection numberOfFeatures="2" timeStamp="2007-08-03T13:10:00.071-06:00" xsi:schemaLocation="http://www.openplans.org/topp http://localhost:8080/geoserver/wfs?service=WFS&amp;version=1.1.0&amp;request=DescribeFeatureType&amp;typeName=topp:leases http://www.opengis.net/wfs http://localhost:8080/geoserver/schemas/wfs/1.1.0/wfs.xsd" xmlns:ogc="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:topp="http://www.openplans.org/topp" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ows="http://www.opengis.net/ows" xmlns:wfs="http://www.opengis.net/wfs"><gml:featureMembers><topp:leases gml:id="leases.1"><gml:boundedBy><gml:Envelope srsName="urn:x-ogc:def:crs:EPSG:6.11.2:4267"><gml:lowerCorner>-107.7912454726602 43.649560413854424</gml:lowerCorner><gml:upperCorner>-107.75539905577847 43.66774946861892</gml:upperCorner></gml:Envelope></gml:boundedBy><topp:meridian>6</topp:meridian><topp:township>0430N</topp:township><topp:range>0910W</topp:range><topp:section>27</topp:section><topp:surveytype>A</topp:surveytype><topp:aliquot>w2sw;</topp:aliquot><topp:serialnumb>WYB   0016999A</topp:serialnumb><topp:adminagenc>BUREAU OF LAND MGMT</topp:adminagenc><topp:price>0.0</topp:price><topp:acres>614.3</topp:acres><topp:dispositio>Authorized</topp:dispositio><topp:casetypeco>310781</topp:casetypeco><topp:casetype>O&amp;g renewal lease - pd</topp:casetype><topp:commodity>Oil &amp; gas</topp:commodity><topp:expireyear>0</topp:expireyear><topp:effectdate>6/5/1926</topp:effectdate><topp:royaltyrt>Rlty rate - 5%</topp:royaltyrt><topp:hbp>HBP</topp:hbp><topp:or>OR</topp:or><topp:name1>GAS VENTURES LLC</topp:name1><topp:perint1>100.0</topp:perint1><topp:perint2>0.0</topp:perint2><topp:perint3>0.0</topp:perint3><topp:perint4>0.0</topp:perint4><topp:updatedate>6/1/2006</topp:updatedate><topp:the_geom><gml:MultiPolygon><gml:polygonMember><gml:Polygon><gml:exterior><gml:LinearRing><gml:posList>-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</gml:posList></gml:LinearRing></gml:exterior></gml:Polygon></gml:polygonMember></gml:MultiPolygon></topp:the_geom></topp:leases><topp:leases gml:id="leases.2"><gml:boundedBy><gml:Envelope srsName="urn:x-ogc:def:crs:EPSG:6.11.2:4267"><gml:lowerCorner>-107.76038385503497 43.65314461898675</gml:lowerCorner><gml:upperCorner>-107.74044949722713 43.66043129530163</gml:upperCorner></gml:Envelope></gml:boundedBy><topp:meridian>6</topp:meridian><topp:township>0430N</topp:township><topp:range>0910W</topp:range><topp:section>34</topp:section><topp:surveytype>A</topp:surveytype><topp:aliquot>nene;</topp:aliquot><topp:serialnumb>WYB   0017060A</topp:serialnumb><topp:adminagenc>BUREAU OF LAND MGMT</topp:adminagenc><topp:price>0.0</topp:price><topp:acres>190.0</topp:acres><topp:dispositio>Authorized</topp:dispositio><topp:casetypeco>310781</topp:casetypeco><topp:casetype>O&amp;g renewal lease - pd</topp:casetype><topp:commodity>Oil &amp; gas</topp:commodity><topp:expireyear>0</topp:expireyear><topp:effectdate>8/14/1929</topp:effectdate><topp:royaltyrt>Rlty rate - 5%</topp:royaltyrt><topp:hbp>HBP</topp:hbp><topp:or>OR</topp:or><topp:name1>TEXACO EXPL &amp; PROD INC</topp:name1><topp:perint1>100.0</topp:perint1><topp:perint2>0.0</topp:perint2><topp:perint3>0.0</topp:perint3><topp:perint4>0.0</topp:perint4><topp:updatedate>6/1/2006</topp:updatedate><topp:the_geom><gml:MultiPolygon><gml:polygonMember><gml:Polygon><gml:exterior><gml:LinearRing><gml:posList>-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</gml:posList></gml:LinearRing></gml:exterior></gml:Polygon></gml:polygonMember></gml:MultiPolygon></topp:the_geom></topp:leases></gml:featureMembers></wfs:FeatureCollection> 
  • examples/xml.html

    old new  
     1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
     2        "http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-strict.dtd"> 
     3<html xmlns="http://www.w3.org/1999/xhtml"> 
     4    <head> 
     5        <title>XML Parsing Example</title> 
     6        <style type="text/css"> 
     7            body { 
     8                margin: 0 2em; 
     9                font-family: sans-serif; 
     10            } 
     11            #output { 
     12                font-family: monospace; 
     13                background-color: #efefef; 
     14                font-size: 0.9em; 
     15                padding: 1em; 
     16            } 
     17            span.code { 
     18                font-family: monospace; 
     19                background-color: #efefef; 
     20                font-size: 0.9em; 
     21                padding: 0.25em; 
     22                line-height: 1.5em; 
     23            } 
     24            ul { 
     25                margin: 0; 
     26                padding: 0 0 1em 1.5em; 
     27            } 
     28            ul li { 
     29                padding-left: 0; 
     30            } 
     31             
     32        </style> 
     33        <script src="../lib/Firebug/firebug.js" type="text/javascript"></script> 
     34        <script src="../lib/OpenLayers.js" type="text/javascript"></script> 
     35        <script type="text/javascript"> 
     36        //<![CDATA[ 
     37         
     38        var format = new OpenLayers.Format.XML(); 
     39        var doc = null; 
     40         
     41        function init() { 
     42            var url = "xml/features.xml"; 
     43            OpenLayers.loadURL(url, null, null, loadSuccess, loadFailure); 
     44        } 
     45         
     46        function loadSuccess(request) { 
     47            updateStatus("loaded"); 
     48            if(!request.responseXML.documentElement) { 
     49                doc = format.read(request.responseText); 
     50            } else { 
     51                doc = request.responseXML; 
     52            } 
     53        } 
     54         
     55        function loadFailure(request) { 
     56            updateStatus("failed to load"); 
     57        } 
     58         
     59        function updateStatus(msg) { 
     60            document.getElementById("loadStatus").firstChild.nodeValue = msg; 
     61        } 
     62 
     63        function updateOutput(text) { 
     64            document.getElementById("output").firstChild.nodeValue = text; 
     65        } 
     66         
     67        function write() { 
     68            var text = format.write(doc); 
     69            updateOutput(text); 
     70        } 
     71         
     72        function getElementsByTagNameNS(node, uri, name) { 
     73            var nodes = format.getElementsByTagNameNS(node, uri, name); 
     74            var pieces = []; 
     75            for(var i=0; i<nodes.length; ++i) { 
     76                pieces.push(format.write(nodes[i])); 
     77            } 
     78            updateOutput(pieces.join(' ')); 
     79        } 
     80         
     81        function hasAttributeNS(node, uri, name) { 
     82            updateOutput(format.hasAttributeNS(node, uri, name)) 
     83        } 
     84 
     85        function getAttributeNodeNS(node, uri, name) { 
     86            var attributeNode = format.getAttributeNodeNS(node, uri, name); 
     87            updateOutput(attributeNode.nodeName + ' = "' + 
     88                         attributeNode.nodeValue + '"'); 
     89        } 
     90 
     91        function getAttributeNS(node, uri, name) { 
     92            var attributeValue = format.getAttributeNS(node, uri, name); 
     93            updateOutput('"' + attributeValue + '"') 
     94        } 
     95         
     96        function createElementNS(uri, name) { 
     97            var node = format.createElementNS(uri, name); 
     98            doc.documentElement.appendChild(node); 
     99            write(); 
     100        } 
     101 
     102        function createTextNode(text) { 
     103            var node = format.createTextNode(text); 
     104            doc.documentElement.appendChild(node); 
     105            write(); 
     106        } 
     107 
     108        window.onload = init; 
     109         
     110        //]]> 
     111        </script> 
     112    </head> 
     113    <body> 
     114        <h3>OpenLayers XML Example</h3> 
     115        <p>OpenLayers has a very simple XML format class (OpenLayers.Format.XML) 
     116        that can be used to read/write XML docs.  The methods available on the 
     117        XML format (or parser if you like) allow for reading and writing of the 
     118        various XML flavors used by the library - in particular the vector data 
     119        formats.  It is by no means intended to be a full-fledged XML toolset. 
     120        Additional methods will be added only as needed elsewhere in the 
     121        library.</p> 
     122        <p>This page loads an XML document and demonstrates a few of the methods 
     123        available in the parser.</p> 
     124        <p>Status: <b>XML document <span id="loadStatus">loading..</span>.</b></p> 
     125        <p>After the XML document loads, see the result of a few of the methods 
     126        below.  Assume that you start with the following code: 
     127        <br /> 
     128        <span class="code"> 
     129            var format = new OpenLayers.Format.XML(); 
     130        </span> 
     131        </p> 
     132        Sample methods 
     133        <ul> 
     134            <li><a href="javascript:void write();">format.write()</a> - write the XML doc as text</li> 
     135            <li><a href="javascript:void getElementsByTagNameNS(doc, 'http://www.opengis.net/gml', 'MultiPolygon');">format.getElementsByTagNameNS()</a> - get all gml:MultiPolygon</li> 
     136            <li><a href="javascript:void hasAttributeNS(doc.documentElement, 'http://www.w3.org/2001/XMLSchema-instance', 'schemaLocation');">format.hasAttributeNS()</a> - test to see schemaLocation attribute exists in the http://www.w3.org/2001/XMLSchema-instance namespace</li> 
     137            <li><a href="javascript:void getAttributeNodeNS(doc.documentElement, 'http://www.w3.org/2001/XMLSchema-instance', 'schemaLocation');">format.getAttributeNodeNS()</a> - get schemaLocation attribute in the http://www.w3.org/2001/XMLSchema-instance namespace</li> 
     138            <li><a href="javascript:void getAttributeNS(doc.documentElement, 'http://www.w3.org/2001/XMLSchema-instance', 'schemaLocation');">format.getAttributeNS()</a> - get schemaLocation attribute value in the http://www.w3.org/2001/XMLSchema-instance namespace</li> 
     139            <li><a href="javascript:void createElementNS('http://bar.com/foo', 'foo:TestNode');">format.createElementNS()</a> - create a foo:TestNode element (and append it to the doc)</li> 
     140            <li><a href="javascript:void createTextNode('test text ');">format.createTextNode()</a> - create a text node (and append it to the doc)</li> 
     141        </ul> 
     142        Output: 
     143        <div id="output">&nbsp;</div> 
     144    </body> 
     145</html> 
  • examples/xml/features.xml

    old new  
     1<?xml version="1.0" encoding="UTF-8"?> 
     2<wfs:FeatureCollection numberOfFeatures="2" timeStamp="2007-08-03T13:10:00.071-06:00" xsi:schemaLocation="http://www.openplans.org/topp http://localhost:8080/geoserver/wfs?service=WFS&amp;version=1.1.0&amp;request=DescribeFeatureType&amp;typeName=topp:leases http://www.opengis.net/wfs http://localhost:8080/geoserver/schemas/wfs/1.1.0/wfs.xsd" xmlns:ogc="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:topp="http://www.openplans.org/topp" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ows="http://www.opengis.net/ows" xmlns:wfs="http://www.opengis.net/wfs"><gml:featureMembers><topp:leases gml:id="leases.1"><gml:boundedBy><gml:Envelope srsName="urn:x-ogc:def:crs:EPSG:6.11.2:4267"><gml:lowerCorner>-107.7912454726602 43.649560413854424</gml:lowerCorner><gml:upperCorner>-107.75539905577847 43.66774946861892</gml:upperCorner></gml:Envelope></gml:boundedBy><topp:meridian>6</topp:meridian><topp:township>0430N</topp:township><topp:range>0910W</topp:range><topp:section>27</topp:section><topp:surveytype>A</topp:surveytype><topp:aliquot>w2sw;</topp:aliquot><topp:serialnumb>WYB   0016999A</topp:serialnumb><topp:adminagenc>BUREAU OF LAND MGMT</topp:adminagenc><topp:price>0.0</topp:price><topp:acres>614.3</topp:acres><topp:dispositio>Authorized</topp:dispositio><topp:casetypeco>310781</topp:casetypeco><topp:casetype>O&amp;g renewal lease - pd</topp:casetype><topp:commodity>Oil &amp; gas</topp:commodity><topp:expireyear>0</topp:expireyear><topp:effectdate>6/5/1926</topp:effectdate><topp:royaltyrt>Rlty rate - 5%</topp:royaltyrt><topp:hbp>HBP</topp:hbp><topp:or>OR</topp:or><topp:name1>GAS VENTURES LLC</topp:name1><topp:perint1>100.0</topp:perint1><topp:perint2>0.0</topp:perint2><topp:perint3>0.0</topp:perint3><topp:perint4>0.0</topp:perint4><topp:updatedate>6/1/2006</topp:updatedate><topp:the_geom><gml:MultiPolygon><gml:polygonMember><gml:Polygon><gml:exterior><gml:LinearRing><gml:posList>-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</gml:posList></gml:LinearRing></gml:exterior></gml:Polygon></gml:polygonMember></gml:MultiPolygon></topp:the_geom></topp:leases><topp:leases gml:id="leases.2"><gml:boundedBy><gml:Envelope srsName="urn:x-ogc:def:crs:EPSG:6.11.2:4267"><gml:lowerCorner>-107.76038385503497 43.65314461898675</gml:lowerCorner><gml:upperCorner>-107.74044949722713 43.66043129530163</gml:upperCorner></gml:Envelope></gml:boundedBy><topp:meridian>6</topp:meridian><topp:township>0430N</topp:township><topp:range>0910W</topp:range><topp:section>34</topp:section><topp:surveytype>A</topp:surveytype><topp:aliquot>nene;</topp:aliquot><topp:serialnumb>WYB   0017060A</topp:serialnumb><topp:adminagenc>BUREAU OF LAND MGMT</topp:adminagenc><topp:price>0.0</topp:price><topp:acres>190.0</topp:acres><topp:dispositio>Authorized</topp:dispositio><topp:casetypeco>310781</topp:casetypeco><topp:casetype>O&amp;g renewal lease - pd</topp:casetype><topp:commodity>Oil &amp; gas</topp:commodity><topp:expireyear>0</topp:expireyear><topp:effectdate>8/14/1929</topp:effectdate><topp:royaltyrt>Rlty rate - 5%</topp:royaltyrt><topp:hbp>HBP</topp:hbp><topp:or>OR</topp:or><topp:name1>TEXACO EXPL &amp; PROD INC</topp:name1><topp:perint1>100.0</topp:perint1><topp:perint2>0.0</topp:perint2><topp:perint3>0.0</topp:perint3><topp:perint4>0.0</topp:perint4><topp:updatedate>6/1/2006</topp:updatedate><topp:the_geom><gml:MultiPolygon><gml:polygonMember><gml:Polygon><gml:exterior><gml:LinearRing><gml:posList>-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</gml:posList></gml:LinearRing></gml:exterior></gml:Polygon></gml:polygonMember></gml:MultiPolygon></topp:the_geom></topp:leases></gml:featureMembers></wfs:FeatureCollection> 
  • lib/OpenLayers.js

    old new  
    159159            "OpenLayers/Layer/Vector.js", 
    160160            "OpenLayers/Layer/GML.js", 
    161161            "OpenLayers/Format.js", 
     162            "OpenLayers/Format/XML.js", 
    162163            "OpenLayers/Format/GML.js", 
    163164            "OpenLayers/Format/KML.js", 
    164165            "OpenLayers/Format/GeoRSS.js", 
  • lib/OpenLayers/Format/XML.js

    old new  
     1/* Copyright (c) 2006 MetaCarta, Inc., published under a modified BSD license. 
     2 * See http://svn.openlayers.org/trunk/openlayers/repository-license.txt  
     3 * for the full text of the license. */ 
     4 
     5/** 
     6 * @requires OpenLayers/Format.js 
     7 * 
     8 * Class: OpenLayers.Format.XML 
     9 * Read and write XML.  
     10 * 
     11 * Inherits from: 
     12 *  - <OpenLayers.Format> 
     13 */ 
     14OpenLayers.Format.XML = OpenLayers.Class.create(); 
     15OpenLayers.Format.XML.prototype =  
     16  OpenLayers.Class.inherit(OpenLayers.Format, { 
     17     
     18    /** 
     19     * Property: xmldom 
     20     * {XMLDom} 
     21     */ 
     22    xmldom: null, 
     23 
     24    /** 
     25     * Constructor: OpenLayers.Format.XML 
     26     * Construct an XML parser.  The parser is used to read and write XML. 
     27     * Reading XML from a string returns a DOM element.  Writing XML from 
     28     * a DOM element returns a string. 
     29     * 
     30     * Parameters: 
     31     * options - {Object} Any optional parameters for this or the parent 
     32     *                         class. 
     33     */ 
     34    initialize: function(options) { 
     35        if(window.ActiveXObject) { 
     36            this.xmldom = new ActiveXObject("Microsoft.XMLDOM"); 
     37        } 
     38        OpenLayers.Format.prototype.initialize.apply(this, [options]); 
     39    }, 
     40 
     41    /** 
     42     * APIMethod: read 
     43     * Deserialize a XML string and return a DOM node. 
     44     * 
     45     * Parameters: 
     46     * text - {String} A XML string 
     47      
     48     * Returns: 
     49     * {XMLElement} A DOM node 
     50     */ 
     51    read: function(text) { 
     52        var index = text.indexOf('<'); 
     53        if(index > 0) { 
     54            text = text.substring(index); 
     55        } 
     56        var node = OpenLayers.Util.Try( 
     57            function() { 
     58                var xmldom; 
     59                // since we want to be able to call this method on the prototype 
     60                // itself, this.xmldom may not exist even if in IE 
     61                if(window.ActiveXObject && !this.xmldom) { 
     62                    xmldom = new ActiveXObject("Microsoft.XMLDOM"); 
     63                } else { 
     64                    xmldom = this.xmldom; 
     65                } 
     66                xmldom.loadXML(text); 
     67                return xmldom; 
     68            }, 
     69            function() { 
     70                return new DOMParser().parseFromString(text, 'text/xml'); 
     71            }, 
     72            function() { 
     73                var req = new XMLHttpRequest(); 
     74                req.open("GET", "data:" + "text/xml" + 
     75                         ";charset=utf-8," + encodeURIComponent(text), false); 
     76                if(req.overrideMimeType) { 
     77                    req.overrideMimeType("text/xml"); 
     78                } 
     79                req.send(null); 
     80                return req.responseXML; 
     81            } 
     82        ); 
     83        return node; 
     84    }, 
     85 
     86    /** 
     87     * APIMethod: write 
     88     * Serialize a DOM node into a XML string. 
     89     *  
     90     * Parameters: 
     91     * node - {Element} A DOM node 
     92     * 
     93     * Returns: 
     94     * {String} The XML string representation of the input node. 
     95     */ 
     96    write: function(node) { 
     97        var data; 
     98        if(this.xmldom) { 
     99            data = node.xml; 
     100        } else { 
     101            var serializer = new XMLSerializer(); 
     102            data = serializer.serializeToString(node); 
     103        } 
     104        return data; 
     105    }, 
     106 
     107    /** 
     108     * APIMethod: createElementNS 
     109     * 
     110     * Parameters: 
     111     * uri - {String} Namespace URI for the element 
     112     * name - {String} The prefixed name of the element (prefix:name) 
     113     *  
     114     * Returns: 
     115     * {Element} A DOM element with namespace. 
     116     */ 
     117    createElementNS: function(uri, name) { 
     118        var element; 
     119        if(this.xmldom) { 
     120            element = this.xmldom.createNode(1, name, uri); 
     121        } else { 
     122            element = document.createElementNS(uri, name); 
     123        } 
     124        return element; 
     125    }, 
     126 
     127    /** 
     128     * APIMethod: createTextNode 
     129     *  
     130     * Parameters: 
     131     * text - {String}  
     132     *  
     133     * Returns:  
     134     * {Element} A DOM text node 
     135     */ 
     136    createTextNode: function(text) { 
     137        var node; 
     138        if(this.xmldom) { 
     139            node = this.xmldom.createTextNode(text); 
     140        } else { 
     141            node = document.createTextNode(text); 
     142        } 
     143        return node; 
     144    }, 
     145 
     146    /** 
     147     * APIMethod: getElementsByTagNameNS 
     148     * Get a list of elements on a node given the namespace URI and local name. 
     149     *  
     150     * Parameters: 
     151     * node - {Element} 
     152     * uri - {String} Namespace URI 
     153     * name - {String} Local name of the tag 
     154     *  
     155     * Returns: 
     156     * {NodeList} A node list or array of elements. 
     157     */ 
     158    getElementsByTagNameNS: function(node, uri, name) { 
     159        var elements = []; 
     160        if(node.getElementsByTagNameNS) { 
     161            elements = node.getElementsByTagNameNS(uri, name); 
     162        } else { 
     163            var allNodes = node.getElementsByTagName("*"); 
     164            var potentialNode, fullName; 
     165            for(var i=0; i<allNodes.length; ++i) { 
     166                potentialNode = allNodes[i]; 
     167                fullName = (potentialNode.prefix) ? 
     168                           (potentialNode.prefix + ":" + name) : name; 
     169                if((name == "*") || (fullName == potentialNode.nodeName)) { 
     170                    if((uri == "*") || (uri == potentialNode.namespaceURI)) { 
     171                        elements.push(potentialNode); 
     172                    } 
     173                } 
     174            } 
     175        } 
     176        return elements; 
     177    }, 
     178 
     179    /** 
     180     * APIMethod: getAttributeNodeNS 
     181     * Get an attribute node given the namespace URI and local name. 
     182     *  
     183     * Parameters: 
     184     * node - {Element} 
     185     * uri - {String} Namespace URI 
     186     * name - {String} Local name of the attribute 
     187     *  
     188     * Returns: 
     189     * {DOMElement} An attribute node or null if none found. 
     190     */ 
     191    getAttributeNodeNS: function(node, uri, name) { 
     192        var attributeNode = null; 
     193        if(node.getAttributeNodeNS) { 
     194            attributeNode = node.getAttributeNodeNS(uri, name); 
     195        } else { 
     196            var attributes = node.attributes; 
     197            var potentialNode, fullName; 
     198            for(var i=0; i<attributes.length; ++i) { 
     199                potentialNode = attributes[i]; 
     200                if(potentialNode.namespaceURI == uri) { 
     201                    fullName = (potentialNode.prefix) ? 
     202                               (potentialNode.prefix + ":" + name) : name; 
     203                    if(fullName == potentialNode.nodeName) { 
     204                        attributeNode = potentialNode; 
     205                        break; 
     206                    } 
     207                } 
     208            } 
     209        } 
     210        return attributeNode; 
     211    }, 
     212 
     213    /** 
     214     * APIMethod: getAttributeNS 
     215     * Get an attribute value given the namespace URI and local name. 
     216     *  
     217     * Parameters: 
     218     * node - {Element} 
     219     * uri - {String} Namespace URI 
     220     * name - {String} Local name of the attribute 
     221     *  
     222     * Returns: 
     223     * {String} An attribute value or and empty string if none found. 
     224     */ 
     225    getAttributeNS: function(node, uri, name) { 
     226        var attributeValue = ""; 
     227        if(node.getAttributeNS) { 
     228            attributeValue = node.getAttributeNS(uri, name); 
     229        } else { 
     230            var attributeNode = this.getAttributeNodeNS(node, uri, name); 
     231            if(attributeNode) { 
     232                attributeValue = attributeNode.nodeValue; 
     233            } 
     234        } 
     235        return attributeValue; 
     236    }, 
     237 
     238    /** 
     239     * APIMethod: hasAttributeNS 
     240     * Determine whether a node has a particular attribute matching the given 
     241     *     name and namespace. 
     242     *  
     243     * Parameters: 
     244     * node - {Element} 
     245     * uri - {String} Namespace URI 
     246     * name - {String} Local name of the attribute 
     247     *  
     248     * Returns: 
     249     * {Boolean} The node has an attribute matching the name and namespace. 
     250     */ 
     251    hasAttributeNS: function(node, uri, name) { 
     252        var found = false; 
     253        if(node.hasAttributeNS) { 
     254            found = node.hasAttributeNS(uri, name); 
     255        } else { 
     256            found = !!this.getAttributeNodeNS(node, uri, name); 
     257        } 
     258        return found; 
     259    }, 
     260 
     261    CLASS_NAME: "OpenLayers.Format.XML"  
     262 
     263});      
  • tests/Format/test_XML.html

    old new  
     1<html>  
     2<head>  
     3    <script src="../../lib/OpenLayers.js"></script>  
     4    <script type="text/javascript"><!--  
     5    var text = 
     6        '<?xml version="1.0"?>' +  
     7        '<ol:root xmlns="http://namespace.default.net" ' + 
     8                 'xmlns:ol="http://namespace.openlayers.org" ' +  
     9                 'xmlns:ta="http://namespace.testattribute.net">' + 
     10            '<ol:child ta:attribute="value1" ' + 
     11                      'attribute="value2">' + 
     12                'junk1' + 
     13            '<' + '/ol:child>' + 
     14            '<ol:child>junk2<' + '/ol:child>' + 
     15            '<ol:child>junk3<' + '/ol:child>' + 
     16            '<element>junk4<' + '/element>' +  
     17            '<ol:element>junk5<' + '/ol:element>' +  
     18        '<' + '/ol:root>'; 
     19 
     20    function test_Format_XML_constructor(t) {  
     21        t.plan(4);  
     22          
     23        var options = {'foo': 'bar'};  
     24        var format = new OpenLayers.Format.XML(options);  
     25        t.ok(format instanceof OpenLayers.Format.XML,  
     26             "new OpenLayers.Format.XML returns object" );  
     27        t.eq(format.foo, "bar", "constructor sets options correctly");  
     28        t.eq(typeof format.read, "function", "format has a read function");  
     29        t.eq(typeof format.write, "function", "format has a write function");  
     30    } 
     31     
     32    function test_Format_XML_read(t) { 
     33        t.plan(5); 
     34         
     35        var format = new OpenLayers.Format.XML(); 
     36        var doc = format.read(text); 
     37        t.eq(doc.nodeType, 9, 
     38             "doc has the correct node type"); 
     39        t.eq(doc.nodeName, "#document", 
     40             "doc has the correct node name"); 
     41        t.ok(doc.documentElement, 
     42             "ok to access doc.documentElement"); 
     43        t.eq(doc.documentElement.nodeName, "ol:root", 
     44             "doc root has the correct node name"); 
     45        t.eq(doc.documentElement.childNodes[1].firstChild.nodeValue, "junk2", 
     46             "second child of doc root has correct child node"); 
     47    } 
     48     
     49    function test_Format_XML_write(t) { 
     50        t.plan(1); 
     51        var format = new OpenLayers.Format.XML(); 
     52        var doc = format.read(text); 
     53        var out = format.write(doc); 
     54        out = out.replace(/[\r\n]/g, ''); 
     55        t.eq(text, out, 
     56             "correctly writes an XML DOM doc"); 
     57    } 
     58     
     59    function test_Format_XML_createElementNS(t) { 
     60        t.plan(5); 
     61        var format = new OpenLayers.Format.XML(); 
     62        var uri = "http://foo.com"; 
     63        var prefix = "foo"; 
     64        var localName = "bar"; 
     65        var qualifiedName = prefix + ":" + name; 
     66        var node = format.createElementNS(uri, qualifiedName); 
     67        t.eq(node.nodeType, 1, 
     68             "node has correct type"); 
     69        t.eq(node.nodeName, qualifiedName, 
     70             "node has correct qualified name"); 
     71        t.eq(node.prefix, prefix, 
     72             "node has correct prefix"); 
     73        t.eq(node.namespaceURI, uri, 
     74             "node has correct namespace uri"); 
     75         
     76        var doc = format.read(text); 
     77        t.ok(doc.documentElement.appendChild(node), 
     78             "node can be appended to a doc root"); 
     79    } 
     80     
     81    function test_Format_XML_createTextNode(t) { 
     82        t.plan(4); 
     83        var format = new OpenLayers.Format.XML(); 
     84        var value = Math.random().toString(); 
     85        var node = format.createTextNode(value); 
     86        t.eq(node.nodeType, 3, 
     87             "node has correct type"); 
     88        t.eq(node.nodeName, "#text", 
     89             "node has correct name"); 
     90        t.eq(node.nodeValue, value, 
     91             "node has correct value"); 
     92         
     93        var doc = format.read(text); 
     94        t.ok(doc.documentElement.appendChild(node), 
     95             "node can be appended to a doc root"); 
     96    } 
     97     
     98    function test_Format_XML_getElementsByTagNameNS(t) { 
     99        t.plan(3); 
     100 
     101        var format = new OpenLayers.Format.XML(); 
     102        var olUri = "http://namespace.openlayers.org"; 
     103        var name = "child"; 
     104        var doc = format.read(text); 
     105        var nodes = format.getElementsByTagNameNS(doc.documentElement, 
     106                                                  olUri, name); 
     107        t.eq(nodes.length, 3, 
     108             "gets correct number of nodes"); 
     109        var qualifiedName = nodes[0].prefix + ":" + name; 
     110        t.eq(nodes[0].nodeName, qualifiedName, 
     111             "first node has correct qualified name"); 
     112         
     113        var defaultUri = "http://namespace.default.net"; 
     114        name = "element"; 
     115        nodes = format.getElementsByTagNameNS(doc.documentElement, 
     116                                                  defaultUri, name); 
     117        t.eq(nodes.length, 1, 
     118             "gets correct number of nodes in default namespace"); 
     119         
     120    } 
     121     
     122    function test_Format_XML_getAttributeNodeNS(t) { 
     123        t.plan(5); 
     124        var format = new OpenLayers.Format.XML(); 
     125        var doc = format.read(text); 
     126        var olUri = "http://namespace.openlayers.org"; 
     127        var taUri = "http://namespace.testattribute.net"; 
     128        var localNodeName = "child"; 
     129        var localAttrName = "attribute"; 
     130        var nodes = format.getElementsByTagNameNS(doc.documentElement, 
     131                                                  olUri, localNodeName); 
     132        var attributeNode = format.getAttributeNodeNS(nodes[0], 
     133                                                      taUri, localAttrName); 
     134        var qualifiedName = attributeNode.prefix + ":" + localAttrName; 
     135 
     136        t.ok(attributeNode, 
     137             "returns non-null value"); 
     138        t.eq(attributeNode.nodeType, 2, 
     139             "attribute node has correct type"); 
     140        t.eq(attributeNode.nodeName, qualifiedName, 
     141             "attribute node has correct qualified name"); 
     142        t.eq(attributeNode.nodeValue, "value1", 
     143             "attribute node has correct value"); 
     144         
     145        var nullAttribute = format.getAttributeNodeNS(nodes[0], 
     146                                                      taUri, "nothing"); 
     147        t.ok(nullAttribute === null, 
     148             "returns null for nonexistent attribute"); 
     149    } 
     150     
     151    function test_Format_XML_getAttributeNS(t) { 
     152        t.plan(2); 
     153        var format = new OpenLayers.Format.XML(); 
     154        var doc = format.read(text); 
     155        var olUri = "http://namespace.openlayers.org"; 
     156        var taUri = "http://namespace.testattribute.net"; 
     157        var localNodeName = "child"; 
     158        var localAttrName = "attribute"; 
     159        var nodes = format.getElementsByTagNameNS(doc.documentElement, 
     160                                                  olUri, localNodeName); 
     161        var attributeValue = format.getAttributeNS(nodes[0], 
     162                                                   taUri, localAttrName); 
     163        t.eq(attributeValue, "value1", 
     164             "got correct attribute value"); 
     165         
     166        var emptyValue = format.getAttributeNS(nodes[0], 
     167                                              taUri, "nothing"); 
     168        t.ok(emptyValue === "", 
     169             "returns empty string for nonexistent attributes"); 
     170    } 
     171     
     172    function test_Format_XML_hasAttributeNS(t) { 
     173        t.plan(2); 
     174        var format = new OpenLayers.Format.XML(); 
     175        var doc = format.read(text); 
     176        var olUri = "http://namespace.openlayers.org"; 
     177        var taUri = "http://namespace.testattribute.net"; 
     178        var localNodeName = "child"; 
     179        var localAttrName = "attribute"; 
     180        var nodes = format.getElementsByTagNameNS(doc.documentElement, 
     181                                                  olUri, localNodeName); 
     182        var found = format.hasAttributeNS(nodes[0], taUri, localAttrName); 
     183        t.ok(found === true, "returns true for good attribute"); 
     184         
     185        found = format.hasAttributeNS(nodes[0], taUri, "nothing"); 
     186        t.ok(found === false, "returns false for bad attribute"); 
     187    } 
     188     
     189    // -->  
     190    </script>  
     191</head>  
     192<body>  
     193</body>  
     194</html>  
  • tests/list-tests.html

    old new  
    2020    <li>Geometry/test_Rectangle.html</li> 
    2121    <li>Geometry/test_Surface.html</li> 
    2222    <li>test_Format.html</li> 
     23    <li>Format/test_XML.html</li> 
    2324    <li>Format/test_GeoRSS.html</li> 
    2425    <li>Format/test_GML.html</li> 
    2526    <li>Format/test_WKT.html</li>