[OpenLayers-Users] XML Parsing via OpenLayers?
Tim Schaub
tschaub at opengeo.org
Fri Oct 10 21:01:53 EDT 2008
Hey-
Maps-R-us wrote:
> Thanks Bart,
>
> I will give that a try. Openlayers sure requires a lot more code than the
> Google Maps API XML parser!
This is not going to make it look any easier, but if you want
inspiration, you could see the XML format tests for some parsing of
arbitrary XML.
http://trac.openlayers.org/browser/trunk/openlayers/tests/Format/XML.html#L330
And, if Maps-R-You, you shouldn't have any reason to complain about a
little code writing :).
More seriously, is there any reason you can't use a standardized format?
Tim
>
> Cheers,
>
> Mark
>
>
>
> Bart van den Eijnden wrote:
>> Maybe you can try and create your own Format class, something like:
>>
>> /**
>> * @requires OpenLayers/Format/XML.js
>> *
>> */
>> OpenLayers.Format.MyFormat = OpenLayers.Class(
>> OpenLayers.Format.XML, {
>>
>> initialize: function(options) {
>> OpenLayers.Format.XML.prototype.initialize.apply(this, [options]);
>> this.options = options;
>> },
>>
>> read: function(data) {
>> if(typeof data == "string") {
>> data = OpenLayers.Format.XML.prototype.read.apply(this,
>> [data]);
>> }
>> // do your stuff here, it's just an example
>> var root = data.documentElement;
>> var children = root.childNodes;
>> for(var i = 0; i < children.length; ++i) {
>> childNode = children[i];
>> var nodeName = childNode.nodeName;
>> if (nodeName == 'Toolbar') {
>> this.parseToolbar(childNode);
>> }
>> }
>> // return your array here
>> },
>>
>> CLASS_NAME: "OpenLayers.Format.MyFormat"
>>
>> });
>>
>>
>> And then:
>>
>> OpenLayers.loadURL(myURL, "", null, function(response) {
>> var format = new OpenLayers.Format.MyFormat();
>> var myArray = format.read(response.responseXML ||
>> response.responseText);
>> console.log(myArray);
>> });
>>
>> Best regards,
>> Bart
>>
>> Maps-R-us wrote:
>>> Thank you Bart,
>>>
>>> I actually do this in my loadSuccess function:
>>>
>>> function loadSuccess(request) {
>>>
>>> updateStatus("loaded");
>>> if(!request.responseXML.documentElement) {
>>> doc = format.read(request.responseText);
>>> } else {
>>> doc = request.responseXML;
>>> }
>>> }
>>>
>>> Is this correct?
>>>
>>> M
>>>
>>> Bart van den Eijnden wrote:
>>>
>>>> You need to parse the XML in the loadSuccess function, check
>>>> examples/GMLParser.html for an example.
>>>>
>>>> Best regards,
>>>> Bart
>>>>
>>>> Maps-R-us wrote:
>>>>
>>>>> Hi there,
>>>>>
>>>>> I would like to retrieve elements in an xml file based on tag name and
>>>>> populate an array. Here is a sample of the XML:
>>>>>
>>>>> <users>
>>>>> <user>
>>>>> <fullname>Test</fullname>
>>>>> <ID>3400</ID>
>>>>> <lat>0.003926753994724706</lat>
>>>>> <lon>0.00446319580078125</lon>
>>>>>
>>>>> <image>http://40.media.vimeo.com/d1/5/35/85/81/portrait-35858134.jpg</image>
>>>>> <floor>3</floor>
>>>>> </user>
>>>>> </users>
>>>>>
>>>>> My code fails on the getElementsByNameNS()
>>>>>
>>>>>
>>>>> var format = new OpenLayers.Format.XML();
>>>>> var doc = null;
>>>>> var url = "officedata.xml";
>>>>> OpenLayers.loadURL(url, null, null, loadSuccess, loadFailure);
>>>>> var attributeValue = [];
>>>>> var attributeValue[1] = format.getElementsByTagNameNS(doc,
>>>>> '*','fullname');
>>>>> alert (attributeValue[1]);
>>>>>
>>>>> Any ideas?
>>>>>
>>>>> Thank you in advance,
>>>>>
>>>>> M
>>>>>
>>>>>
>>>>>
>>>> --
>>>> Bart van den Eijnden
>>>> OSGIS, Open Source GIS
>>>> bartvde at osgis.nl
>>>> http://www.osgis.nl
>>>>
>>>> _______________________________________________
>>>> Users mailing list
>>>> Users at openlayers.org
>>>> http://openlayers.org/mailman/listinfo/users
>>>>
>>>>
>>>>
>>>
>>
>> --
>> Bart van den Eijnden
>> OSGIS, Open Source GIS
>> bartvde at osgis.nl
>> http://www.osgis.nl
>>
>> _______________________________________________
>> Users mailing list
>> Users at openlayers.org
>> http://openlayers.org/mailman/listinfo/users
>>
>>
>
--
Tim Schaub
OpenGeo - http://opengeo.org
Expert service straight from the developers.
More information about the Users
mailing list