Results 1 to 8 of 8

Thread: Why my XML files is contain only one line of data?

  1. #1

    Thread Starter
    Hyperactive Member solar115's Avatar
    Join Date
    Apr 2002
    Location
    Thailand
    Posts
    268

    Unhappy Why my XML files is contain only one line of data?

    I code my program somthing like this...
    ObjXML.Load("my_data.xml")
    If ObjXML.ParseError.ErrorCode = 0 Then
    Set ObjRoot = ObjXML.CreateElement("poll_detail")
    ObjXML.DocumentElement.AppendChild ObjRoot
    Set ObjSub = Objxml.CreateElement("user_vote")
    ObjSub.Text = vVote & ""
    ObjRoot.AppendChild ObjSub
    Set ObjSub = Objxml.CreateElement("user_problem")
    ObjSub.Text = vProblem & ""
    ObjRoot.AppendChild ObjSub
    ObjXML.Save("my_data.xml")
    End If

    After run above code, My program will be do all process well but when I see my XML file (my_data.xml) all data be contained in one line like this

    <?xml version="1.0" encoding="windows-874"?>
    <poll_data><poll_detail><user_vote>5</user_vote><user_problem>nothing</user_problem><poll_detail></poll_detail><user_vote>4</user_vote><user_problem>OK</user_problem></poll_detail></poll_data>

    How can I code it for result like this

    <?xml version="1.0" encoding="windows-874"?>
    <poll_data>
    <poll_detail>
    <user_vote>5</user_vote>
    <user_problem>nothing</user_problem>
    </poll_detail>
    <poll_detail>
    <user_vote>4</user_vote>
    <user_problem>OK</user_problem>
    </poll_detail>
    </poll_data>
    ^solaris^

  2. #2
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    add a vbCrLf everytime you add a line (to add a new line)
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    vbCrLf probably won't be satisfactory since it will introduce unwanted characters into the xml.

    When you use IE, xmlSpy or some other tool that recognizes xml the unformmatted text won't matter - the code will be properly formatted by those tools. If you want to read it in a text editor then you can modify this code. It breaks up the xml into nodes that are all left-justified. That's not a perfect result, but it's the best that I've seen.
    VB Code:
    1. Public Sub PrettyPrint(Optional bVerifying As Boolean = False)
    2. '***************************************************************************
    3. 'Purpose: When nodes are added, they wind up on the same line. This does not
    4. '         affect how they are read by xml readers, but it does make them
    5. '         difficult to read in Word, etc. While this routine does not correct
    6. '         indenting, it does at least break up the run-on lines.
    7. 'Inputs:  The newly-modified xml file
    8. 'Outputs: The newly-modified xml file reformatted
    9. '***************************************************************************
    10.  
    11.     Dim intInFile As Integer
    12.     Dim intOutFile As Integer
    13.     Dim strLine As String
    14.     Dim strOutputLine As String
    15.     Dim strTempFile As String
    16.     Dim bDone As Boolean
    17.     Dim intPos As Integer
    18.    
    19.     On Error GoTo ErrorRoutine
    20.    
    21.     If Not gSave.AnyChanges Then
    22.         Exit Sub
    23.     End If
    24.  
    25.     strTempFile = TemporaryFileName
    26.    
    27.     intInFile = FreeFile
    28.     If bVerifying Then
    29.         Open gstrTempFileName For Input As intInFile
    30.     Else
    31.         Open gstrXMLFileName For Input As intInFile
    32.     End If
    33.    
    34.     intOutFile = FreeFile
    35.    
    36.     Open strTempFile For Output As intOutFile
    37.    
    38.     While Not EOF(intInFile)
    39.         Line Input #intInFile, strLine
    40.         bDone = False
    41.         Do Until bDone
    42.             intPos = InStr(1, strLine, "><")
    43.             If intPos = 0 Then
    44.                 bDone = True
    45.                 Print #intOutFile, strLine
    46.             Else
    47.                 strOutputLine = Left$(strLine, intPos)
    48.                 Print #intOutFile, strOutputLine
    49.                 strLine = Right$(strLine, Len(strLine) - intPos)
    50.             End If
    51.         Loop
    52.     Wend
    53.  
    54.     Close intInFile
    55.     Close intOutFile
    56.    
    57.     If bVerifying Then
    58.         FileCopy strTempFile, gstrTempFileName
    59.     Else
    60.         FileCopy strTempFile, gstrXMLFileName
    61.     End If
    62.    
    63.     Kill strTempFile
    64.    
    65.     Exit Sub
    66.  
    67. ErrorRoutine:
    68.  
    69.     DisplayError "PrettyPrint"
    70.  
    71. End Sub

  4. #4
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    Martin I think you might have some bugs in your coding. I'm just browsing it, but "TemporaryFileName" ???

    Should be obvious enough to fix though.

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    There are no bugs (at least none that have ever shown up). Note that I said that solar115 would need to modify the code. TemporaryFileName is a function that returns a unique, temporary, file name. Here is that function and the function it calls.
    VB Code:
    1. Public Function TemporaryFileName() As String
    2. '***************************************************************************
    3. 'Purpose: Call the routine that creates a unique temporary file name
    4. 'Inputs:  None
    5. 'Outputs: The temp file name
    6. '***************************************************************************
    7.  
    8.     On Error GoTo ErrorRoutine
    9.  
    10.     TemporaryFileName = gUtil.CreateTempFile("xml")
    11.    
    12.     Exit Function
    13.  
    14. ErrorRoutine:
    15.  
    16.     DisplayError "TemporaryFileName"
    17.  
    18. End Function
    19.  
    20. Public Function CreateTempFile(strPrefix As String) As String
    21. '***************************************************************************
    22. 'Purpose: Create a unique temporary file name
    23. 'Inputs:  strPrefix - The prefix to be used as the first part of the temp
    24. '         file name. Note: only the first 3 characters get used.
    25. 'Outputs: The temp file name
    26. '***************************************************************************
    27.  
    28.     Dim strTmpPath As String * 512
    29.     Dim strTmpName As String * 576
    30.     Dim intRet As Long
    31.    
    32.     intRet = GetTempPath(512, strTmpPath)
    33.     If (intRet > 0 And intRet < 512) Then
    34.        intRet = GetTempFileName(strTmpPath, strPrefix, 0, strTmpName)
    35.        If intRet <> 0 Then
    36.           CreateTempFile = Left$(strTmpName, _
    37.              InStr(strTmpName, vbNullChar) - 1)
    38.        End If
    39.     End If
    40.  
    41. End Function
    42.  
    43. Private Declare Function GetTempFileName Lib "kernel32" _
    44.          Alias "GetTempFileNameA" (ByVal lpszPath As String, _
    45.          ByVal lpPrefixString As String, ByVal wUnique As Long, _
    46.          ByVal lpTempFileName As String) As Long

  6. #6

    Thread Starter
    Hyperactive Member solar115's Avatar
    Join Date
    Apr 2002
    Location
    Thailand
    Posts
    268
    Hey All!

    I mean I want to correct my code when I append any data in XML file.
    ^solaris^

  7. #7

  8. #8

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width