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

xml add vbcrlf 2

Status
Not open for further replies.

kennedymr2

Programmer
May 23, 2001
594
AU
Is there a simple way of adding vbcrlf to an .xml file, so that when i open it in say notepad.exe it will not just show as one big long string.

ie open it and write it out with vbcrlf

i do not seem to be able to work out what the delimiter in the xml file is.???


Appreciate any help


kennedymr2
 
Howdy,

I assume you create the XML via MSXML DOM?
If so, first create an XSL (e.g. "Indenter.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" indent="yes" encoding="UTF-8" />

<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

Then, in your XML creation code, add something like this:
Code:
Dim Indenter As MSXML2.DOMDocument60,outXml As MSXML2.DOMDocument60
Dim buf As String, XMLPath As String
'...
'...
Set Indenter = New MSXML2.DOMDocument60
Indenter.Load PathToYourIndenter_xsl
XMLPath=InXml.Url
    
    buf = inXml.transformNode(Indenter)
    Set outXml = New DOMDocument60
    With outXml
        .setProperty "ProhibitDTD", False
        .async = False
        .resolveExternals = False
        .validateOnParse = False
        .preserveWhiteSpace = True
        .loadXML buf
    End With
    Kill XMLPath
    outXml.save XMLPath

With "inXml" being your XML Object.
:)

Cheers,
MakeItSo

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
XML data is often exchanged in UTF-8 format (both because it is compact and you can often "pretend" ASCII or the 7-bit subset of an ANSI encoding is UTF-8 and get away with it) and with LF as a line delimiter (because of both Unix-chauvinism and the fact that LF is more compact than CRLF).

The newlines are often omitted entirely from XML markup because it adds overhead without meaning. Since XML is very often sent via (relatively) slow networks more compact is almost always better.


While XSL can be very powerful and flexible, it can also be dog slow because it is interpretive.

For tiny documents this does not matter, but if all you are after is pretty-printing compact XML there is a much higher performance option for this commonly used application. You can use SAX to do this with very little overhead, and it can handle data from a file or remote server as input as well as a DOM.

Here is a free-standing example that creates a dummy large compact XML file on disk, then reformats it to a new file.

Code:
Option Explicit

Private Const COMPACT As String = "compact.xml"
Private Const FORMATTED As String = "formatted.xml"

Private Sub FormatFileToFile(ByVal CompactFile As String, ByVal FormattedFile As String)
    'Reads, parses, and reformats the XML "CompactFile" into an ADODB.Stream and writes
    'it to "FormattedFile."
    '
    'Note the UTF-8 output never gets a BOM.  If we want one we have to write it
    'here explicitly after opening the Stream.
    Dim wrtFormatted As MSXML2.MXXMLWriter
    Dim stmFormatted As ADODB.Stream
    Dim rdrFormatted As MSXML2.SAXXMLReader

    Set stmFormatted = New ADODB.Stream
    With stmFormatted
        .open
        .Type = adTypeBinary
        Set wrtFormatted = New MSXML2.MXXMLWriter
        With wrtFormatted
            .omitXMLDeclaration = False
            .standalone = True
            .byteOrderMark = False 'If not set then .encoding is ignored.
            .encoding = "utf-8"    'Even if .byteOrderMark = True,
                                   'UTF-8 never gets a BOM.
            .indent = True
            .output = stmFormatted
            Set rdrFormatted = New MSXML2.SAXXMLReader
            With rdrFormatted
                Set .contentHandler = wrtFormatted
                Set .dtdHandler = wrtFormatted
                Set .errorHandler = wrtFormatted
                .putProperty "[URL unfurl="true"]http://xml.org/sax/properties/lexical-handler",[/URL] _
                             wrtFormatted
                .putProperty "[URL unfurl="true"]http://xml.org/sax/properties/declaration-handler",[/URL] _
                             wrtFormatted
                .parseURL CompactFile
            End With
        End With
        .SaveToFile FormattedFile
        .Close
    End With
End Sub

Private Sub MakeLargeXML(ByVal FileName As String)
    Dim F As Integer
    Dim Level1 As Integer
    Dim Level2 As Integer
    
    F = FreeFile(0)
    Open FileName For Output As #F
    'Note: We're actually writing ANSI here for simplicity but it will
    'be valid UTF-8 for our purposes.
    Print #F, "<?xml version=""1.0"" encoding=""utf-8"" standalone=""yes""?>";
    Print #F, "<document>";
    For Level1 = 1 To 5000
        Print #F, "<child instance="""; CStr(Level1); """>";
        For Level2 = 1 To 100
            Print #F, "<grandchild instance="""; CStr(Level2); """/>";
        Next
        Print #F, "</child>";
    Next
    Print #F, "</document>";
    Close #F
End Sub

Private Sub Main()
    'Skip making new compact XML file if we already have it.
    On Error Resume Next
    GetAttr COMPACT
    If Err Then
        On Error GoTo 0
        MakeLargeXML COMPACT
    End If
    On Error GoTo 0
    
    'Delete any existing formatted XML file before creating a new one.
    On Error Resume Next
    Kill FORMATTED
    On Error GoTo 0
    FormatFileToFile COMPACT, FORMATTED
    
    MsgBox "Done!"
End Sub
 
Thanks very much for the replies.
I appreciate the explanations.

What i was trying to achieve was...
Open an existing .xml within a vb6 program, in say notepad, and do a simple edit.

I have found a way of doing it as follows..
Private Sub Command1_Click()
URL = "c:\xw\Test.xml"
WebBrowser1.Navigate2 URL, navNoHistory
End Sub

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
Open App.Path & "\Test2.xml" For Output As #1
x = Replace(WebBrowser1.Document.documentElement.innerText, "-", "")
x = LTrim(x)
Print #1, x
Close #1
'Then open in notepad....
End Sub

This seems to work fine, just to get a result, but not really very professional.???

Appreciate the help

Regards Kennedymr2




 
That's nice dilettante!
I will surely try your SAX approach soon enough. Looks lean and mean and good.
[thumbsup2]

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
If you just want to open an xml file to modify it, XML notepad works pretty well.


In your vb6 app, you could shell out to XML Notepad to make it easier for your users to load the file.

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Looking back I wish I had named rdrFormatted something else like rdrCompact but at least it works. Sadly indenting is only done using TAB characters and sometimes you'd rather use something like 2 to 4 spaces. Ah well, you want fancy you use XSL or hand-rolled code I guess.

You'd think with Microsoft drooling on their shoes over XML so much there would be a standard, basic, XML editor shipping in Windows, wouldn't you? After all they imply XML as an alternative to INI format for settings, and that users should hand-edit the stuff. So why not a decent editor for XML that at least handles formatting and +/- hierarchy collapsing?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top