How to add schema to root element using msxml 6? - XML SOAP
This is a discussion on How to add schema to root element using msxml 6? - XML SOAP ; I want to add the schema info to the root element of the file. When
the file is generated I want the root element to look like:
<site xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="site.xsd">
I have the following code but the parts related to ...
-
How to add schema to root element using msxml 6?
I want to add the schema info to the root element of the file. When
the file is generated I want the root element to look like:
<site xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="site.xsd">
I have the following code but the parts related to the schema do not
work for me and I don't how to add the schema info to the root
element.
Public Function CreateXMLFile(DocType As String, _
ADORST As ADODB.Recordset, _
XMLFileName As String, _
Optional XMLPath As String, _
Optional ADORST2 As ADODB.Recordset) As Boolean
' Comments : Create XML file from supplied rst using MSXML6
' Parameters: DocType - String that contains the type of document
to export. The values can be:
' site
' well
' visit
' ADORST - ADODB recordset containing the data to
export
' XMLFileName - string that represents the name of the
file to be saved
' XMLPath - path of the xml file to export. If its
empty, save to same location as the Front End
' ADORST2 - ADODB recordset containing the subitems to
export
' Created : 04 Feb 2007 06:50 JA
' Modified :
' --------------------------------------------------
On Error GoTo ErrorHandler
Dim xmlDoc As MSXML2.DOMDocument
'Dim xmlDoc As MSXML2.FreeThreadedDOMDocument
'Dim NSMgr As MSXML2.MXNamespaceManager60
Dim SchemaCache As MSXML2.XMLSchemaCache
Dim root As IXMLDOMElement
Dim xmlElement As IXMLDOMElement
Dim newNode As IXMLDOMNode
Dim fld As ADODB.Field
Dim strPath As String
CreateXMLFile = False
Set xmlDoc = New MSXML2.DOMDocument
xmlDoc.async = False
xmlDoc.validateOnParse = False
xmlDoc.resolveExternals = False
xmlDoc.preserveWhiteSpace = True
'Create XML prolog and processing Instructions
Set newNode = xmlDoc.createProcessingInstruction("xml",
"version=""1.0"" encoding=""ISO-8859-1""")
xmlDoc.appendChild newNode
Set newNode = Nothing
'Add schema
'Set NSMgr = New MSXML2.MXNamespaceManager60
'NSMgr.declarePrefix "xsi", "http://www.w3.org/2001/XMLSchema-
instance"
'NSMgr.declarePrefix "", DocType & ".xsd"
'NSMgr.declarePrefix "xsi:noNamespaceSchemaLocation", DocType &
".xsd"
Set SchemaCache = New MSXML2.XMLSchemaCache
'SchemaCache.Add "xsi", "http://www.w3.org/2001/XMLSchema-
instance"
SchemaCache.Add "", CurrentProject.Path & "\" & DocType & ".xsd"
Set xmlDoc.schemas = SchemaCache
'Set xmlDoc.schemas = NSMgr
'Create root node
Set root = xmlDoc.createElement(DocType)
xmlDoc.appendChild root
'Go through the fields in the recordset and add them to the xml
file
For Each fld In ADORST.Fields
root.appendChild xmlDoc.createTextNode(vbNewLine + vbTab)
Set newNode = xmlDoc.createElement(fld.Name)
If Not IsNull(fld.Value) Then
newNode.Text = fld.Value
End If
root.appendChild newNode
Set newNode = Nothing
Next
'Build path and save
If XMLPath = vbNullString Then
strPath = CurrentProject.Path & "\" & XMLFileName
Else
strPath = XMLPath & XMLFileName
End If
xmlDoc.Save strPath
CreateXMLFile = True
ExitHandler:
Set root = Nothing
Set xmlDoc = Nothing
Exit Function
ErrorHandler:
MsgBox _
Prompt:="Error " & Err.Number & vbNewLine & Err.Description &
vbNewLine & "In CreateXMLFile."
Resume ExitHandler
End Function
-
Re: How to add schema to root element using msxml 6?
* JesseAviles wrote in microsoft.public.xml:
>I want to add the schema info to the root element of the file. When
>the file is generated I want the root element to look like:
> <site xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>xsi:noNamespaceSchemaLocation="site.xsd">
Create a new attribute node using xmlDoc.createNode and add it to the
<site> element node using setAttributeNode like so
var noNsSL = xmlDoc.createNode(
NODE_ATTRIBUTE,
"noNamespaceSchemaLocation",
"site.xsd");
siteElem.setAttributeNode(noNsSL);
--
Björn Höhrmann · mailto:bjoern@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
-
Re: How to add schema to root element using msxml 6?
Bjoern Hoehrmann wrote:
> * JesseAviles wrote in microsoft.public.xml:
>> I want to add the schema info to the root element of the file. When
>> the file is generated I want the root element to look like:
>> <site xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>> xsi:noNamespaceSchemaLocation="site.xsd">
>
> Create a new attribute node using xmlDoc.createNode and add it to the
> <site> element node using setAttributeNode like so
>
> var noNsSL = xmlDoc.createNode(
> NODE_ATTRIBUTE,
> "noNamespaceSchemaLocation",
> "site.xsd");
> siteElem.setAttributeNode(noNsSL);
That should be
var noNsSL = xmlDoc.createNode(
2,
"noNamespaceSchemaLocation",
"http://www.w3.org/2001/XMLSchema-instance"
);
noNsSL.value = "site.xsd";
siteElem.setAttributeNode(noNsSL);
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
-
Re: How to add schema to root element using msxml 6?
Finally made it work. This is the final code after modifying what you
said:
Public Function CreateXMLFile(DocType As String, _
ADORST As ADODB.Recordset, _
XMLFileName As String, _
Optional XMLPath As String, _
Optional ADORST2 As ADODB.Recordset) As Boolean
' Comments : Create XML file from supplied rst using MSXML6
' Parameters: DocType - String that contains the type of document
to export. The values can be:
' site
' well
' visit
' ADORST - ADODB recordset containing the data to
export
' XMLFileName - string that represents the name of the
file to be saved
' XMLPath - path of the xml file to export. If its
empty, save to same location as the Front End
' ADORST2 - ADODB recordset containing the subitems to
export
' Created : 04 Feb 2007 06:50 JA
' Modified : 2007-02-15 21:44 JA
' --------------------------------------------------
On Error GoTo ErrorHandler
Dim xmlDoc As MSXML2.DOMDocument60
Dim root As IXMLDOMElement
Dim xmlElement As IXMLDOMElement
Dim newNode As IXMLDOMNode
Dim namedNodeMap As IXMLDOMNamedNodeMap
Dim atrNSNode As IXMLDOMAttribute
Dim fld As ADODB.Field
Dim strPath As String
CreateXMLFile = False
Set xmlDoc = New MSXML2.DOMDocument60
xmlDoc.async = False
xmlDoc.validateOnParse = False
xmlDoc.resolveExternals = False
xmlDoc.preserveWhiteSpace = True
'Create XML prolog and processing Instructions
Set newNode = xmlDoc.createProcessingInstruction("xml",
"version=""1.0"" encoding=""ISO-8859-1""")
xmlDoc.appendChild newNode
Set newNode = Nothing
'Create root node and Add schema
Set root = xmlDoc.createElement(DocType)
xmlDoc.appendChild root
Set atrNSNode = xmlDoc.createNode(NODE_ATTRIBUTE, "xmlns:xsi",
"http://www.w3.org/2001/XMLSchema-instance")
atrNSNode.Value = "http://www.w3.org/2001/XMLSchema-instance"
Set root = xmlDoc.selectSingleNode("//" & DocType)
root.setAttributeNode atrNSNode
Set atrNSNode = xmlDoc.createNode(NODE_ATTRIBUTE, _
"xsi:noNamespaceSchemaLocation", _
"http://www.w3.org/2001/XMLSchema-instance")
atrNSNode.Value = DocType & ".xsd"
Set root = xmlDoc.selectSingleNode("//" & DocType)
root.setAttributeNode atrNSNode
'Go through the fields in the recordset and add them to the xml
file
For Each fld In ADORST.Fields
root.appendChild xmlDoc.createTextNode(vbNewLine + vbTab)
Set newNode = xmlDoc.createElement(fld.Name)
If Not IsNull(fld.Value) Then
newNode.Text = fld.Value
End If
root.appendChild newNode
Set newNode = Nothing
Next
root.appendChild xmlDoc.createTextNode(vbNewLine)
'Build path and save
If XMLPath = vbNullString Then
strPath = CurrentProject.Path & "\" & XMLFileName
Else
strPath = XMLPath & XMLFileName
End If
xmlDoc.Save strPath
CreateXMLFile = True
ExitHandler:
Set root = Nothing
Set xmlDoc = Nothing
Exit Function
ErrorHandler:
MsgBox _
Prompt:="Error " & Err.Number & vbNewLine & Err.Description &
vbNewLine & "In CreateXMLFile."
Resume ExitHandler
End Function
Tanks for your help.
-
Re: How to add schema to root element using msxml 6?
JesseAviles wrote:
> Set root = xmlDoc.createElement(DocType)
> xmlDoc.appendChild root
> Set atrNSNode = xmlDoc.createNode(NODE_ATTRIBUTE, "xmlns:xsi",
> "http://www.w3.org/2001/XMLSchema-instance")
Note that xmlns:someprefix attributes are by definition in the namespace
http://www.w3.org/2000/xmlns/ so the above line should be
Set atrNSNode = xmlDoc.createNode(NODE_ATTRIBUTE, "xmlns:xsi",
"http://www.w3.org/2000/xmlns/")
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Similar Threads
-
By Application Development in forum XML SOAP
Replies: 1
Last Post: 12-12-2007, 09:00 AM
-
By Application Development in forum XML SOAP
Replies: 2
Last Post: 06-05-2007, 06:24 AM
-
By Application Development in forum XML SOAP
Replies: 0
Last Post: 01-25-2007, 02:15 PM
-
By Application Development in forum XML SOAP
Replies: 0
Last Post: 11-14-2006, 06:14 PM
-
By Application Development in forum XML SOAP
Replies: 1
Last Post: 11-10-2006, 04:57 PM