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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting Doctype in an XML file 1

Status
Not open for further replies.

gtardio

Programmer
Jun 8, 2007
4
ES
Hello,

I am creting an XML file with visual basic 6 and I can`t

insert the DOCTYPE LINE at the begging

<!DOCTYPE invoice-batch PUBLIC
"-//easyap/easyap Mapping DTD//EN"
"InvoiceImport-DINOSOL.dtd">

My client needs this file and the doctype property is read only.

Thanks
 
This is how you do it. (The setting of validateonparse and resolveexternals is important.)
[tt]
'dim oparser as new msxml2.domdocument30 '3.0 or higher
with oparser
set oroot=.createelement("root") 'or some name of design
end with

s="<?xml version=""1.0"" encoding=""utf-8"" ?>"
s=s & vbcrlf & "<!DOCTYPE invoice-batch PUBLIC ""-//easyap/easyap Mapping DTD//EN"" ""InvoiceImport-DINOSOL.dtd"">"
s=s & vbcrlf & oroot.xml

[blue]'now here is the important procedure
with oparser
.validateonparser=false
.resolveexternals=false
end with
oparser.loadxml s[/blue]

'and now you continue building the document
[/tt]
 
Hello,

I can`t get the first line in the XML.

What I am doing wrong?

Private Sub Command1_Click()
Dim s As String
Dim crlf As String
Dim lDOMDoc As New MSXML2.DOMDocument30
Dim lDOMRootNode As MSXML2.IXMLDOMNode
Dim lDOMLev1Node As MSXML2.IXMLDOMNode

crlf = Chr(10) & Chr(13)
Set lDOMRootNode = lDOMDoc.createElement("root")
s = "<?xml version=""1.0""encoding=""UTF-8"" ?>"
s = s & crlf & "DOCTYPE invoice-batch PUBLIC ""-//easyap/easyap Mapping DTD//EN ""InvoiceImport-DINOSOL.dtd"">"
s = s & crlf & lDOMRootNode.xml
lDOMDoc.validateOnParse = False
lDOMDoc.resolveExternals = False
lDOMDoc.loadXML (s)
lDOMDoc.appendChild lDOMRootNode
Set lDOMLev1Node = lDOMDoc.createElement("invoice-group")
lDOMRootNode.appendChild lDOMDoc.createTextNode(crlf)
lDOMRootNode.appendChild lDOMLev1Node
lDOMRootNode.appendChild lDOMDoc.createTextNode(crlf)
lDOMDoc.save ("c:\temp\easy.xml")

End Sub


Thanks very much for your help!
 
Hello,

I made some changes and I finally got the first line and child nodes. I had some errors on the s string. Then I was trying to append nodes to the root node and apparently this is not possble. Finally , I concatenated the s string with the rootnode (that by now has some childs) and then loaded the string to the MSXML doument.

Thanks Tsuji, this has been incredible help for me. I was stuck WEEKS, with this!!!!

Here is the code.

By

Private Sub Command1_Click()
Dim s As String
Dim crlf As String
Dim lDOMDoc As New MSXML2.DOMDocument30
Dim lDOMRootNode As MSXML2.IXMLDOMNode
Dim lDOMLev1Node As MSXML2.IXMLDOMNode

crlf = Chr(10) & Chr(13)
Set lDOMRootNode = lDOMDoc.createElement("root")
s = "<?xml version=""1.0"" encoding=""utf-8"" ?>"
s = s & crlf & "<!DOCTYPE invoice-batch PUBLIC ""-//easyap/easyap Mapping DTD//EN"" ""InvoiceImport-DINOSOL.dtd"">"
s = s & crlf
Set lDOMDoc = CreateObject("MSXML2.DOMDocument.3.0")
lDOMDoc.validateOnParse = False
lDOMDoc.resolveExternals = False
lDOMDoc.loadXML (s)
Set lDOMLev1Node = lDOMDoc.createElement("invoice-group")
lDOMRootNode.appendChild lDOMDoc.createTextNode(crlf)
lDOMRootNode.appendChild lDOMLev1Node
lDOMRootNode.appendChild lDOMDoc.createTextNode(crlf)
s = s & lDOMRootNode.xml
lDOMDoc.loadXML (s)
lDOMDoc.save ("c:\temp\easy.xml")
End Sub
 
[tt]Private Sub Command1_Click()
Dim s As String
Dim crlf As String
Dim lDOMDoc As New MSXML2.DOMDocument30
Dim lDOMRootNode As MSXML2.IXMLDOMNode
Dim lDOMLev1Node As MSXML2.IXMLDOMNode

[red]'[/red]crlf = Chr(10) & Chr(13)
[blue]crlf = Chr(13) & Chr(10)[/blue]
Set lDOMRootNode = lDOMDoc.createElement("root")
s = "<?xml version=""1.0"" encoding=""utf-8"" ?>"
s = s & crlf & "<!DOCTYPE invoice-batch PUBLIC ""-//easyap/easyap Mapping DTD//EN"" ""InvoiceImport-DINOSOL.dtd"">"
s = s & crlf
[green]'Here you can directly set up the root xml
'It is <root /> we all know
'The above through createElement is to put on value of how things come about and do not take it for granted that it is <root />[/green]
[blue]s = s & IDOMRootNode.xml[/blue]
[green]'You don't need to create the instance again, just stick to the existing early bound one[/green]
[red]'[/red]Set lDOMDoc = CreateObject("MSXML2.DOMDocument.3.0")
lDOMDoc.validateOnParse = False
lDOMDoc.resolveExternals = False
lDOMDoc.loadXML (s)
[green]'This line will give you back the document's root reference[/green]
[blue]Set lDOMRootNode = lDOMDoc.documentElement[/blue]
Set lDOMLev1Node = lDOMDoc.createElement("invoice-group")
lDOMRootNode.appendChild lDOMDoc.createTextNode(crlf)
lDOMRootNode.appendChild lDOMLev1Node
lDOMRootNode.appendChild lDOMDoc.createTextNode(crlf)
[green]'You don't need anymore this, the lDOMDoc is already properly setup[/green]
[red]'[/red]s = s & lDOMRootNode.xml
[red]'[/red]lDOMDoc.loadXML (s)
lDOMDoc.save ("c:\temp\easy.xml")
End Sub
[/tt]
 
Upon looking into your doctype line, I think you have to name your root element by "invoice-batch" to reflect your intention.
[tt]
Private Sub Command1_Click()
Dim s As String
Dim crlf As String
Dim lDOMDoc As New MSXML2.DOMDocument30
Dim lDOMRootNode As MSXML2.IXMLDOMNode
Dim lDOMLev1Node As MSXML2.IXMLDOMNode

crlf = Chr(13) & Chr(10)

Set lDOMRootNode = lDOMDoc.createElement("[red]invoice-batch[/red]")

s = "<?xml version=""1.0"" encoding=""utf-8"" ?>"
s = s & crlf & "<!DOCTYPE invoice-batch PUBLIC ""-//easyap/easyap Mapping DTD//EN"" ""InvoiceImport-DINOSOL.dtd"">"
s = s & crlf
s = s & IDOMRootNode.xml

lDOMDoc.validateOnParse = False
lDOMDoc.resolveExternals = False
lDOMDoc.loadXML s

Set lDOMRootNode = lDOMDoc.documentElement
Set lDOMLev1Node = lDOMDoc.createElement("invoice-group")
lDOMRootNode.appendChild lDOMDoc.createTextNode(crlf)
lDOMRootNode.appendChild lDOMLev1Node
lDOMRootNode.appendChild lDOMDoc.createTextNode(crlf)

lDOMDoc.save "c:\temp\easy.xml"
End Sub
[/tt]
 
Hi again,

1.- Yes the final code has

Set lDOMRootNode = lDOMDoc.createElement("invoice-batch"). This xml file is validated with a dtd.

<:)

2.- After inserting the line
Set lDOMRootNode = lDOMDoc.documentElement

I can add nodes again !!

3.- For the moment I am sticking to load the xml at the end of the process. (after creating all the sub nodes)


lDOMDoc.loadXML (s & lDOMRootNode.xml)

Aparently, this way, it insert the tabs automatically. I was doing this manually, every time I created a new level I inserted the chr(9) character. (lot of work! since its a long xml).

For some reason, (??) this xml file has to be seen with a note pad, and needs to have the correct carriage returns and tabs. Maybe there is a way to insert CR and tabs automatically but loading the rootnode does the same.


Thanks again for your time!
 
>3.- For the moment I am sticking to load the xml at the end of the process. (after creating all the sub nodes)
I don't like entangling on the matter. Could you read carefully my revision? In particular:
>[self] [tt][green]'You don't need anymore this, the lDOMDoc is already properly setup[/green][/tt]
You never need to loadxml ever again apart from the initial setup.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top