Results 1 to 15 of 15

Thread: [RESOLVED] Can I parse XML or psuedo parse it without the MSXML library?

  1. #1

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Resolved [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:
    1. <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

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    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.

  4. #4

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Can I parse XML or psuedo parse it without the MSXML library?

    Quote Originally Posted by techgnome View Post
    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.

    Quote Originally Posted by dilettante View Post
    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

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  5. #5

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Can I parse XML or psuedo parse it without the MSXML library?

    Duplicate
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  6. #6
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    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?

  7. #7

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Can I parse XML or psuedo parse it without the MSXML library?

    Quote Originally Posted by MarkT View Post
    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

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  8. #8
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    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

  9. #9

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    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

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  10. #10
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    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

  11. #11

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    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

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  12. #12
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Can I parse XML or psuedo parse it without the MSXML library?

    Can you attach a sample that isn't working?

  13. #13

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Can I parse XML or psuedo parse it without the MSXML library?

    Quote Originally Posted by MarkT View Post
    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

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  14. #14
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Can I parse XML or psuedo parse it without the MSXML library?

    Quote Originally Posted by weirddemon View Post
    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.

  15. #15

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Can I parse XML or psuedo parse it without the MSXML library?

    Quote Originally Posted by MarkT View Post
    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

    Quote 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
  •  



Click Here to Expand Forum to Full Width