WelcomeServicesPortfolioKnowledge BaseContact Us
27 Seconds, Inc.


Knowledge base View Article

need hosting?

Need hosting? We use and love our host - CrystalTech.com!
Need hosting? We use and love our host - CrystalTech.com!

Text Disguise - CAPTCHA-image/Human Interactive Proof web services

National Holiday Dates and Bank Holiday Dates - web site that provides the dates of national and bank holidays for the United States, United Kingdom, Ireland and Scotland

Holiday Web Service - web services for programmers to provide the dates of national and bank holidays in their applications for the United States, United Kingdom, Ireland and Scotland

Our Family Heart - web site to help families communicate no matter where they are

What?! You haven't heard about the greatest remote controlled flyer to come out in years?! Checkout the FlyTech DragonFly now!

XML Factory Functions

Summary: Functions to do some common XML functionality. Adding nodes, removing nodes, starting a document, etc.
 
Keywords: DOM
MSXML
PARSE
XML
Applicable Software: Active Server Pages (ASP)
 
Body:

This code uses an outdated version of the XML Parser.

Const XMLFactory_DEBUG = False

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Function: XMLFactory_loadNewDocument(filePath As String) As DOMDocument
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function XMLFactory_loadNewDocument(filePath)
If XMLFactory_DEBUG Then Response.Write Server.HTMLEncode("XMLFactory_loadNewDocument(" & filePath & ")") & "<br><br>"

Dim m_oXMLDocument: Set m_oXMLDocument = Server.CreateObject("MSXML2.DOMDocument")
	m_oXMLDocument.async = False

	If Not fileExists(filePath) Then
		Set XMLFactory_loadNewDocument = Nothing
		Exit Function
	End If
	
	m_oXMLDocument.Load filePath
	
	Set XMLFactory_loadNewDocument = m_oXMLDocument

End Function

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Function: XMLFactory_newDocument(documentElementNames As String, documentElementText As String) As DOMDocument
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function XMLFactory_newDocument(documentElementName, documentElementText)
If XMLFactory_DEBUG Then Response.Write Server.HTMLEncode("XMLFactory_newDocument(" & documentElementName & ", " & documentElementText & ")") & "<br><br>"

Dim m_oXMLDocument: Set m_oXMLDocument = Server.CreateObject("MSXML2.DOMDocument")
    
    m_oXMLDocument.appendChild m_oXMLDocument.createNode(7, "xml", "")
    m_oXMLDocument.appendChild m_oXMLDocument.createNode(1, documentElementName, "")

	If IsNull(documentElementText) Then
		'do nothing
    ElseIf Len(documentElementText) > 0 Then 
		m_oXMLDocument.documentElement.Text = documentElementText
	End If
    
    Set XMLFactory_newDocument = m_oXMLDocument

End Function

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Function: XMLFactory_parseNewDocument(xmlString As String) As DOMDocument
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function XMLFactory_parseNewDocument(xmlString)
If XMLFactory_DEBUG Then Response.Write Server.HTMLEncode("XMLFactory_parseNewDocument(" & xmlString & ")") & "<br><br>"

Dim m_oXMLDocument: Set m_oXMLDocument = Server.CreateObject("MSXML2.DOMDocument")
    m_oXMLDocument.async = False
    m_oXMLDocument.loadXML xmlString
    
    Set XMLFactory_parseNewDocument = m_oXMLDocument
	
End Function

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Function: XMLFactory_receiveNewDocument(requestObject As RequestObject) As DOMDocument
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function XMLFactory_receiveNewDocument(requestObject)
If XMLFactory_DEBUG Then Response.Write Server.HTMLEncode("XMLFactory_receiveNewDocument(requestObject)") & "<br><br>"

Dim m_oXMLDocument: Set m_oXMLDocument = Server.CreateObject("MSXML2.DOMDocument")
    m_oXMLDocument.async = False
    m_oXMLDocument.Load requestObject
    
    Set XMLFactory_receiveNewDocument = m_oXMLDocument

End Function

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Function: XMLFactory_sendDocument(ByRef xmlDocument As DOMDocument, httpURL As String) As String
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function XMLFactory_sendDocument(ByRef xmlDocument, httpURL)
If XMLFactory_DEBUG Then Response.Write Server.HTMLEncode("XMLFactory_sendDocument(xmlDocument, " & httpURL & ")") & "<br><br>"
    
Dim m_oXMLHTTP: Set m_oXMLHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP")
    m_oXMLHTTP.Open "POST", httpURL, False
    m_oXMLHTTP.send xmlDocument
    
    XMLFactory_sendDocument = m_oXMLHTTP.responseText
    
    Set m_oXMLHTTP = Nothing
	
End Function

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Function: XMLFactory_transformDocument(ByRef xmlDocument As DOMDocument, xslFilePath As String) As String
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function XMLFactory_transformDocument(ByRef xmlDocument, xslFilePath)
If XMLFactory_DEBUG Then Response.Write Server.HTMLEncode("XMLFactory_transformDocument(xmlDocument, " & xslFilePath & ")") & "<br><br>"
    
Dim m_oXSLDocument: Set m_oXSLDocument = Server.CreateObject("MSXML2.DOMDocument")   
    m_oXSLDocument.async = False

	If Not fileExists(xslFilePath) Then
		XMLFactory_transformDocument = ""
		Exit Function
	End If
	
    m_oXSLDocument.Load xslFilePath
    
    XMLFactory_transformDocument = xmlDocument.transformNode(m_oXSLDocument)
        
End Function

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Function: XMLFactory_parseNewNode(xmlString As String) As XMLNode
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function XMLFactory_parseNewNode(xmlString)
If XMLFactory_DEBUG Then Response.Write Server.HTMLEncode("XMLFactory_parseNewNode(" & xmlString & ")") & "<br><br>"

Dim m_oXMLTempDocument: Set m_oXMLTempDocument = Server.CreateObject("MSXML2.DOMDocument")
    m_oXMLTempDocument.async = False
    m_oXMLTempDocument.loadXML xmlString
    
    Set XMLFactory_parseNewNode = m_oXMLTempDocument.documentElement

End Function

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Function: XMLFactory_newNode(nodeName As String, nodeText As String) As XMLNode
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function XMLFactory_newNode(nodeName, nodeText)
If XMLFactory_DEBUG Then Response.Write Server.HTMLEncode("XMLFactory_newNode(" & nodeName & ", " & nodeText & ")") & "<br><br>"

Dim m_oXMLTempDocument: Set m_oXMLTempDocument = Server.CreateObject("MSXML2.DOMDocument")
Dim m_oXMLNode
    Set m_oXMLNode = m_oXMLTempDocument.createNode(1, nodeName, "")
	
	If IsNull(nodeText) Then
		'do nothing
    ElseIf Len(nodeText) > 0 Then 
		m_oXMLNode.Text = nodeText
	End If
    
    Set XMLFactory_newNode = m_oXMLNode
	
Set m_oXMLTempDocument = Nothing

End Function

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Function: XMLFactory_newAttribute(attributeName As String, attributeText As String) As XMLAttribute
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function XMLFactory_newAttribute(attributeName, attributeText)
If XMLFactory_DEBUG Then Response.Write Server.HTMLEncode("XMLFactory_newAttribute(" & attributeName & ", " & attributeText & ")") & "<br><br>"

Dim m_oXMLTempDocument: Set m_oXMLTempDocument = Server.CreateObject("MSXML2.DOMDocument")
Dim m_oXMLAttribute
    
    Set m_oXMLAttribute = m_oXMLTempDocument.createNode(2, attributeName, "")
	
	If IsNull(attributeText) Then
		'do nothing
    ElseIf Len(attributeText) > 0 Then 
		m_oXMLAttribute.Text = attributeText
	End If
    
    Set XMLFactory_newAttribute = m_oXMLAttribute
	
Set m_oXMLTempDocument = Nothing

End Function

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Function: fileExists(filePath As String) As Boolean
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function fileExists(filePath)
Dim m_bOut
Dim m_oFS: Set m_oFS = Server.CreateObject("Scripting.FileSystemObject")
	m_bOut = m_oFS.FileExists(filePath)
Set m_oFS = Nothing

	fileExists = m_bOut

End Function

** Original code credit goes to Ben Sarsgard. He wrote the original code for these functions within a COM object. I later revised it into ASP functions.

 
Author: Douglas L. Setzer, II, http://www.27seconds.com
Posted On: 3/5/2002 11:31:10 AM

Rate this article: Average: 7.6
n/a12345678910
Comments?

Article Search   |   All Articles

 

 
©2002 27 Seconds, Inc. All Rights Reserved.