For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > April 2006 > XML from XSD









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author XML from XSD
S W

2006-04-25, 6:57 pm

Hi,

I would like to be able to create an xml file using the schema from an
xsd file. The file doesn't need any data, I can add it later, but I
already know the structure of the xml file I need to create, its in the
xsd. Is this possible using MSXML2 in a vb6 application? Having created
the file, I'll then go back, find the nodes I want and add the data.

I am struggling a bit because I can't find the help files for xml in my
Visual Studio 6 installation of MSDN help. If I put the cursor on an
MSXML property or method and press F1, I don't get any help, the MSDN
just says "Keyword not found". Normally of course help on the
highlighted topic appears. Maybe I'm missing an update?

Any help gratefully received,

Regards,

SW
Ralph

2006-04-25, 6:57 pm


"S W" <anonymousgravy@msn.com> wrote in message
news:uszS3SJaGHA.1192@TK2MSFTNGP03.phx.gbl...
> Hi,
>
> I would like to be able to create an xml file using the schema from an
> xsd file. The file doesn't need any data, I can add it later, but I
> already know the structure of the xml file I need to create, its in the
> xsd. Is this possible using MSXML2 in a vb6 application? Having created
> the file, I'll then go back, find the nodes I want and add the data.
>
> I am struggling a bit because I can't find the help files for xml in my
> Visual Studio 6 installation of MSDN help. If I put the cursor on an
> MSXML property or method and press F1, I don't get any help, the MSDN
> just says "Keyword not found". Normally of course help on the
> highlighted topic appears. Maybe I'm missing an update?
>
> Any help gratefully received,
>
> Regards,
>
> SW



Can't help with specific info, but can help with the "help" issue. The Help
files were never available and now never will be for VB6.

At the time the MSXML libraries were being developed there was a wide-range
of 'competing' standards and MS was changing the product about every 6
months. Results and documentation will depend dramatically on the version of
MSXML you are using. MSDN online is the best you are going to get from MS...
http://msdn.microsoft.com/library/d...db69cb3ba60.asp

Next best stop (once you fully understand the buzz words) is the Microsoft
XML newsgroups..
microsoft.public.xml....

hth
-ralph



Mike D Sutton

2006-04-25, 6:57 pm

> I would like to be able to create an xml file using the schema from an xsd
> file. The file doesn't need any data, I can add it later, but I already
> know the structure of the xml file I need to create, its in the xsd. Is
> this possible using MSXML2 in a vb6 application? Having created the file,
> I'll then go back, find the nodes I want and add the data.
>
> I am struggling a bit because I can't find the help files for xml in my
> Visual Studio 6 installation of MSDN help. If I put the cursor on an MSXML
> property or method and press F1, I don't get any help, the MSDN just says
> "Keyword not found". Normally of course help on the highlighted topic
> appears. Maybe I'm missing an update?


XSD files are simply XML files, you'll find out all you need to know about
their structure here:
http://www.w3schools.com/schema/default.asp
If you need information about MSXML then you'll find it here:
http://msdn.microsoft.com/library/e...nch_xmlprod.asp
Hope this helps,

Mike


- Microsoft Visual Basic MVP -
E-Mail: EDais@mvps.org
WWW: Http://EDais.mvps.org/


S W

2006-04-26, 3:56 am

Mike D Sutton wrote:
>
>
> XSD files are simply XML files, you'll find out all you need to know about
> their structure here:
> http://www.w3schools.com/schema/default.asp
> If you need information about MSXML then you'll find it here:
> http://msdn.microsoft.com/library/e...nch_xmlprod.asp
> Hope this helps,
>
> Mike
>
>
> - Microsoft Visual Basic MVP -
> E-Mail: EDais@mvps.org
> WWW: Http://EDais.mvps.org/
>
>


Hi Mike and Ralph,

Thanks for your input. If you had an XSD file and you wanted to create
an xml file in the same structure, but instead of the definition tags,
you wanted data, how would you go about it in VB6?
E.g.
Part of my XSD:

<xsd:element name="CONFIG">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="User" type="xs:string"/>
<xsd:element name="SaveDate" type="xs:string"/>
<xsd:element name="Filename" type="xs:string"/>
<xsd:element name="Created" type="xs:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

XML I need to create:


<CONFIG>
<User>Joe Bloggs</User>
<SaveDate>12/05/2005</SaveDate>
<FileName>File1.txt</FileName>
<Created></Created>
</CONFIG>

So what is the best way in VB6 to get from one to the other? Is there an
MSXML function that will use the XSD structure to create the XML file or
do I need to create each tag individually using MSXML functions?

Thanks again,

SW
Tony Proctor

2006-04-26, 3:56 am

There are 3rd-party tools that do this sort of thing. For instance, XMLSpy
(http://www.altova.com/download_spy_enterprise.html) has a menu option
'DTD/Schema. - Generate Sample XML File...' once an XSD file has been
loaded.

Tony Proctor

"S W" <anonymousgravy@msn.com> wrote in message
news:ehx3WWPaGHA.1352@TK2MSFTNGP05.phx.gbl...
> Mike D Sutton wrote:
xsd[color=darkred]
file,[color=darkred]
MSXML[color=darkred]
says[color=darkred]
about[color=darkred]
>
> Hi Mike and Ralph,
>
> Thanks for your input. If you had an XSD file and you wanted to create
> an xml file in the same structure, but instead of the definition tags,
> you wanted data, how would you go about it in VB6?
> E.g.
> Part of my XSD:
>
> <xsd:element name="CONFIG">
> <xsd:complexType>
> <xsd:sequence>
> <xsd:element name="User" type="xs:string"/>
> <xsd:element name="SaveDate" type="xs:string"/>
> <xsd:element name="Filename" type="xs:string"/>
> <xsd:element name="Created" type="xs:string"/>
> </xsd:sequence>
> </xsd:complexType>
> </xsd:element>
>
> XML I need to create:
>
>
> <CONFIG>
> <User>Joe Bloggs</User>
> <SaveDate>12/05/2005</SaveDate>
> <FileName>File1.txt</FileName>
> <Created></Created>
> </CONFIG>
>
> So what is the best way in VB6 to get from one to the other? Is there an
> MSXML function that will use the XSD structure to create the XML file or
> do I need to create each tag individually using MSXML functions?
>
> Thanks again,
>
> SW



Mike D Sutton

2006-04-26, 7:56 am

> Thanks for your input. If you had an XSD file and you wanted to create an
> xml file in the same structure, but instead of the definition tags, you
> wanted data, how would you go about it in VB6?

<xml snipped>
> So what is the best way in VB6 to get from one to the other? Is there an
> MSXML function that will use the XSD structure to create the XML file or
> do I need to create each tag individually using MSXML functions?


There is no in-built function that I know of in the MSXML DOM that will just
generate the document for you, however you may want to follow that up on the
xml groups to verify that.
Alternatively either use a third party solution as Tony suggested or simply
iterate the XSD document and build up a new XML document based on what you
find - Depending on how complex your XSD is this could be a pretty simple
bit of code (your above example for instance is very simple, however when
you start getting ranges of data, patterns and so on it gets a little more
complex.)
Hope this helps,

Mike


- Microsoft Visual Basic MVP -
E-Mail: EDais@mvps.org
WWW: Http://EDais.mvps.org/


Edward Wohlman

2006-04-26, 6:56 pm

If you have an XSD it is far easier to load it into the SOM, Schema Object
Model than it would be to load it into the DOM and try to interpret the XSD
properties yourself.

I think there was even an example somewhere of how to build a document tree
from the SOM

This article tells you more about it.
http://www.awprofessional.com/artic...=31360&seqNum=5




"Mike D Sutton" <EDais@mvps.org> wrote in message
news:%23bG49SSaGHA.1192@TK2MSFTNGP04.phx.gbl...
> <xml snipped>
>
> There is no in-built function that I know of in the MSXML DOM that will
> just generate the document for you, however you may want to follow that up
> on the xml groups to verify that.
> Alternatively either use a third party solution as Tony suggested or
> simply iterate the XSD document and build up a new XML document based on
> what you find - Depending on how complex your XSD is this could be a
> pretty simple bit of code (your above example for instance is very simple,
> however when you start getting ranges of data, patterns and so on it gets
> a little more complex.)
> Hope this helps,
>
> Mike
>
>
> - Microsoft Visual Basic MVP -
> E-Mail: EDais@mvps.org
> WWW: Http://EDais.mvps.org/
>



Edward Wohlman

2006-04-26, 6:56 pm


Start from here:
http://msdn.microsoft.com/library/d...2a5ddedbd1d.asp

"Edward Wohlman" <eddwo@hotpop.com> wrote in message
news:uZOn3UUaGHA.1192@TK2MSFTNGP03.phx.gbl...
> If you have an XSD it is far easier to load it into the SOM, Schema Object
> Model than it would be to load it into the DOM and try to interpret the
> XSD properties yourself.
>
> I think there was even an example somewhere of how to build a document
> tree from the SOM
>
> This article tells you more about it.
> http://www.awprofessional.com/artic...=31360&seqNum=5
>
>
>
>
> "Mike D Sutton" <EDais@mvps.org> wrote in message
> news:%23bG49SSaGHA.1192@TK2MSFTNGP04.phx.gbl...
>
>



Mike D Sutton

2006-04-26, 6:56 pm

> If you have an XSD it is far easier to load it into the SOM, Schema Object
> Model than it would be to load it into the DOM and try to interpret the
> XSD properties yourself.
>
> I think there was even an example somewhere of how to build a document
> tree from the SOM


Cool, wasn't aware of the SOM, thanks for the heads up!

Mike


- Microsoft Visual Basic MVP -
E-Mail: EDais@mvps.org
WWW: Http://EDais.mvps.org/


S W

2006-04-26, 6:56 pm

Edward Wohlman wrote:[color=darkred]
> Start from here:
> http://msdn.microsoft.com/library/d...2a5ddedbd1d.asp
>
> "Edward Wohlman" <eddwo@hotpop.com> wrote in message
> news:uZOn3UUaGHA.1192@TK2MSFTNGP03.phx.gbl...
>

Ed and All,

Thanks for the help, I will study the vbscript example.
Being new to xml, when I first began to learn about xsd files, I felt
sure that if the schema was there to validate the xml file, then it
could be used to create the file. I will keep on looking. Meanwhile I'm
using the DOM to create my xml file node by node. It works!

Regards,
SW
Stefan Berglund

2006-04-26, 6:56 pm

On Wed, 26 Apr 2006 22:35:46 +0100, S W <anonymousgravy@msn.com> wrote:
in <en4eclXaGHA.1348@TK2MSFTNGP05.phx.gbl>

>Edward Wohlman wrote:
>
>Ed and All,
>
>Thanks for the help, I will study the vbscript example.
>Being new to xml, when I first began to learn about xsd files, I felt
>sure that if the schema was there to validate the xml file, then it
>could be used to create the file. I will keep on looking. Meanwhile I'm
>using the DOM to create my xml file node by node. It works!
>
>Regards,
>SW


Just imagine this: What if the schema isn't there? Then what do you do?
Do you really want to be dependent on m$ and their schema? Isn't there
some other way to rethink this problem? Surely, there's another
solution if you just give it a little thought. There's always another
solution if you look at closely enough.

---
This posting is provided "AS IS" with no warranties and no guarantees either express or implied.

Stefan Berglund
Edward

2006-04-27, 7:56 am


It wouldn't be possible to create a sample document from only schema,
since the schema it not a description of exactly what a document should
be, only when a given document is valid.

For simple schemas it might work, but as soon as you get even slightly
complicated the possible variations become huge.

If a schema contains a choice group, which branch of the choice should
the sample document contain?
If a schema contains a group with minOccurs = 0 and maxOccurs =
Infinite how many repeats of that group should go into a sample
document?

Unless you can answer all these questions up front any sample document
you autogenerate from an xsd will only be one example of a valid
document, and not necessarily one with the structure you require.

If you want to look into XSDs I suggest you download at least the free
version of XmlSpy.
http://www.altova.com/download_spy_home.html
The schema viewer is superb and makes even the most complex schemas
easy to understand.
The free version will only display single-file schemas but it should
be enough to get you started.


S W wrote:
> Edward Wohlman wrote:
>
> Ed and All,
>
> Thanks for the help, I will study the vbscript example.
> Being new to xml, when I first began to learn about xsd files, I felt
> sure that if the schema was there to validate the xml file, then it
> could be used to create the file. I will keep on looking. Meanwhile I'm
> using the DOM to create my xml file node by node. It works!
>
> Regards,
> SW


Edward Wohlman

2006-04-28, 3:56 am

Heres a small VB example I knocked together that creates an empty document
from an XSD.

Using the xmlwriter makes it really easy, as you don't need to worry about
creating <opening> </closing> or <empty/> tags.

Private Sub Command1_Click()
'load in the schema
Dim MySchemaCache As New XMLSchemaCache60
MySchemaCache.validateOnLoad = False
Call MySchemaCache.Add("{xsd root namespace}", "{xsd file path}")
Call MySchemaCache.Validate

'get a reference to the validated schema
Dim MySchema As ISchema
Set MySchema = MySchemaCache.getSchema("{xsd root namespace}")

'get s reference to the schema definition for the root node of the
document
Dim elem As ISchemaElement
Set elem = MySchema.elements.itemByName("{xsd root node}")

'create an xmlwriter
Dim writer As MXXMLWriter60, content As IVBSAXContentHandler
Set writer = New MXXMLWriter
Set content = writer
writer.indent = True

'create the document
content.startDocument
Call ProcessType(elem, content)
content.endDocument
writer.flush

'do something with the output
Text1.Text = writer.output

End Sub

Private Sub ProcessType(typ As ISchemaItem, output As IVBSAXContentHandler)
Dim subtype As ISchemaItem, particle As ISchemaParticle, docount As
Integer
docount = 1
On Error Resume Next
Set particle = typ
on error goto 0
If Not particle Is Nothing Then
If particle.minOccurs > 1 Then
docount = particle.minOccurs
End If
If particle.maxOccurs > -1 Then
docount = particle.maxOccurs
End If
End If

Dim n As Integer
For n = 1 To docount
Select Case typ.itemType
Case SOMITEM_ELEMENT
Dim elem As ISchemaElement
Set elem = typ
Call output.startElement(vbNullChar, vbNullChar, elem.Name, Nothing)
Call ProcessType(elem.Type, output)
Call output.endElement(vbNullChar, vbNullChar, elem.Name)
Case SOMITEM_COMPLEXTYPE
Dim cpx As ISchemaComplexType
Set cpx = typ
Call ProcessType(cpx.contentModel, output)
Case SOMITEM_SEQUENCE, SOMITEM_ALL
'output each part of the sequence in order
Dim seq As ISchemaModelGroup
Set seq = typ
For Each subtype In seq.particles
Call ProcessType(subtype, output)
Next
Case SOMITEM_CHOICE
Dim choice As ISchemaModelGroup
Set choice = typ
'only output the first option in the choice group
Call ProcessType(choice.particles.Item(0), output)
Case SOMITEM_SIMPLETYPE
Dim typeinfo As ISchemaType
Set typeinfo = typ
If typeinfo.enumeration.length > 0 Then
Call output.characters(typeinfo.enumeration.Item(0))
End If
Case SOMITEM_EMPTYPARTICLE, SOMITEM_ANYTYPE
'do nothing, just return
Call output.characters("")
Case Else
'whatever
End Select
Next
End Sub


"Edward" <EddWo@hotpop.com> wrote in message
news:1146132227.419328.5070@j33g2000cwa.googlegroups.com...
>
> It wouldn't be possible to create a sample document from only schema,
> since the schema it not a description of exactly what a document should
> be, only when a given document is valid.
>
> For simple schemas it might work, but as soon as you get even slightly
> complicated the possible variations become huge.
>
> If a schema contains a choice group, which branch of the choice should
> the sample document contain?
> If a schema contains a group with minOccurs = 0 and maxOccurs =
> Infinite how many repeats of that group should go into a sample
> document?
>
> Unless you can answer all these questions up front any sample document
> you autogenerate from an xsd will only be one example of a valid
> document, and not necessarily one with the structure you require.
>
> If you want to look into XSDs I suggest you download at least the free
> version of XmlSpy.
> http://www.altova.com/download_spy_home.html
> The schema viewer is superb and makes even the most complex schemas
> easy to understand.
> The free version will only display single-file schemas but it should
> be enough to get you started.
>
>
> S W wrote:
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com