This is a multi-part message in MIME format.
--------------060001050103050909090200
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

I have mercilessly hacked previous XML code, I am trying to create a function
that will create a child node. Which this function does. What I would really
like is to be able to create a child of a particular element. Nothing I have
goggled seems very clear. I would also like to be able to check to see if the
element listed as the root element exists. My problem is, that I can work well
with code (not with fancy explanations, I gotta see it work to figure it out).
Any help would be greatly appreciated.

<--------Code--------------------<<
Sub XMLCreateChild(strRootElement, strChildElement, strElementAtribute,
strElementValue)
Dim oChildNode, objCurrNode

'Add Code to see if strRootElement exists
'If False Error
'If True then set strRootElement as the root element

' --------------------------------------------
' add an element to the xml file...
' --------------------------------------------
Set oChildNode = xmlDoc.createElement(strChildElement)
oRootNode.appendChild oChildNode ' make child of root element
' ----------------------------------------------
' add an "attribute" to this child node...
' ----------------------------------------------
oChildNode.setAttribute strElementAtribute, strElementValue
End Sub
<-------End Code-----------------<<

P.S. I also could not figure out how to create (or what to call) an element that
has sub elements. My goal is to create a XML file, like the enclosed. Many thanks.

--------------060001050103050909090200
Content-Type: text/xml;
name="CSOptionsBasic.xml"
Content-Transfer-Encoding: base64
Content-Disposition: inline;
filename="CSOptionsBasic.xml"

PE9wdGlvbnM+DQogIDxCYXNpY09wdGlvbnM+DQogICAgPGJvb1NuZFN1Y2Nlc3MgLz4NCiAg
ICA8Ym9vU25kRmFpbCAvPg0KICAgIDxib29TbmRDb21wbGV0ZSAvPg0KICAgIDxyZG9HcmFw
aDAgLz4NCiAgICA8cmRvR3JhcGgxIC8+DQogICAgPERyb3BEb3duMSAvPg0KICAgIDxEcm9w
RG93bjIgLz4NCiAgICA8RHJvcERvd24zIC8+DQogICAgPERyb3BEb3duNCAvPg0KICAgIDxJ
bnB1dEZpbGUgLz4NCiAgPC9CYXNpY09wdGlvbnM+DQogIDxBZHZhbmNlZE9wdGlvbnM+DQog
ICAgPFVwUGljdHVyZSAvPg0KICAgIDxVcEV4Y2hQaWN0dXJlIC8+DQogICAgPFVwQmFja1Bp
Y3R1cmUgLz4NCiAgICA8VXBOZXR3UGljdHVyZUROUyAvPg0KICAgIDxVcE5ldHdQaWN0dXJl
V0lOcyAvPg0KICAgIDxVcFByaW50UGljdHVyZSAvPg0KICAgIDxNaWRQaWN0dXJlIC8+DQog
ICAgPERvd25QaWN0dXJlIC8+DQogICAgPERvd25FeGNoUGljdHVyZSAvPg0KICAgIDxEb3du
QmFja1BpY3R1cmUgLz4NCiAgICA8RG93bk5ldHdQaWN0dXJlRE5TIC8+DQogICAgPERvd25O
ZXR3UGljdHVyZVdJTnMgLz4NCiAgICA8RG93blByaW50UGljdHVyZSAvPg0KICAgIDxEb3du
U291bmQgLz4NCiAgICA8VXBTb3VuZCAvPg0KICAgIDxDb21wbGV0ZVNvdW5kIC8+DQogIDwv
QWR2YW5jZWRPcHRpb25zPg0KPC9PcHRpb25zPg==
--------------060001050103050909090200--

Re: Help with XML function by Anthony

Anthony
Thu Mar 01 03:15:01 CST 2007


"E C H (He of too much code)" <glasswalkertheurge@juno.com> wrote in message
news:%23e8laZ8WHHA.996@TK2MSFTNGP02.phx.gbl...
> I have mercilessly hacked previous XML code, I am trying to create a
function
> that will create a child node. Which this function does. What I would
really
> like is to be able to create a child of a particular element. Nothing I
have
> goggled seems very clear. I would also like to be able to check to see if
the
> element listed as the root element exists. My problem is, that I can work
well
> with code (not with fancy explanations, I gotta see it work to figure it
out).
> Any help would be greatly appreciated.
>
> <--------Code--------------------<<
> Sub XMLCreateChild(strRootElement, strChildElement, strElementAtribute,
> strElementValue)
> Dim oChildNode, objCurrNode
>
> 'Add Code to see if strRootElement exists
> 'If False Error
> 'If True then set strRootElement as the root element
>
> ' --------------------------------------------
> ' add an element to the xml file...
> ' --------------------------------------------
> Set oChildNode = xmlDoc.createElement(strChildElement)
> oRootNode.appendChild oChildNode ' make child of root element
> ' ----------------------------------------------
> ' add an "attribute" to this child node...
> ' ----------------------------------------------
> oChildNode.setAttribute strElementAtribute, strElementValue
> End Sub
> <-------End Code-----------------<<
>
> P.S. I also could not figure out how to create (or what to call) an
element that
> has sub elements. My goal is to create a XML file, like the enclosed.
Many thanks.
>


This is my standard VBScript function for adding a child element:-

Function AddElem(roParent, rsName, rvntValue)
Set AddElem = roParent.ownerDocument.createElement(rsName)
roParent.appendChild AddElem
If Not IsNull(rvntValue) AddElem.Text = rvntValue
End Function


Give the above function to generate your XML you can use this code :-



Dim moDOM : Set oDOM = CreateObject("MSXML2.DOMDocument.3.0")

Dim moRoot, moBasicOptions, moAdvancedOptions

Set moRoot = AddElem(moDOM, "Options", Null)

AddBasicOptions moRoot
AddAdvancedOptions moRoot

Sub AddBasicOptions(roParent)
Dim elem : Set elem = AddElem(roParent, "BasicOptions", Null)

AddElem elem, "booSndSuccess"
AddElem elem, "booSndFail"
.
.
.
AddElem elem, "InputFile "
End Function

Sub AddAdvancedOptions(roParent)
Dim elem : Set elem = AddElem(roParent, "AdvancedOptions", Null)

AddElem elem, "UpPicture "
AddElem elem, "UpExchPicture "
.
.
.
AddElem elem, "CompleteSound "
End Function




----------------------------------------------------------------------------
----


> <Options>
> <BasicOptions>
> <booSndSuccess />
> <booSndFail />
> <booSndComplete />
> <rdoGraph0 />
> <rdoGraph1 />
> <DropDown1 />
> <DropDown2 />
> <DropDown3 />
> <DropDown4 />
> <InputFile />
> </BasicOptions>
> <AdvancedOptions>
> <UpPicture />
> <UpExchPicture />
> <UpBackPicture />
> <UpNetwPictureDNS />
> <UpNetwPictureWINs />
> <UpPrintPicture />
> <MidPicture />
> <DownPicture />
> <DownExchPicture />
> <DownBackPicture />
> <DownNetwPictureDNS />
> <DownNetwPictureWINs />
> <DownPrintPicture />
> <DownSound />
> <UpSound />
> <CompleteSound />
> </AdvancedOptions>
> </Options>



Re: Help with XML function by Anthony

Anthony
Thu Mar 01 03:30:23 CST 2007


"Anthony Jones" <Ant@yadayadayada.com> wrote in message
news:uEH4YI%23WHHA.4252@TK2MSFTNGP06.phx.gbl...
>
> "E C H (He of too much code)" <glasswalkertheurge@juno.com> wrote in
message
> news:%23e8laZ8WHHA.996@TK2MSFTNGP02.phx.gbl...
> > I have mercilessly hacked previous XML code, I am trying to create a
> function
> > that will create a child node. Which this function does. What I would
> really
> > like is to be able to create a child of a particular element. Nothing I
> have
> > goggled seems very clear. I would also like to be able to check to see
if
> the
> > element listed as the root element exists. My problem is, that I can
work
> well
> > with code (not with fancy explanations, I gotta see it work to figure it
> out).
> > Any help would be greatly appreciated.
> >
> > <--------Code--------------------<<
> > Sub XMLCreateChild(strRootElement, strChildElement, strElementAtribute,
> > strElementValue)
> > Dim oChildNode, objCurrNode
> >
> > 'Add Code to see if strRootElement exists
> > 'If False Error
> > 'If True then set strRootElement as the root element
> >
> > ' --------------------------------------------
> > ' add an element to the xml file...
> > ' --------------------------------------------
> > Set oChildNode = xmlDoc.createElement(strChildElement)
> > oRootNode.appendChild oChildNode ' make child of root element
> > ' ----------------------------------------------
> > ' add an "attribute" to this child node...
> > ' ----------------------------------------------
> > oChildNode.setAttribute strElementAtribute, strElementValue
> > End Sub
> > <-------End Code-----------------<<
> >
> > P.S. I also could not figure out how to create (or what to call) an
> element that
> > has sub elements. My goal is to create a XML file, like the enclosed.
> Many thanks.
> >
>
>
> This is my standard VBScript function for adding a child element:-
>
> Function AddElem(roParent, rsName, rvntValue)
> Set AddElem = roParent.ownerDocument.createElement(rsName)
> roParent.appendChild AddElem
> If Not IsNull(rvntValue) AddElem.Text = rvntValue
> End Function
>
>
> Give the above function to generate your XML you can use this code :-
>
>
>
> Dim moDOM : Set oDOM = CreateObject("MSXML2.DOMDocument.3.0")
>
> Dim moRoot, moBasicOptions, moAdvancedOptions
>
> Set moRoot = AddElem(moDOM, "Options", Null)
>
> AddBasicOptions moRoot
> AddAdvancedOptions moRoot
>
> Sub AddBasicOptions(roParent)
> Dim elem : Set elem = AddElem(roParent, "BasicOptions", Null)
>
> AddElem elem, "booSndSuccess"
> AddElem elem, "booSndFail"
> .
> .
> .
> AddElem elem, "InputFile "
> End Function
>
> Sub AddAdvancedOptions(roParent)
> Dim elem : Set elem = AddElem(roParent, "AdvancedOptions", Null)
>
> AddElem elem, "UpPicture "
> AddElem elem, "UpExchPicture "
> .
> .
> .
> AddElem elem, "CompleteSound "
> End Function
>

Oops those last AddElems should have a third parameter Null. (I wish
VBScript had allow for Optional params in it's procedures)

>
>
>
> --------------------------------------------------------------------------
--
> ----
>
>
> > <Options>
> > <BasicOptions>
> > <booSndSuccess />
> > <booSndFail />
> > <booSndComplete />
> > <rdoGraph0 />
> > <rdoGraph1 />
> > <DropDown1 />
> > <DropDown2 />
> > <DropDown3 />
> > <DropDown4 />
> > <InputFile />
> > </BasicOptions>
> > <AdvancedOptions>
> > <UpPicture />
> > <UpExchPicture />
> > <UpBackPicture />
> > <UpNetwPictureDNS />
> > <UpNetwPictureWINs />
> > <UpPrintPicture />
> > <MidPicture />
> > <DownPicture />
> > <DownExchPicture />
> > <DownBackPicture />
> > <DownNetwPictureDNS />
> > <DownNetwPictureWINs />
> > <DownPrintPicture />
> > <DownSound />
> > <UpSound />
> > <CompleteSound />
> > </AdvancedOptions>
> > </Options>
>
>



Re: Help with XML function by E

E
Thu Mar 01 10:54:20 CST 2007

Cool, I can include this in the sub. Is their a way to check to see if an
element actually exists? Then this will be done :-).

Anthony Jones wrote:
> "Anthony Jones" <Ant@yadayadayada.com> wrote in message
> news:uEH4YI%23WHHA.4252@TK2MSFTNGP06.phx.gbl...
> message
> if
> work
>
> Oops those last AddElems should have a third parameter Null. (I wish
> VBScript had allow for Optional params in it's procedures)
>
> --
>
>

Re: Help with XML function by E

E
Thu Mar 01 12:14:23 CST 2007

Question, where is roParent defined. I am trying to play with the code. Ugh
Learning new stuff sucks. Playing with new stuff rules ;-). Thanks much.

Anthony Jones wrote:
> "Anthony Jones" <Ant@yadayadayada.com> wrote in message
> news:uEH4YI%23WHHA.4252@TK2MSFTNGP06.phx.gbl...
> message
> if
> work
>
> Oops those last AddElems should have a third parameter Null. (I wish
> VBScript had allow for Optional params in it's procedures)
>
> --
>
>

Re: Help with XML function by Anthony

Anthony
Thu Mar 01 12:17:34 CST 2007


"E C H (He of too much code)" <glasswalkertheurge@juno.com> wrote in message
news:et5zIJCXHHA.1036@TK2MSFTNGP03.phx.gbl...
> Cool, I can include this in the sub. Is their a way to check to see if an
> element actually exists? Then this will be done :-).

Function GetChild(roParent, rsName, rbCreate)
Dim elem : Set elem = oParent.selectSingleNode(rsName)
If Elem Is Nothing and rbCreate Then
Set elem = AddElem(roParent, rsName, Null)
End If
Set GetChild = Elem
End Function


GetChild(moRoot, "SomeItem", True).Text = SomeValue

The code above will assign SomeValue to an existing element called SomeItem.
If the element doesn't exist it will create one.







>
> Anthony Jones wrote:
> > "Anthony Jones" <Ant@yadayadayada.com> wrote in message
> > news:uEH4YI%23WHHA.4252@TK2MSFTNGP06.phx.gbl...
> > message
> > if
> > work
> >
> > Oops those last AddElems should have a third parameter Null. (I wish
> > VBScript had allow for Optional params in it's procedures)
> >
> > --
> >
> >



Re: Help with XML function by E

E
Thu Mar 01 21:02:13 CST 2007

Ok, I am fried. But, I am pretty close to being done with my sub. Take a look
at the code, if I am doing anything not "proper" (in keeping with standards or
whatever), let me know. Thanks everyone.

'CALL BELOW --------------------------------------------------------------
XMLCreateChild "/Options", "BasicOptions", "", ""
XMLCreateChild "/Options/BasicOptions", "Sound", "On_Error", "True"
XMLCreateChild "/Options/BasicOptions", "Sound", "On_Success", "False"
XMLCreateChild "/Options/BasicOptions", "Sound", "On_Complete", "True"

'SUB BELOW ---------------------------------------------------------------
Sub XMLCreateChild(strRootElement, strChildElement, strElementAtribute,
strElementValue)
Dim oChildNode, objCurrNode, strRootElementPath, objRootNode

strRootElementPath = strRootElement
Set objRootNode = xmlDoc.documentElement.selectSingleNode(strRootElementPath)
' --------------------------------------------
' add an element to the xml file...
' --------------------------------------------
Set oChildNode = xmlDoc.createElement(strChildElement)
objRootNode.appendChild oChildNode ' make child of root element

' ----------------------------------------------
' add an "attribute" to this child node...
' ----------------------------------------------
If Not strElementAtribute = "" Then
oChildNode.setAttribute strElementAtribute, strElementValue
End If
End Sub


E C H (He of too much code) wrote:
> I have mercilessly hacked previous XML code, I am trying to create a
> function that will create a child node. Which this function does. What
> I would really like is to be able to create a child of a particular
> element. Nothing I have goggled seems very clear. I would also like to
> be able to check to see if the element listed as the root element
> exists. My problem is, that I can work well with code (not with fancy
> explanations, I gotta see it work to figure it out). Any help would be
> greatly appreciated.
<SNIP>

Re: Help with XML function by Anthony

Anthony
Fri Mar 02 04:30:45 CST 2007


"E C H (He of too much code)" <glasswalkertheurge@juno.com> wrote in message
news:O73Z1cHXHHA.528@TK2MSFTNGP03.phx.gbl...
> Ok, I am fried. But, I am pretty close to being done with my sub. Take a
look
> at the code, if I am doing anything not "proper" (in keeping with
standards or
> whatever), let me know. Thanks everyone.
>
> 'CALL BELOW --------------------------------------------------------------
> XMLCreateChild "/Options", "BasicOptions", "", ""
> XMLCreateChild "/Options/BasicOptions", "Sound", "On_Error", "True"
> XMLCreateChild "/Options/BasicOptions", "Sound", "On_Success", "False"
> XMLCreateChild "/Options/BasicOptions", "Sound", "On_Complete", "True"
>
> 'SUB BELOW ---------------------------------------------------------------
> Sub XMLCreateChild(strRootElement, strChildElement, strElementAtribute,
> strElementValue)
> Dim oChildNode, objCurrNode, strRootElementPath, objRootNode
>
> strRootElementPath = strRootElement
> Set objRootNode =
xmlDoc.documentElement.selectSingleNode(strRootElementPath)
> ' --------------------------------------------
> ' add an element to the xml file...
> ' --------------------------------------------
> Set oChildNode = xmlDoc.createElement(strChildElement)
> objRootNode.appendChild oChildNode ' make child of root element
>
> ' ----------------------------------------------
> ' add an "attribute" to this child node...
> ' ----------------------------------------------
> If Not strElementAtribute = "" Then
> oChildNode.setAttribute strElementAtribute, strElementValue
> End If
> End Sub

Have you tested this code to see what the output would be? Is it really
your intention to create multiple 'Sound' elements? OR did you want to
create a single 'Sound' element carrying the three attributes?

Two things that I would consider to be a problem. The function is coupled
to the fact that there is an external variable called xmlDoc upon which it
depends. This isn't good code construction when it can be avoided. Also
the code causes a search for a node "/Options/BasicOptions" three times
where one or even none would do.

Let's go back to my original function but this time I'll explain what it is
doing:-

Function AddElem(roParent, rsName, rvntValue)
Set AddElem = roParent.ownerDocument.createElement(rsName)
roParent.appendChild AddElem
If Not IsNull(rvntValue) AddElem.Text = rvntValue
End Function

The function takes an XMLDOMNode (that can be the DOMDocument itself or any
Element) as the roParent parameter. It uses the parent nodes ownerDocument
property to fetch the DOMDocument the owns the node. This de-couples the
function from having to aware of an external Document variable against which
it needs to call the createElement method.

It uses the second Name parameter (BTW, the r prefix indicates that the
identify is a ByRef parameter of the function) to create the element that is
to be returned as the output of the function.

It then appends the created node to the parent node.

If the third parameter is not null (I don't use an empty string because an
empty string is a valid value for an element) then the assign it to the
text content of the element just created.


Using this function then to do what you want to do above (assuming you
actually only want one 'Sound' element) is:-

Dim moDOM : Set oDOM = CreateObject("MSXML2.DOMDocument.3.0")

Dim moRoot, moBasicOptions

Set moRoot = AddElem(moDOM, "Options", Null)
Set moBasicOptions = AddElem(moRoot , "BasicOptions", Null)

With AddElem(moBasicOptions, "Sound", Null)
.setAttributes "On_Error", "True"
.setAttributes "On_Success", "False"
.setAttributes "On_Complete", "True"
End With


BTW on a question XML design (which is a little bit subjective to personal
preference). I would replace "BasicOptions" with just "Basic" after all
this node is already inside a node called "Options" there is no need to
restate that this node represents an option.









>
>
> E C H (He of too much code) wrote:
> > I have mercilessly hacked previous XML code, I am trying to create a
> > function that will create a child node. Which this function does. What
> > I would really like is to be able to create a child of a particular
> > element. Nothing I have goggled seems very clear. I would also like to
> > be able to check to see if the element listed as the root element
> > exists. My problem is, that I can work well with code (not with fancy
> > explanations, I gotta see it work to figure it out). Any help would be
> > greatly appreciated.
> <SNIP>



Re: Help with XML function by E

E
Fri Mar 02 08:45:47 CST 2007



Anthony Jones wrote:
> "E C H (He of too much code)" <glasswalkertheurge@juno.com> wrote in message
> news:O73Z1cHXHHA.528@TK2MSFTNGP03.phx.gbl...
> look
> standards or
> xmlDoc.documentElement.selectSingleNode(strRootElementPath)
>
> Have you tested this code to see what the output would be? Is it really
> your intention to create multiple 'Sound' elements? OR did you want to
> create a single 'Sound' element carrying the three attributes?
Yes, I have tested the code. Upon looking at it again, you are correct,
organizationally it doesn't make much sense. I just used this for testing, I
will be creating the XML file that I showed on a earlier post.
>
> Two things that I would consider to be a problem. The function is coupled
> to the fact that there is an external variable called xmlDoc upon which it
> depends. This isn't good code construction when it can be avoided. Also
> the code causes a search for a node "/Options/BasicOptions" three times
> where one or even none would do.
Agreed, this is a poor programming! But I have done it because it meets my
goals. It is in my opinion much simpler than creating a node with a separate
function. It is also easier (in my opinion) to understand.
>
> Let's go back to my original function but this time I'll explain what it is
> doing:-
>
> Function AddElem(roParent, rsName, rvntValue)
> Set AddElem = roParent.ownerDocument.createElement(rsName)
> roParent.appendChild AddElem
> If Not IsNull(rvntValue) AddElem.Text = rvntValue
> End Function
>
> The function takes an XMLDOMNode (that can be the DOMDocument itself or any
> Element) as the roParent parameter. It uses the parent nodes ownerDocument
> property to fetch the DOMDocument the owns the node. This de-couples the
> function from having to aware of an external Document variable against which
> it needs to call the createElement method.
Interesting, I will have to look into this. Looks like I will be keeping my
code though.
>
> It uses the second Name parameter (BTW, the r prefix indicates that the
> identify is a ByRef parameter of the function) to create the element that is
> to be returned as the output of the function.
That is an interesting idea, my naming scheme is a little different, I indicate
stuff like boo (boolean), num (integer), etc.
>
> It then appends the created node to the parent node.
This is my rub, I would like to dynamically create the whole deal with one
function. In my mind it makes more sense.
>
> If the third parameter is not null (I don't use an empty string because an
> empty string is a valid value for an element) then the assign it to the
> text content of the element just created.
Good call, I am going to switch to NULL. Although I can't think of any reason
at the moment to have "", might as well have the option.
>
>
> Using this function then to do what you want to do above (assuming you
> actually only want one 'Sound' element) is:-
>
> Dim moDOM : Set oDOM = CreateObject("MSXML2.DOMDocument.3.0")
>
> Dim moRoot, moBasicOptions
>
> Set moRoot = AddElem(moDOM, "Options", Null)
> Set moBasicOptions = AddElem(moRoot , "BasicOptions", Null)
>
> With AddElem(moBasicOptions, "Sound", Null)
> .setAttributes "On_Error", "True"
> .setAttributes "On_Success", "False"
> .setAttributes "On_Complete", "True"
> End With
I am going to be creating the structure from the previous XML file as stated.
Will make a function to read the values in easier.
>
>
> BTW on a question XML design (which is a little bit subjective to personal
> preference). I would replace "BasicOptions" with just "Basic" after all
> this node is already inside a node called "Options" there is no need to
> restate that this node represents an option.
Yes, I need to think more like XML (Option\Basic) makes much sense.

P.S. When I run the TidyXML function it removes "<?xml version="1.0"
encoding="windows-1252"?>", and I am at a loss. The code is below. Any ideas?

Sub TidyXML(filXMLnot, filXMLtidy)
Const ForReading = 1, ForWriting = 2
Const TristateTrue = -1, TristateFalse = 0, TristateUseDefault = -2
Const allowOverwrite = True
Dim asUnicode : asUnicode = TristateTrue ' opens as unicode
Dim asASCII : asASCII = TristateFalse ' opens as ascii
Dim asSysDefault : asSysDefault = TristateUseDefault ' opens using system default
Dim oFile, oTextStream ' as object(s)

Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")

Dim sXML : sXML = filXMLnot
Dim sTidyXML

' (tidy first step) place each element on a new line
sTidyXML = Replace(sXML, ">", ">" & vbCrLf)

' (tidy second step) indent each element
sTidyXML = Replace(sTidyXML, "<", " <")

' (third step) remove indent from rootnode tags...
sTidyXML = Replace(sTidyXML, " <?", "<?")
sTidyXML = Replace(sTidyXML, vbCrLf & vbCrLf, vbCrLf)
sTidyXML = Replace(sTidyXML, " <" & sRootNodeName, "<" & sRootNodeName)
sTidyXML = Replace(sTidyXML, " </" & sRootNodeName, "</" & sRootNodeName)

' lastly, change the comment...
'sTidyXML = Replace(sTidyXML, "(root comment) xml", "(root comment) TIDY xml")

sXMLTidySpec = filXMLtidy

' write out the tidy file...
objFSO.CreateTextFile filXMLtidy, allowOverwrite ' creates the file (no return)...
Set oXMLOutFile = objFSO.GetFile(filXMLtidy) ' "objectify" the file...

Set oTextStream = oXMLOutFile.OpenAsTextStream(ForWriting) ' , TristateUseDefault)
oTextStream.Write sTidyXML
oTextStream.Close

Set objFSO = nothing
End Sub

Re: Help with XML function by Anthony

Anthony
Fri Mar 02 15:32:29 CST 2007


"E C H (He of too much code)" <glasswalkertheurge@juno.com> wrote in message
news:ORNl$lNXHHA.4252@TK2MSFTNGP06.phx.gbl...
>
>
> Anthony Jones wrote:
> > "E C H (He of too much code)" <glasswalkertheurge@juno.com> wrote in
message
> > news:O73Z1cHXHHA.528@TK2MSFTNGP03.phx.gbl...
> > look
> > standards or
> > xmlDoc.documentElement.selectSingleNode(strRootElementPath)
> >
> > Have you tested this code to see what the output would be? Is it really
> > your intention to create multiple 'Sound' elements? OR did you want to
> > create a single 'Sound' element carrying the three attributes?

> Yes, I have tested the code. Upon looking at it again, you are correct,
> organizationally it doesn't make much sense. I just used this for
testing, I
> will be creating the XML file that I showed on a earlier post.



> >
> > Two things that I would consider to be a problem. The function is
coupled
> > to the fact that there is an external variable called xmlDoc upon which
it
> > depends. This isn't good code construction when it can be avoided.
Also
> > the code causes a search for a node "/Options/BasicOptions" three times
> > where one or even none would do.
> Agreed, this is a poor programming! But I have done it because it meets
my
> goals. It is in my opinion much simpler than creating a node with a
separate
> function. It is also easier (in my opinion) to understand.
> >
> > Let's go back to my original function but this time I'll explain what it
is
> > doing:-
> >
> > Function AddElem(roParent, rsName, rvntValue)
> > Set AddElem = roParent.ownerDocument.createElement(rsName)
> > roParent.appendChild AddElem
> > If Not IsNull(rvntValue) AddElem.Text = rvntValue
> > End Function

I've noticed this function is bugged in couple of places. Too much JScript
has lead to there not being a Then in the If. Also whilst a DOMDocument
node has an ownerDocument property it isn't self referencing it's simply
nothing so the function doesn't actually work for the root node.

I didn't notice this because my usual approach would be:-

Dim moDOM : Set oDOM = CreateObject("MSXML2.DOMDocument.3.0")
moDOM.loadXML "<options />"

moBasic = AddElem(moDOM.documentElement,"Basic", Null)


> >
> > The function takes an XMLDOMNode (that can be the DOMDocument itself or
any
> > Element) as the roParent parameter. It uses the parent nodes
ownerDocument
> > property to fetch the DOMDocument the owns the node. This de-couples
the
> > function from having to aware of an external Document variable against
which
> > it needs to call the createElement method.
> Interesting, I will have to look into this. Looks like I will be keeping
my
> code though.
> >
> > It uses the second Name parameter (BTW, the r prefix indicates that the
> > identify is a ByRef parameter of the function) to create the element
that is
> > to be returned as the output of the function.
> That is an interesting idea, my naming scheme is a little different, I
indicate
> stuff like boo (boolean), num (integer), etc.

That's good I do that too but since booleans, longs, objects and strings are
by far the most common types I simply use b, l, o and s. ;)

> >
> > It then appends the created node to the parent node.
> This is my rub, I would like to dynamically create the whole deal with one
> function. In my mind it makes more sense.
> >
> > If the third parameter is not null (I don't use an empty string because
an
> > empty string is a valid value for an element) then the assign it to the
> > text content of the element just created.
> Good call, I am going to switch to NULL. Although I can't think of any
reason
> at the moment to have "", might as well have the option.

If you happen to use this sort of code to generate HTML you will need to do
this to create empty elements where a closing tag is required. Eg. <script
src="theURLHere"></script>


> >
> >
> > Using this function then to do what you want to do above (assuming you
> > actually only want one 'Sound' element) is:-
> >
> > Dim moDOM : Set oDOM = CreateObject("MSXML2.DOMDocument.3.0")
> >
> > Dim moRoot, moBasicOptions
> >
> > Set moRoot = AddElem(moDOM, "Options", Null)
> > Set moBasicOptions = AddElem(moRoot , "BasicOptions", Null)
> >
> > With AddElem(moBasicOptions, "Sound", Null)
> > .setAttributes "On_Error", "True"
> > .setAttributes "On_Success", "False"
> > .setAttributes "On_Complete", "True"
> > End With
> I am going to be creating the structure from the previous XML file as
stated.
> Will make a function to read the values in easier.
> >
> >
> > BTW on a question XML design (which is a little bit subjective to
personal
> > preference). I would replace "BasicOptions" with just "Basic" after all
> > this node is already inside a node called "Options" there is no need to
> > restate that this node represents an option.
> Yes, I need to think more like XML (Option\Basic) makes much sense.
>
> P.S. When I run the TidyXML function it removes "<?xml version="1.0"
> encoding="windows-1252"?>", and I am at a loss. The code is below. Any
ideas?
>
> Sub TidyXML(filXMLnot, filXMLtidy)
> Const ForReading = 1, ForWriting = 2
> Const TristateTrue = -1, TristateFalse = 0, TristateUseDefault = -2
> Const allowOverwrite = True
> Dim asUnicode : asUnicode = TristateTrue ' opens as unicode
> Dim asASCII : asASCII = TristateFalse ' opens as ascii
> Dim asSysDefault : asSysDefault = TristateUseDefault ' opens using system
default
> Dim oFile, oTextStream ' as object(s)
>
> Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
>
> Dim sXML : sXML = filXMLnot

Why are you doing the above line??

> Dim sTidyXML
>
> ' (tidy first step) place each element on a new line
> sTidyXML = Replace(sXML, ">", ">" & vbCrLf)
>
> ' (tidy second step) indent each element
> sTidyXML = Replace(sTidyXML, "<", " <")
>
> ' (third step) remove indent from rootnode tags...
> sTidyXML = Replace(sTidyXML, " <?", "<?")
> sTidyXML = Replace(sTidyXML, vbCrLf & vbCrLf, vbCrLf)
> sTidyXML = Replace(sTidyXML, " <" & sRootNodeName, "<" & sRootNodeName)
> sTidyXML = Replace(sTidyXML, " </" & sRootNodeName, "</" &
sRootNodeName)
>
> ' lastly, change the comment...
> 'sTidyXML = Replace(sTidyXML, "(root comment) xml", "(root comment) TIDY
xml")
>
> sXMLTidySpec = filXMLtidy

Where is sXMLTidySpec defined??

>
> ' write out the tidy file...
> objFSO.CreateTextFile filXMLtidy, allowOverwrite ' creates the file (no
return)...
> Set oXMLOutFile = objFSO.GetFile(filXMLtidy) ' "objectify" the file...
>
> Set oTextStream = oXMLOutFile.OpenAsTextStream(ForWriting) ' ,
TristateUseDefault)
> oTextStream.Write sTidyXML
> oTextStream.Close
>
> Set objFSO = nothing
> End Sub


To be frank. Stop doing that. XML does not need to be created to please
the human eye. There are plenty of (free) tools that will display and even
edit XML contents without out all of the above malarky which will lead to
trouble.

To answer your question I suspect you've passed xmlDoc.XML to the first
parameter, when you do that the encoding in the XML is meaningless since the
XML string is always unicode.




Re: Help with XML function by E

E
Fri Mar 02 19:05:54 CST 2007

In the environment that this will be used, their are no XML editors. While it
would be easily viewed by IE ... nevermind. I don't think they should be
editing anyway. I will just stick with plain XML I think.

Anthony Jones wrote:
> "E C H (He of too much code)" <glasswalkertheurge@juno.com> wrote in message
<SNIP>
>
> To be frank. Stop doing that. XML does not need to be created to please
> the human eye. There are plenty of (free) tools that will display and even
> edit XML contents without out all of the above malarky which will lead to
> trouble.
>
> To answer your question I suspect you've passed xmlDoc.XML to the first
> parameter, when you do that the encoding in the XML is meaningless since the
> XML string is always unicode.
>
>
>