|
-
Nov 14th, 2012, 05:10 PM
#1
[RESOLVED] Can I parse XML or psuedo parse it without the MSXML library?
I can't use any dependencies for the update I'm about to push. The last thing I need to do is read some XML from an XML file.
Here is an example of the XML:
XML Code:
<Car DateEntered="11/14/2012" Plate="TEST" State="TX" Type="TEST" Alert="default/5" Make="TEST" Year="1988" Description="TESTETSTETST"><Car>
That's pretty much it. Anyone have any ideas? After this, I should be completely done with VB6! Yay! Then I can start converting everything to C#
CodeBank contributions: Process Manager, Temp File Cleaner
 Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Nov 14th, 2012, 05:55 PM
#2
Re: Can I parse XML or psuedo parse it without the MSXML library?
that's it? jsut that one line? If that's all you need and that's all you have to deal with, just that one line... as long as it's consistent, and even if it isn't to some extent, you could probably use a regex pattern to get what you need out of it... no, I don't know how... but I know it's possible... regex isn't exactly in my wheelhouse... in fact it's not even in my boat... it's still sitting on the dock somewhere... I know it's there, that's about it... but... I'm sure there's someone around here who could probably whip that up faster than I could write this reply.
-tg
-
Nov 14th, 2012, 09:36 PM
#3
Re: Can I parse XML or psuedo parse it without the MSXML library?
If the source of this XML has a robust impementation rather than creating "pidgeon XML" then no simple parsing is going to be reliable. You need to observe a large subset of the rules to be successful.
Just consider a simple example: what if one of the attributes contains a quotation mark character? So you have to handle entities.
White space can be a blank space, CR, LF, CRLF, Tab, or a number of them in any combination.
XML can use any character encoding, though UTF-8 is the most common. But playing the "let's assume UTF-8 is ANSI" game leads to failure often enough we get a wail for help on that subject alone here once a month.
Unless you can use pidgeon XML (which implies you control all partners in the data exchange) you're pretty much going to need MSXML or a good 3rd party library. Targeting MSXML 3.0 is pretty safe as it is considered the "safe fallback" for applications that don't want to deploy 6.0, the current version.
Using the right version of MSXML in Internet Explorer applies to desktop applications as well as to IE scripting.
MSXML 3.0 is our preferred “fallback” - It is installed on every OS from a fully patched Win2k SP4 installation on up, so it requires “zero-deployment” and is serviced regularly with the OS.
-
Nov 15th, 2012, 09:13 AM
#4
Re: Can I parse XML or psuedo parse it without the MSXML library?
 Originally Posted by techgnome
that's it? jsut that one line? If that's all you need and that's all you have to deal with, just that one line... as long as it's consistent, and even if it isn't to some extent, you could probably use a regex pattern to get what you need out of it... no, I don't know how... but I know it's possible... regex isn't exactly in my wheelhouse... in fact it's not even in my boat... it's still sitting on the dock somewhere... I know it's there, that's about it... but... I'm sure there's someone around here who could probably whip that up faster than I could write this reply.
-tg
Well, yes and no. There will be more lines, but that's just for additional entries. Each entry will have only one line and only those properties. So it shouldn't be so hard, I think. It's just that VB6 in general makes things so hard and the fact that I can't use a library makes it even harder.
 Originally Posted by dilettante
If the source of this XML has a robust impementation rather than creating "pidgeon XML" then no simple parsing is going to be reliable. You need to observe a large subset of the rules to be successful.
Just consider a simple example: what if one of the attributes contains a quotation mark character? So you have to handle entities.
White space can be a blank space, CR, LF, CRLF, Tab, or a number of them in any combination.
XML can use any character encoding, though UTF-8 is the most common. But playing the "let's assume UTF-8 is ANSI" game leads to failure often enough we get a wail for help on that subject alone here once a month.
Unless you can use pidgeon XML (which implies you control all partners in the data exchange) you're pretty much going to need MSXML or a good 3rd party library. Targeting MSXML 3.0 is pretty safe as it is considered the "safe fallback" for applications that don't want to deploy 6.0, the current version.
Using the right version of MSXML in Internet Explorer applies to desktop applications as well as to IE scripting.
The XML is created by me. I control what is written and how it is written to the file. I just need a way to grab the data from that one line.
CodeBank contributions: Process Manager, Temp File Cleaner
 Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Nov 15th, 2012, 09:19 AM
#5
Re: Can I parse XML or psuedo parse it without the MSXML library?
CodeBank contributions: Process Manager, Temp File Cleaner
 Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Nov 15th, 2012, 09:25 AM
#6
Re: Can I parse XML or psuedo parse it without the MSXML library?
Will the data always have the same fields in the same order?
-
Nov 15th, 2012, 09:30 AM
#7
Re: Can I parse XML or psuedo parse it without the MSXML library?
 Originally Posted by MarkT
Will the data always have the same fields in the same order?
Yeah. For the most part. The data for each field will always be different. But the fields will always be in that order. This solution doesn't need to be perfect. It just needs to work. When I convert it to C#, I'll make it properly.
CodeBank contributions: Process Manager, Temp File Cleaner
 Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Nov 15th, 2012, 10:31 AM
#8
Re: Can I parse XML or psuedo parse it without the MSXML library?
Here is something quick and dirty for you to play with. You should be able to call GetLineData within a loop to parse the entire file.
Code:
Private Type xmlData
AttributeName As String
AttributeValue As String
End Type
Private Sub Command1_Click()
Dim xmlValues() As xmlData
Dim strXML As String
Dim i As Integer
strXML = "<Car DateEntered=""11/14/2012"" Plate=""TEST"" State=""TX"" Type=""TEST"" Alert=""default/5"" Make=""TEST"" Year=""1988"" Description=""TESTETSTETST""></Car>"
xmlValues = GetLineData(strXML)
For i = 0 To 7
Debug.Print xmlValues(i).AttributeName & vbTab & "-" & vbTab & xmlValues(i).AttributeValue
Next i
End Sub
Private Function GetLineData(ByVal xml As String) As xmlData()
Dim i As Integer
Dim xmlLineData(7) As xmlData
For i = 0 To 7
xmlLineData(i) = GetAttributeValue(i, xml)
Next i
GetLineData = xmlLineData
End Function
Private Function GetAttributeValue(ByVal AttributeIndex As Integer, ByVal xml As String) As xmlData
Dim intStartPos As Integer
Dim intEndPos As Integer
Dim intLen As Integer
Dim strFields(7) As String
Dim strValue As String
strFields(0) = "DateEntered="
strFields(1) = "Plate="
strFields(2) = "State="
strFields(3) = "Type="
strFields(4) = "Alert="
strFields(5) = "Make="
strFields(6) = "Year="
strFields(7) = "Description="
intStartPos = InStr(xml, strFields(AttributeIndex)) + Len(strFields(AttributeIndex)) + 1
If AttributeIndex < UBound(strFields) Then
intEndPos = InStr(intStartPos, xml, strFields(AttributeIndex + 1))
Else
intEndPos = InStr(intStartPos, xml, ">")
End If
intLen = intEndPos - intStartPos - 2
strValue = Mid(xml, intStartPos, intLen)
GetAttributeValue.AttributeName = Left(strFields(AttributeIndex), Len(strFields(AttributeIndex)) - 1)
GetAttributeValue.AttributeValue = strValue
End Function
-
Nov 15th, 2012, 10:51 AM
#9
Re: Can I parse XML or psuedo parse it without the MSXML library?
Thanks, Mark. That looks good. But I'm getting the following error on the provided line.
User-defined type not defined
Code:
Public Function GetLineData(ByVal xml As String) As xmlData()
I copied your code as is. I haven't changed anything. I wanted to test before I started setting up the loop and what not. xmlData is clearly defined, so I'm not sure why this is coming up.
Last edited by weirddemon; Nov 15th, 2012 at 10:56 AM.
CodeBank contributions: Process Manager, Temp File Cleaner
 Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Nov 15th, 2012, 10:58 AM
#10
Re: Can I parse XML or psuedo parse it without the MSXML library?
You would get that error if this part was missing from your general declaratons section.
Code:
Private Type xmlData
AttributeName As String
AttributeValue As String
End Type
-
Nov 15th, 2012, 11:08 AM
#11
Re: Can I parse XML or psuedo parse it without the MSXML library?
It's in the same module as the other methods. What do you mean by general declarations section?
CodeBank contributions: Process Manager, Temp File Cleaner
 Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Nov 15th, 2012, 11:09 AM
#12
Re: Can I parse XML or psuedo parse it without the MSXML library?
Can you attach a sample that isn't working?
-
Nov 15th, 2012, 11:14 AM
#13
Re: Can I parse XML or psuedo parse it without the MSXML library?
 Originally Posted by MarkT
Can you attach a sample that isn't working?
Err... this is strange. It won't even compile in the main app, but when I put the code in a test app, it runs just fine.
CodeBank contributions: Process Manager, Temp File Cleaner
 Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Nov 15th, 2012, 11:22 AM
#14
Re: Can I parse XML or psuedo parse it without the MSXML library?
 Originally Posted by weirddemon
What do you mean by general declarations section?
I found a quick explaination online
The General Declarations Section of a form or standard module. This section of a module is not labeled as such; it simply exists at the beginning of a code module, after the "Option Explicit" statement but prior to the first Sub or Function procedure. Declaring a variable here makes it a project-level variable (if the Public or Global keyword is used) or a module-level variable (if the Private or Dim keyword is used).
If you try to define the type outside of the general declarations section you will get a compiler error.
-
Nov 15th, 2012, 11:24 AM
#15
Re: Can I parse XML or psuedo parse it without the MSXML library?
 Originally Posted by MarkT
I found a quick explaination online
If you try to define the type outside of the general declarations section you will get a compiler error.
Oh. So declare it at the top. Gotcha. It's compiling now. Thanks, mark.
CodeBank contributions: Process Manager, Temp File Cleaner
 Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
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
|