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">
5 <title>XML Parsing Example</title>
6 <link rel="stylesheet" href="../theme/default/style.css" type="text/css" />
7 <link rel="stylesheet" href="style.css" type="text/css" />
8 <style type="text/css">
10 font-family: monospace;
11 background-color: #efefef;
16 font-family: monospace;
17 background-color: #efefef;
24 padding: 0 0 1em 1.5em;
31 <script src="../lib/Firebug/firebug.js" type="text/javascript"></script>
32 <script src="../lib/OpenLayers.js" type="text/javascript"></script>
33 <script type="text/javascript">
36 var format = new OpenLayers.Format.XML();
40 var url = "xml/features.xml";
41 OpenLayers.loadURL(url, null, null, loadSuccess, loadFailure);
44 function loadSuccess(request) {
45 updateStatus("loaded");
46 if(!request.responseXML.documentElement) {
47 doc = format.read(request.responseText);
49 doc = request.responseXML;
53 function loadFailure(request) {
54 updateStatus("failed to load");
57 function updateStatus(msg) {
58 document.getElementById("loadStatus").firstChild.nodeValue = msg;
61 function updateOutput(text) {
62 document.getElementById("output").firstChild.nodeValue = text;
66 var text = format.write(doc);
70 function getElementsByTagNameNS(node, uri, name) {
71 var nodes = format.getElementsByTagNameNS(node, uri, name);
73 for(var i=0; i<nodes.length; ++i) {
74 pieces.push(format.write(nodes[i]));
76 updateOutput(pieces.join(' '));
79 function hasAttributeNS(node, uri, name) {
80 updateOutput(format.hasAttributeNS(node, uri, name))
83 function getAttributeNodeNS(node, uri, name) {
84 var attributeNode = format.getAttributeNodeNS(node, uri, name);
85 updateOutput(attributeNode.nodeName + ' = "' +
86 attributeNode.nodeValue + '"');
89 function getAttributeNS(node, uri, name) {
90 var attributeValue = format.getAttributeNS(node, uri, name);
91 updateOutput('"' + attributeValue + '"')
94 function createElementNS(uri, name) {
95 var node = format.createElementNS(uri, name);
96 doc.documentElement.appendChild(node);
100 function createTextNode(text) {
101 var node = format.createTextNode(text);
102 doc.documentElement.appendChild(node);
106 window.onload = init;
112 <h1 id="title">XML Format Example</h1>
118 Shows the use of the OpenLayers XML format class
122 <p>OpenLayers has a very simple XML format class (OpenLayers.Format.XML)
123 that can be used to read/write XML docs. The methods available on the
124 XML format (or parser if you like) allow for reading and writing of the
125 various XML flavors used by the library - in particular the vector data
126 formats. It is by no means intended to be a full-fledged XML toolset.
127 Additional methods will be added only as needed elsewhere in the
129 <p>This page loads an XML document and demonstrates a few of the methods
130 available in the parser.</p>
131 <p>Status: <b>XML document <span id="loadStatus">loading..</span>.</b></p>
132 <p>After the XML document loads, see the result of a few of the methods
133 below. Assume that you start with the following code:
136 var format = new OpenLayers.Format.XML();
141 <li><a href="javascript:void write();">format.write()</a> - write the XML doc as text</li>
142 <li><a href="javascript:void getElementsByTagNameNS(doc, 'http://www.opengis.net/gml', 'MultiPolygon');">format.getElementsByTagNameNS()</a> - get all gml:MultiPolygon</li>
143 <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>
144 <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>
145 <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>
146 <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>
147 <li><a href="javascript:void createTextNode('test text ');">format.createTextNode()</a> - create a text node (and append it to the doc)</li>
150 <div id="output"> </div>