Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically build HTML in VB advice.......

Status
Not open for further replies.

towser

Programmer
Feb 19, 2001
29
GB
I have to develop a VB Component that reads through a defined XML document and then dynamically build HTML or ASP code which will be splatted into an existing template on the web server. The Component will sit on an APP server so I don't think it will have the IIS capabilities to use the webclass object.

I haven't got a problem with parsing the XML or displaying the html code in a browser from VB.

My question actually relates to physically building up the html code within VB. I'm finding it very long winded having to build all the tags up as I loop through the XML tags.

So I was interested if anyone knows of a easier method of coding this please ?

Cheers and apologies if this is in the incorrect section.

 
What sort of control do you require over the process? A good deal depends on what the XML contains and what sort of HTML presentation you want to generate from it.

Simple tag-mapping might be done by loading up a Collection or Scripting.Dictionary to start with.

You might also look into XSLT processing via something like MSXML 4.0 Using ASP with XSLT Processor and Template Objects, most of which applies nicely to VB programming as well.
 
Thanks.

The idea is to create a very basic html output, the layout and style will then be controlled using specific Cascading Style Sheets.

The XML will basically contain a load of tags either containing text or field names and then the attributes for those fields i.e maxlength, font class and validation rules.

Could you give a me a quick example of what would be in the collection or Scripting.Dictionary please ? (not the actual code just a brief written description)

Cheers
 
That was a quick thought I tossed off.

I was thinking that any XML tag that might map to an HTML <TABLE> would be in the collection as key=<the XML tag> value=<TABLE>, same for things mapping to <TR>s and <TD>s.

Then have a loop that read the XML as a stream, breaking out the tags and doing basically [tt]Write colTagMap.Item(strXMLTag)[/tt] for tags, and just writing the data between tags as straight text. Like:
[tt]
While Not EOF(1)
strXML = ParseTagOrText(1)
If IsATag(strXML) Then
Print #2, colTagMap.Item(strXML);
Else
Print #2, strXML;
End If
Loop
[/tt]
Where ParseTagOrText() gets the input text and chunks off tags or text-between-tags and IsATag() might even be as trivial as a test of the first char of the parameter = "<" or something.

Sorry for the rough code there, but I realized the text I wrote was vague. And yes, this is simplistic.
 
I should elaborate somewhat:

ParseTagOrText() would need to parse attributes properly, and most likely return a UDT, Collection, or a custom Class instance or something and not a simple String. Then the logic for "writing out" the HTML tags and text would need to be smarter about processing attributes, etc.

The "mapping" Collection itself would probably not be especially useful, so you might need to return a "tag ID" value you could do a Select Case on to execute code to emit the HTML you want for each type of tag.

By the time you are done you might want to look at XSLT processing components instead of rolling your own this way.
 
Thanks dilettante, that is usefull. I was kinda hoping there would be a method that wouldn't involve that type of manual translation of the XML tags, but I guess it was wishfull thinking.

I did look at the XSLT but that needed to be rigidly fixed to the XML format coming in and I haven't got that luxury.
 
dilettante, I'm not sure you should use the phrase "I tossed off" in future posts.


 
An excellent point towser. Sort of makes the suggestion look even worse than it was.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top