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

VBScript creates one long line 1

Status
Not open for further replies.

jtokach

MIS
Jul 1, 2002
9
0
0
US
Hi there,

The following script produces one long line of tags.
Code:
<?xml version="1.0"?>
<rootElement><childElement1/><childElement2/></rootElement>

How do you format the output to look like this using the XLMDOM?
Code:
<?xml version="1.0"?>
<rootElement>
    <childElement1/>
    <childElement2/>
</rootElement>

Code:
 Dim objDom
 Dim objRoot
 Dim objChild1
 Dim objChild2
 Dim objPI


 Set objDom = CreateObject("Microsoft.XMLDOM")
 Set objRoot = objDom.createElement("rootElement")
 objDom.appendChild objRoot

 Set objChild1 = objDom.createElement("childElement1")
 objRoot.appendChild objChild1

 Set objChild2 = objDom.createElement("childElement2")
 objRoot.appendChild objChild2

 Set objPI = objDom.createProcessingInstruction("xml","version='1.0'")

 objDom.insertBefore objPI, objDom.childNodes(0)

 objDom.Save "c:\MyXMLDoc.xml"

Thanks!
 
There's no difference between them. They're both valid XML.

The one that "looks pretty" in fact takes up more memory and disk space because of all the unneeded whitespace in it.

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
Easy to do with XSL. Save as indent.xsl:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:template match="/">
    <xsl:copy-of select="."/>
  </xsl:template>
</xsl:stylesheet>
Then do transform:
Code:
Dim objDom
Dim objRoot
Dim objChild1
Dim objChild2
Dim objPI

Set objDom = CreateObject("Microsoft.XMLDOM")
Set objRoot = objDom.createElement("rootElement")
objDom.appendChild objRoot

Set objChild1 = objDom.createElement("childElement1")
objRoot.appendChild objChild1

Set objChild2 = objDom.createElement("childElement2")
objRoot.appendChild objChild2

Set objPI = objDom.createProcessingInstruction("xml","version='1.0'")

objDom.insertBefore objPI, objDom.childNodes(0)

'Load XSL
set xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load(Server.MapPath("indent.xsl"))

'Transform file
objDom.transformNode(xsl)

objDom.Save "c:\MyXMLDoc.xml"
 
Thanks JontyMC. This is the answer I was looking for. It needed quite a bit of modification on my side as I'm not doing this over HTTP, but all turned out well. Here's the basic mods I needed to make for those interested in the .VBS version. The XSL needed to be different. I don't know why it didn't work the other way, but I got this to work.

Code:
Dim objDom
Dim objRoot
Dim objChild1
Dim objChild2
Dim objPI

Set objDom = CreateObject("Microsoft.XMLDOM")
Set objRoot = objDom.createElement("rootElement")
objDom.appendChild objRoot

Set objChild1 = objDom.createElement("childElement1")
objRoot.appendChild objChild1

Set objChild2 = objDom.createElement("childElement2")
objRoot.appendChild objChild2

'Set objPI = objDom.createProcessingInstruction("xml","version='1.0'")

'objDom.insertBefore objPI, objDom.childNodes(0)

'Create and Load XSL
' <?xml version="1.0" encoding="UTF-8"?>
' <xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
'   <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
'   <xsl:template match="/ | @* | node()">
'     <xsl:copy>
' 		<xsl:apply-templates select="@* | node()" />
' 	</xsl:copy>
'   </xsl:template>
' </xsl:stylesheet>

Dim it
it = "<?xml version=" & chr(34) & "1.0" & chr(34) & " encoding=" & chr(34) & "UTF-8" & chr(34) & "?>" & vbcrlf
it = it & "<xsl:stylesheet version=" & chr(34) & "1.0" & chr(34) & " xmlns:xsl=" & chr(34) & "[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL] & chr(34) & ">" & vbcrlf
it =  it & "<xsl:output method=" & chr(34) & "xml" & chr(34) & " version=" & chr(34) & "1.0" & chr(34) & " encoding=" & chr(34) & "UTF-8" & chr(34) & " indent=" & chr(34) & "yes" & chr(34) & "/>" & vbcrlf
it =  it & "<xsl:template match=" & chr(34) & "/ | @* | node()" & chr(34) & ">" & vbcrlf
it =  it & "<xsl:copy>"& vbcrlf
it =  it & "<xsl:apply-templates select=" & chr(34) & "@* | node()" & chr(34) & " />"& vbcrlf
it =  it & "</xsl:copy>"& vbcrlf
it =  it & "</xsl:template>"& vbcrlf
it =  it & "</xsl:stylesheet>"

wscript.echo it

set xsl = CreateObject("Microsoft.XMLDOM")
xsl.async = false
xsl.loadXML(it)
wscript.echo "E " & err.number


'Transform file
'objDom.transformNode(xsl)
objDom.transformNodeToObject xsl, objDom

wscript.echo "E " & err.number

objDom.Save "c:\MyXMLDoc2.xml"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top