Files
openlayers/examples/xml.html
crschmidt 7da6a3540e Merge the excellent documentation work done during foss4g into trunk. Many
thanks to all the contributors who helped put this together. 
I'm not exactly sure of what's going to happen with this, but for now,
at http://openlayers.org/dev/doc/examples.html you can see links to all the
examples *with descriptions*. Hooray!


git-svn-id: http://svn.openlayers.org/trunk/openlayers@5362 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
2007-12-08 14:21:53 +00:00

158 lines
6.2 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>XML Parsing Example</title>
<style type="text/css">
body {
margin: 0 2em;
font-family: sans-serif;
}
#output {
font-family: monospace;
background-color: #efefef;
font-size: 0.9em;
padding: 1em;
}
span.code {
font-family: monospace;
background-color: #efefef;
font-size: 0.9em;
padding: 0.25em;
line-height: 1.5em;
}
ul {
margin: 0;
padding: 0 0 1em 1.5em;
}
ul li {
padding-left: 0;
}
</style>
<script src="../lib/Firebug/firebug.js" type="text/javascript"></script>
<script src="../lib/OpenLayers.js" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
var format = new OpenLayers.Format.XML();
var doc = null;
function init() {
var url = "xml/features.xml";
OpenLayers.loadURL(url, null, null, loadSuccess, loadFailure);
}
function loadSuccess(request) {
updateStatus("loaded");
if(!request.responseXML.documentElement) {
doc = format.read(request.responseText);
} else {
doc = request.responseXML;
}
}
function loadFailure(request) {
updateStatus("failed to load");
}
function updateStatus(msg) {
document.getElementById("loadStatus").firstChild.nodeValue = msg;
}
function updateOutput(text) {
document.getElementById("output").firstChild.nodeValue = text;
}
function write() {
var text = format.write(doc);
updateOutput(text);
}
function getElementsByTagNameNS(node, uri, name) {
var nodes = format.getElementsByTagNameNS(node, uri, name);
var pieces = [];
for(var i=0; i<nodes.length; ++i) {
pieces.push(format.write(nodes[i]));
}
updateOutput(pieces.join(' '));
}
function hasAttributeNS(node, uri, name) {
updateOutput(format.hasAttributeNS(node, uri, name))
}
function getAttributeNodeNS(node, uri, name) {
var attributeNode = format.getAttributeNodeNS(node, uri, name);
updateOutput(attributeNode.nodeName + ' = "' +
attributeNode.nodeValue + '"');
}
function getAttributeNS(node, uri, name) {
var attributeValue = format.getAttributeNS(node, uri, name);
updateOutput('"' + attributeValue + '"')
}
function createElementNS(uri, name) {
var node = format.createElementNS(uri, name);
doc.documentElement.appendChild(node);
write();
}
function createTextNode(text) {
var node = format.createTextNode(text);
doc.documentElement.appendChild(node);
write();
}
window.onload = init;
//]]>
</script>
</head>
<body>
<h1 id="title">XML Format Example</h1>
<div id="tags">
</div>
<p id="shortdesc">
Shows the use of the OpenLayers XML format class
</p>
<div id="docs">
<p>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.</p>
<p>This page loads an XML document and demonstrates a few of the methods
available in the parser.</p>
<p>Status: <b>XML document <span id="loadStatus">loading..</span>.</b></p>
<p>After the XML document loads, see the result of a few of the methods
below. Assume that you start with the following code:
<br />
<span class="code">
var format = new OpenLayers.Format.XML();
</span>
</p>
Sample methods
<ul>
<li><a href="javascript:void write();">format.write()</a> - write the XML doc as text</li>
<li><a href="javascript:void getElementsByTagNameNS(doc, 'http://www.opengis.net/gml', 'MultiPolygon');">format.getElementsByTagNameNS()</a> - get all gml:MultiPolygon</li>
<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>
<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>
<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>
<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>
<li><a href="javascript:void createTextNode('test text ');">format.createTextNode()</a> - create a text node (and append it to the doc)</li>
</ul>
Output:
<div id="output">&nbsp;</div>
</div>
</body>
</html>