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

How to create XML Document

Status
Not open for further replies.

ChrisEb

Programmer
Sep 11, 2003
3
DE
Hello all,

I have a question creating xml documents. I want to create the following line in my xml document:

<?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?>

I have the following code:

dim vCodeSetInstruction As IXMLDOMProcessingInstruction
dim g_XmlDoc as MSXML2.DOMDocument

Set g_XmlDoc = New MSXML2.DOMDocument
Set vCodeSetInstruction = g_XmlDoc.createProcessingInstruction _
(&quot;xml&quot;, &quot;version=&quot;&quot;1.0&quot;&quot; encoding=&quot;&quot;ISO-8859-1&quot;&quot;&quot;)

g_XmlDoc.appendChild vCodeSetInstruction

When I write this to a file I see only this:

<?xml version=&quot;1.0&quot;?>

Where is my encoding? How can I write this into the processing instruction? Am I doing something wrong?

Any help would be appreciated.

Regards
Chris
 

Its not really that you are doing something wrong, its more of the way VB handles strings.

try...
[tt]
(&quot;xml&quot;, &quot;version=&quot;&quot;1.0&quot;&quot; & &quot;encoding=&quot; & &quot;&quot;ISO-8859-1&quot;&quot;)
[/tt]
You may have to play with it a bit

Good Luck

 
I found the solution. Instead of using DOMDocument.xml attribute (that was my way) I have to use DOMDocument.Save method to get what I want.

Regards
Chris
 
I think vb5prgrmr was right - it was the usual VB &quot; problem. What you did should have worked fine once the quote-marks were sorted out. I have used the same method as you, I just used single quotes as delimiters:

[/code]
Set xpProcIns = .createProcessingInstruction(&quot;xml&quot;, &quot;version='1.0' encoding='UTF-8'&quot;)

.appendChild xpProcIns
[/code]

Regards

Daren



Must think of a witty signature
 
Here's some VB6 code for this:
Code:
Private Const m_PI_ENCODING As String = &quot;windows-1252&quot;
Private Const m_PI_VERSION As String = &quot;version=&quot;&quot;1.0&quot;&quot;&quot;
:
:
Set oUserDOM = New MSXML2.DOMDocument40
Set oPINode = oUserDOM.createProcessingInstruction(&quot;xml&quot;, m_PI_VERSION)
Set oPIAttr = oUserDOM.createAttribute(&quot;encoding&quot;)
oPIAttr.Text = m_PI_ENCODING
Call oPINode.Attributes.setNamedItem(oPIAttr)
Call oUserDOM.appendChild(oPINode)
The trick is that you have to use the createProcessingInstruction call to get the <?xml version=&quot;1.0&quot;?> element in there, but you then append the encoding as your garden-variety attribute to it.

BTW, string-concatenation is a bad way to produce XML. You run into problems where the entities aren't escaped. If you were to do this:
Code:
sXML = xXML & &quot;<MyElement>Wiggens & Johnson</MyElement>&quot;
you will produce invalid XML, as the ampersand was not escaped. While you can write a function to escape the ampersand, quote characters (single & double), and the angle-brackets; it's messy, and it's much better for you to let the DOM handle it.
Code:
Dim oElem as MSXML4.IXMLDOMElement

Set oElem = oMyDOM.CreateElement(&quot;MyElement&quot;)
oElem.Text = &quot;Wiggens & Johnson&quot;
Call oRootNode.appendChild(oElem)
(where oMyDOM is your DOM object, and oRootNode is the node you want to append you element under).

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top