|
-
Jan 16th, 2003, 10:18 PM
#1
Thread Starter
Hyperactive Member
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>
-
Jan 16th, 2003, 11:13 PM
#2
The picture isn't missing
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  .
-
Jan 16th, 2003, 11:29 PM
#3
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:
Public Sub PrettyPrint(Optional bVerifying As Boolean = False)
'***************************************************************************
'Purpose: When nodes are added, they wind up on the same line. This does not
' affect how they are read by xml readers, but it does make them
' difficult to read in Word, etc. While this routine does not correct
' indenting, it does at least break up the run-on lines.
'Inputs: The newly-modified xml file
'Outputs: The newly-modified xml file reformatted
'***************************************************************************
Dim intInFile As Integer
Dim intOutFile As Integer
Dim strLine As String
Dim strOutputLine As String
Dim strTempFile As String
Dim bDone As Boolean
Dim intPos As Integer
On Error GoTo ErrorRoutine
If Not gSave.AnyChanges Then
Exit Sub
End If
strTempFile = TemporaryFileName
intInFile = FreeFile
If bVerifying Then
Open gstrTempFileName For Input As intInFile
Else
Open gstrXMLFileName For Input As intInFile
End If
intOutFile = FreeFile
Open strTempFile For Output As intOutFile
While Not EOF(intInFile)
Line Input #intInFile, strLine
bDone = False
Do Until bDone
intPos = InStr(1, strLine, "><")
If intPos = 0 Then
bDone = True
Print #intOutFile, strLine
Else
strOutputLine = Left$(strLine, intPos)
Print #intOutFile, strOutputLine
strLine = Right$(strLine, Len(strLine) - intPos)
End If
Loop
Wend
Close intInFile
Close intOutFile
If bVerifying Then
FileCopy strTempFile, gstrTempFileName
Else
FileCopy strTempFile, gstrXMLFileName
End If
Kill strTempFile
Exit Sub
ErrorRoutine:
DisplayError "PrettyPrint"
End Sub
-
Jan 16th, 2003, 11:40 PM
#4
Frenzied Member
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.
-
Jan 16th, 2003, 11:48 PM
#5
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:
Public Function TemporaryFileName() As String
'***************************************************************************
'Purpose: Call the routine that creates a unique temporary file name
'Inputs: None
'Outputs: The temp file name
'***************************************************************************
On Error GoTo ErrorRoutine
TemporaryFileName = gUtil.CreateTempFile("xml")
Exit Function
ErrorRoutine:
DisplayError "TemporaryFileName"
End Function
Public Function CreateTempFile(strPrefix As String) As String
'***************************************************************************
'Purpose: Create a unique temporary file name
'Inputs: strPrefix - The prefix to be used as the first part of the temp
' file name. Note: only the first 3 characters get used.
'Outputs: The temp file name
'***************************************************************************
Dim strTmpPath As String * 512
Dim strTmpName As String * 576
Dim intRet As Long
intRet = GetTempPath(512, strTmpPath)
If (intRet > 0 And intRet < 512) Then
intRet = GetTempFileName(strTmpPath, strPrefix, 0, strTmpName)
If intRet <> 0 Then
CreateTempFile = Left$(strTmpName, _
InStr(strTmpName, vbNullChar) - 1)
End If
End If
End Function
Private Declare Function GetTempFileName Lib "kernel32" _
Alias "GetTempFileNameA" (ByVal lpszPath As String, _
ByVal lpPrefixString As String, ByVal wUnique As Long, _
ByVal lpTempFileName As String) As Long
-
Jan 16th, 2003, 11:56 PM
#6
Thread Starter
Hyperactive Member
Hey All!
I mean I want to correct my code when I append any data in XML file.
-
Jan 16th, 2003, 11:58 PM
#7
-
Jan 16th, 2003, 11:59 PM
#8
I should say that I've searched high and low for a way to do it right and haven't been able to find a way. If you do, please let me know.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|