XML Template

Piero Cavalieri piero.cavalieri at HEIDI.IT
Fri Nov 4 01:24:14 PST 2005


Some months ago I did the same (.NET + MapServer CGI) and at the end used
an xml file for templates (only for browse mode). You can do the job
without using xml files (you can use a fixed structure of your choice and
maybe this is also faster). However if you want to use xml files, be sure
MapServer doesn't put info that goes before the template's xml document
declaration (4.4 did that...).
My code fragment was so:

private void generateImgXml (string parameters) {
   // Create a 'WebRequest' with the specified url.
   queryString = baseUrl + parameters;
   if(debug) Response.Write("<br>" + queryString);
   HttpWebRequest webRequest = (HttpWebRequest)
WebRequest.Create(queryString);
   webRequest.Method = "GET";
   // Send the WebRequest and read response
   HttpWebResponse webResponse = (HttpWebResponse)
webRequest.GetResponse();
   Stream sResponse = webResponse.GetResponseStream();
   StreamReader reader = new StreamReader(sResponse);
   //first line is additional info from MapServer
   string header = reader.ReadLine();
   //actual parameters
   XmlDocument xmldoc = new XmlDocument();
   string doc = reader.ReadToEnd();
   xmldoc.LoadXml(doc);
   xmldoc.PreserveWhitespace = true;
   XmlNode root = xmldoc.DocumentElement;
   img = root.SelectSingleNode("img").Attributes
["value"].Value;
   mapwidth = root.SelectSingleNode
("mapwidth").Attributes["value"].Value;
   mapheight = root.SelectSingleNode
("mapheight").Attributes["value"].Value;
   scalebar = root.SelectSingleNode
("scalebar").Attributes["value"].Value;
   imgxy = root.SelectSingleNode("imgxy").Attributes
["value"].Value;
   imgext = root.SelectSingleNode("imgext").Attributes
["value"].Value;
   referenceMap = root.SelectSingleNode
("referenceMap").Attributes["value"].Value;
   legend = root.SelectSingleNode("legend").InnerText;
   sResponse.Close();
   reader.Close();
   webResponse.Close();
  }

Template file was so:

<?xml version="1.0" encoding="utf-8" ?>
<map>
 <img value="[img]"/>
 <mapwidth value="[mapwidth]"/>
 <mapheight value="[mapheight]"/>
 <scalebar value="[scalebar]"/>
 <referenceMap value="[ref]"/>
 <imgxy value="[center]"/>
 <imgext value="[mapext]"/>
 <legend>
  <![CDATA[
  [legend]
  ]]>
 </legend>
</map>

As you see, when reading xml file, I discard first line (which invalidate
the xml doc).

Ok?
  Cheers
      P.



Mike Saunt ha scritto:

> Tricia
>
> We have just finished implementing a system like this to get CGI version
talking with .NET - simple but very powerful especially with the template
replacement options introduced in v4 for the CGI URL parameters.
>
> In essence we take the output into a XML template that is built in a
fashion to hold all possible output parameters from each execution of the
CGI (mode=browse) - this is at my office (it is mdnight here in the UK) but
I will try to remember to send to you tomorrow.  We simply load this
in .NET and and have an 'state' variable holding this between execution.
>
> With mode=query (or nquery) we build XML templates per layer.  These are
then streamed back from an execution and then are loaded into a .NET
datatable - we can then easily manipulate, sort, delete records etc.  The
format we've used is as follows:
>
> <row>
>   <fieldname1>[fieldname1]</fieldname1>
>   <fieldname2>[fieldname2]</fieldname2>
>   <fieldname3>[fieldname3]</fieldname3>
> </row>
>
> When MapServer parses this file it replaces the [fieldname1] values with
the attribute information.
>
> There may be a few on the list who wonder why we have gone about it this
way.  We have an existing .NET web application that runs on large vendor
software and needed to look at addressing some needs for our customers who
need more flexible environment.  We didn't opt for MapScript in c# at
present as we investigated and it appeared to have some memory issues.  The
great thing with MapServer CGI is that it is totally stateless, executes in
fractions of a second (we access over 1 GB of MapInfo files with no
problem) and in some cases is actually faster than the vendor software.
>
> If anyone wants any further information or ode then just let us know
>
> Cheers
> mike
>
> On 03/11/05, Tricia Williams <pgwillia at student.cs.uwaterloo.ca> wrote:
>
>     Hi List,
>
>        I was wondering if it is possible to use an xml template instead
of a
>     html template to handle nquery results?  I have tried experimenting
but
>     the results are not recognized as xml.
>
>        My header contains:
>
>     <?xml version="1.0" encoding="UTF-8"?>
>     <result>
>
>        My template contains:
>
>     <place>
>       ...
>     </place>
>
>        My footer contains:
>
>     </results>
>
>        Any advice or guidance would be much appreciated.
>
>     Cheers,
>     Tricia
>



More information about the MapServer-users mailing list