Results 1 to 30 of 30

Thread: Reading an xml file in vb6.0

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2012
    Posts
    58

    Reading an xml file in vb6.0

    Can someone tell me how to read and get the value of of StreetNumber (1050) in the following xml file?

    <?xml version="1.0" encoding="utf-8"?>
    <Country xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <city>
    <index>1</index>
    <StreetName>Street name</StreetName>
    <StreetNumber>1050</StreetNumber>
    <status>Success</status>
    </city>
    </Country>

  2. #2
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,731

    Re: Reading an xml file in vb6.0

    theres many ways.
    if u just need to one string in a xml, I would recommend to just read the entire file into a string and do a search using instr
    by searching for "<StreetNumber>" and "</StreetNumber>" you will get the needed values to use Mid$()

    if its just the first one you could combine reading a file and check each line, using "Line Input"
    when found you break the reading.

  3. #3
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,259

    Re: Reading an xml file in vb6.0

    Quote Originally Posted by dimbil View Post
    Can someone tell me how to read and get the value of of StreetNumber (1050) in the following xml file?

    <?xml version="1.0" encoding="utf-8"?>
    <Country xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <city>
    <index>1</index>
    <StreetName>Street name</StreetName>
    <StreetNumber>1050</StreetNumber>
    <status>Success</status>
    </city>
    </Country>
    You have a broad set of COM-libs you can use to parse that conveniently:
    - either use one of the MS-XML-COM-libs which come preinstalled on your system,
    - or the XML-parser included in the RichClient-lib

    Though before posting any code (in case these results come from a "Web-Service", via an https-API)...

    Such WebApis usually offer you a choice, to retrieve the results either in XML- or in JSON-format.

    And it is the latter one, which is the more commonly used these days.
    (and yes, there's also several good JSON-parsers available, to parse such results in VB6).

    Olaf

  4. #4

    Thread Starter
    Member
    Join Date
    Nov 2012
    Posts
    58

    Re: Reading an xml file in vb6.0

    Quote Originally Posted by Schmidt View Post
    You have a broad set of COM-libs you can use to parse that conveniently:
    - either use one of the MS-XML-COM-libs which come preinstalled on your system,
    - or the XML-parser included in the RichClient-lib

    Though before posting any code (in case these results come from a "Web-Service", via an https-API)...

    Such WebApis usually offer you a choice, to retrieve the results either in XML- or in JSON-format.

    And it is the latter one, which is the more commonly used these days.
    (and yes, there's also several good JSON-parsers available, to parse such results in VB6).

    Olaf


    Do you have an example for this to send it to me please?

  5. #5
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,259

    Re: Reading an xml file in vb6.0

    Quote Originally Posted by dimbil View Post
    Do you have an example for this to send it to me please?
    Can you confirm a few things first?
    - is it a WebAPI you address, which gives you these results?
    - if yes, does it have a param-switch, which tells it to deliver the results as JSON?
    - if yes, could you make that "switch" - and post an example for such a JSON-result?

    Olaf

  6. #6

    Thread Starter
    Member
    Join Date
    Nov 2012
    Posts
    58

    Re: Reading an xml file in vb6.0

    Quote Originally Posted by Schmidt View Post
    Can you confirm a few things first?
    - is it a WebAPI you address, which gives you these results?
    - if yes, does it have a param-switch, which tells it to deliver the results as JSON?
    - if yes, could you make that "switch" - and post an example for such a JSON-result?

    Olaf
    Thank you dear Schmidt for your reply. I really need your help
    Yes, it is a Web API from which I get the result in xml format as it follows but I don't know where to look if it has a param-switch which tells it to deliver the results as JSON. Can you please tell me where to look for this param-switch?

    <?xml version="1.0" encoding="utf-8"?>
    <Country xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <city>
    <index>1</index>
    <StreetName>Beacon str</StreetName>
    <StreetNumber>1050</StreetNumber>
    <status>Success</status>
    </city>
    </Country>

  7. #7
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,259

    Re: Reading an xml file in vb6.0

    Quote Originally Posted by dimbil View Post
    I don't know where to look if it has a param-switch which tells it to deliver the results as JSON.
    What WebAPI are we talking about (a link to its documentation would be nice,
    to see whether it supports JSON-formatted-results).

    Olaf

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Reading an xml file in vb6.0

    Not really much to this if you XML documents are aways this simple:

    Code:
        'Reference to:  Microsoft XML, v3.0
        Dim Element As MSXML2.IXMLDOMElement
    
        With New MSXML2.DOMDocument
            .Load "sample.xml" 'Local file name here, could also be HTTP or HTTPS URL.
    
            'Note that despite the name, the getElementsByTagName() method returns a
            'node list and not an elements list.  So we must query for the element
            'interface of the node in order to call getElementsByTagName() as we
            'descend the tree:
            Set Element = .documentElement.getElementsByTagName("city")(0)
            MsgBox Element.getElementsByTagName("StreetNumber")(0).Text
        End With
    Or you could use slow and clunky XPath queries, which is what you generally find in old ASP and JavaScript examples.

  9. #9
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,169

    Re: Reading an xml file in vb6.0

    Quote Originally Posted by dilettante View Post
    Or you could use slow and clunky XPath queries, which is what you generally find in old ASP and JavaScript examples.
    Using XPath (and other query-like languages) is *the* way to go on any modern DOM traversing code, at least since jquery stormed the web world in 2006.

    Code:
        Dim Element As MSXML2.IXMLDOMElement
    
        With New MSXML2.DOMDocument
            .Load "d:\temp\aaa.xml"
            Set Element = .documentElement
        End With
        '--- XPath works if there is no "city" nor "StreetNumber" node
        For Each Element In Element.selectNodes("city/StreetNumber")
            MsgBox Element.Text
            Exit For
        Next
    cheers,
    </wqw>

  10. #10

    Thread Starter
    Member
    Join Date
    Nov 2012
    Posts
    58

    Re: Reading an xml file in vb6.0

    Quote Originally Posted by wqweto View Post
    Using XPath (and other query-like languages) is *the* way to go on any modern DOM traversing code, at least since jquery stormed the web world in 2006.

    Code:
        Dim Element As MSXML2.IXMLDOMElement
    
        With New MSXML2.DOMDocument
            .Load "d:\temp\aaa.xml"
            Set Element = .documentElement
        End With
        '--- XPath works if there is no "city" nor "StreetNumber" node
        For Each Element In Element.selectNodes("city/StreetNumber")
            MsgBox Element.Text
            Exit For
        Next
    cheers,
    </wqw>

    I am very sorry for disturbing you again dear wqweto,
    Can you please tell me how to save in an xml file the response that I get from REST API?

  11. #11
    Frenzied Member
    Join Date
    May 2014
    Location
    Kallithea Attikis, Greece
    Posts
    1,289

    Re: Reading an xml file in vb6.0

    use this code https://www.vbforums.com/attachment....1&d=1605384509

    (from this post https://www.vbforums.com/showthread....=1#post5499679)
    Add in module1.bas this code
    and in immediate mode execute Test2
    Code:
    Sub Test2()
    Dim xml$
    xml$ = Replace("<?xml version='1.0' encoding='utf-8'?><Country xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'><city><index>1</index><StreetName>Street name AAAAAAAAA</StreetName><StreetNumber>1050</StreetNumber><status>Success</status></city><city><index>2</index><StreetName>Street name BBBBBB</StreetName><StreetNumber>1051</StreetNumber><status>Success</status></city></Country>", "'", Chr$(34))
    Dim xmldata As New XmlMono2, c As Collection, child As XmlMono2
    xmldata.xml(xmldata) = xml$
    Set c = xmldata.GetListByTag("city")
    For Each child In c
        Debug.Print child.textFromChild("StreetName")
        Debug.Print child.textFromChild("index")
    Next
    End Sub
    Last edited by georgekar; Dec 25th, 2021 at 02:55 PM.

  12. #12
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Reading an xml file in vb6.0

    Pretty bad excuse to fall into the XPath trap. You can always:

    Code:
        'Reference to:  Microsoft XML, v6.0
        Dim Element As MSXML2.IXMLDOMElement
    
        With New MSXML2.DOMDocument60
            .Load "sample.xml" 'Local file name here, could also be HTTP or HTTPS URL.
            Set Element = .documentElement.getElementsByTagName("city")(0)
            If Element Is Nothing Then
                MsgBox "No city"
            Else
                Set Element = Element.getElementsByTagName("StreetNumber")(0)
                If Element Is Nothing Then
                    MsgBox "No StreetNumber"
                Else
                    MsgBox Element.Text
                End If
            End If
        End With

  13. #13
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,169

    Re: Reading an xml file in vb6.0

    XPath trap? Let’s then warn against the getElementsByTagName medical hazards too - you might get arthritis from typing until you finally get your any data out of a moderately nested xml response )

    cheers,
    </wqw>

  14. #14

    Thread Starter
    Member
    Join Date
    Nov 2012
    Posts
    58

    Re: Reading an xml file in vb6.0

    This is the link of API documentation but it is in Greek and I don't know if it can help you : file:///C:/myData/%CE%A4%CE%B5%CF%87%CE%BD%CE%B9%CE%BA%CE%AE%20%CF%80%CE%B5%CF%81%CE%B9%CE%B3%CF%81%CE%B1%CF%86%CE%AE% 20%CE%B4%CE%B9%CE%B5%CF%80%CE%B1%CF%86%CF%8E%CE%BD%20REST%20API%20%CE%B3%CE%B9%CE%B1%20%CE%B4%CE%B9% CE%B1%CE%B2%CE%AF%CE%B2%CE%B1%CF%83%CE%B7%20%CE%BA%CE%B1%CE%B9%20%CE%BB%CE%AE%CF%88%CE%B7%20%CE%B4%C E%B5%CE%B4%CE%BF%CE%BC%CE%AD%CE%BD%CF%89%CE%BD%20%CE%B3%CE%B9%CE%B1%20%CF%87%CF%81%CE%AE%CF%83%CF%84 %CE%B5%CF%82%20ERP.pdf
    Maybe if you tellme what exactly should I look for, I could help you to help me solving my problem.

    Thank you very much for your time!

  15. #15
    Frenzied Member
    Join Date
    May 2014
    Location
    Kallithea Attikis, Greece
    Posts
    1,289

    Re: Reading an xml file in vb6.0

    Greeks for dimbil:
    Φίλε έχεις μαύρα μεσάνυχτα από προγραμματισμό! Δεν σε σώνει τίποτα!

    Dude, you've know nothing about programming! Nothing saves you!

  16. #16
    Frenzied Member
    Join Date
    May 2014
    Location
    Kallithea Attikis, Greece
    Posts
    1,289

    Re: Reading an xml file in vb6.0

    The link from dimbil is this https://www.aade.gr/sites/default/fi...ficial_upd.pdf
    It is for my Digital Accounting and Tax Application (myDATA), the greek goverment system for taxes.
    I think dimbil is a novice programmer to work on this.

  17. #17

    Thread Starter
    Member
    Join Date
    Nov 2012
    Posts
    58

    Re: Reading an xml file in vb6.0

    Thank you for your wishes

  18. #18

    Thread Starter
    Member
    Join Date
    Nov 2012
    Posts
    58

    Re: Reading an xml file in vb6.0

    I am trying to learn a few things. If I was an expert I would not ask for help.
    All of us begin from somewhere

  19. #19
    Frenzied Member
    Join Date
    May 2014
    Location
    Kallithea Attikis, Greece
    Posts
    1,289

    Re: Reading an xml file in vb6.0

    You have a lot to learn, and practice before you play with whatever you imagine that you can code...
    So first you have to learn abouτ string encoding, then you have to learn how visual basic save a file, but before that, you have to learn what is a file.

    So what is a file (i ask something which is known for all files).

  20. #20

    Thread Starter
    Member
    Join Date
    Nov 2012
    Posts
    58

    Re: Reading an xml file in vb6.0

    Quote Originally Posted by georgekar View Post
    You have a lot to learn, and practice before you play with whatever you imagine that you can code...
    So first you have to learn abouτ string encoding, then you have to learn how visual basic save a file, but before that, you have to learn what is a file.

    So what is a file (i ask something which is known for all files).
    I know that I have to learn a lot.
    I am in the procedure concerning xml files and I wanted to learn a few things on it, that's why I asked you for help.
    Every time I face a problem I learn something new with help from others.
    I just want to know how to read the response from REST API in xml format like the following.
    I want to read the values of StreetName and StreetNumber using vb6.0 which will be different every time I send data to API and get the response from REST API.
    Can you help me on this please?


    <?xml version="1.0" encoding="utf-8"?>
    <Country xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <city>
    <index>1</index>
    <StreetName>Street name</StreetName>
    <StreetNumber>1050</StreetNumber>
    <status>Success</status>
    </city>
    </Country>

  21. #21

    Thread Starter
    Member
    Join Date
    Nov 2012
    Posts
    58

    Re: Reading an xml file in vb6.0

    Quote Originally Posted by dimbil View Post
    I know that I have to learn a lot.
    I am in the procedure concerning xml files and I wanted to learn a few things on it, that's why I asked you for help.
    Every time I face a problem I learn something new with help from others.
    I just want to know how to read the response from REST API in xml format like the following.
    I want to read the values of StreetName and StreetNumber using vb6.0 which will be different every time I send data to API and get the response from REST API.
    Can you help me on this please?


    <?xml version="1.0" encoding="utf-8"?>
    <Country xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <city>
    <index>1</index>
    <StreetName>Street name</StreetName>
    <StreetNumber>1050</StreetNumber>
    <status>Success</status>
    </city>
    </Country>

    I am sorry for disturbing you again.
    If I understand well (or am I wrong?), the REST API response is in memory in xml format (as above) and I don't know how to read the values of StreetName and StreetNumber.
    Can you please tell me how to read these values and save them in two separate variable of vb6.0?

  22. #22
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,731

    Re: Reading an xml file in vb6.0

    that is why u should follow my example.
    this because u learn more that way.
    if you use the other methods, u will never learn the details.
    and since you need to learn "a lot" you should start there, like everybody else started.

    usually when I put examples its ignored, because the most experienced members here gives u "smarter" examples,
    but in the same time they don't teach u as much. and thats is what Im always trying to do. but most of the time, if not always its ignored because "it require u to work for it"

    so stop trying to learn by using expert methods, and start from scratch with basic functions like:

    InStr
    Mid$
    Len/LenB
    Asc/AscW
    Chr/ChrW
    Line Input
    StrConv

    and all other basic functions that should be "primary" to learn FIRST.
    Last edited by baka; Dec 27th, 2021 at 05:13 AM.

  23. #23

    Thread Starter
    Member
    Join Date
    Nov 2012
    Posts
    58

    Re: Reading an xml file in vb6.0

    Quote Originally Posted by baka View Post
    that is why u should follow my example.
    this because u learn more that way.
    if you use the other methods, u will never learn the details.
    and since you need to learn "a lot" you should start there, like everybody else started.

    usually when I put examples its ignored, because the most experienced members here gives u "smarter" examples,
    but in the same time they don't teach u as much. and thats is what Im always trying to do. but most of the time, if not always its ignored because "it require u to work for it"

    so stop trying to learn by using expert methods, and start from scratch with basic functions like:

    InStr
    Mid$
    Len/LenB
    Asc/AscW
    Chr/ChrW
    Line Input
    StrConv

    and all other basic functions that should be "primary" to learn FIRST.

    Do you mean to use the following code that you have sent to me?
    I have learned the functions that you have sent to me.
    Will xml$ contains the different values every time I get the REST API response after sending data to the API?
    In your code I see that it contains the values as I have sent them to you.
    Which code should I add in module1.bas" ? The code from xmlMono2 that you have sent to me?

    In your example, I don't understand why do you use the Replace, is it possible to explain it to me please?



    Re: Reading an xml file in vb6.0
    use this code https://www.vbforums.com/attachment....1&d=1605384509

    (from this post https://www.vbforums.com/showthread....=1#post5499679)
    Add in module1.bas this code
    and in immediate mode execute Test2
    Code:

    Sub Test2()
    Dim xml$
    xml$ = Replace("<?xml version='1.0' encoding='utf-8'?><Country xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'><city><index>1</index><StreetName>Street name AAAAAAAAA</StreetName><StreetNumber>1050</StreetNumber><status>Success</status></city><city><index>2</index><StreetName>Street name BBBBBB</StreetName><StreetNumber>1051</StreetNumber><status>Success</status></city></Country>", "'", Chr$(34))
    Dim xmldata As New XmlMono2, c As Collection, child As XmlMono2
    xmldata.xml(xmldata) = xml$
    Set c = xmldata.GetListByTag("city")
    For Each child In c
    Debug.Print child.textFromChild("StreetName")
    Debug.Print child.textFromChild("index")
    Next
    End Sub

  24. #24
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,731

    Re: Reading an xml file in vb6.0

    I never sent u anything like that, u confuse with someone else.

    what I sent u is this:

    - open the .xml file using the "Open" method and read the entire file into a "string"
    - search the string using InStr
    - you will get numbers where the different text is found in the string (using InStr) and you can "copy" that part using Mid$ method.

    so what you need to do for now is:

    - Learn Open filename as Input as #freefile, with Line Input # and Close #
    so first you need to learn how to open and read a file

    - Learn Len() method
    this is good to know how to get the length of a string, that in many cases is good to have

    - Learn the InStr() method
    this method will return the position of a specific text by searching another text, this you will use to allocate StreetName

    - Learn the Mid$() method
    this method is used to copy parts of the text into a new text, so we can cut out parts that we want

    this should be fundamental and "a most" know if you want to learn how to program, otherwise it will always be confusing and you will always need someone else make coding for you.
    I shouldn't even need to do this post, this should be obvious and mandatory when you learn a language.

    (now you need to LEARN each one. AND NOT BY ASKING HERE, for each one, you search, theres thousands of examples for each method if you google or search vbforums)

    (and NO, I will not do it for you, so please no PM asking me to make it, YOU NEED TO LEARN yourself, otherwise pay someone else to do the coding if you don't want to learn)
    Last edited by baka; Dec 27th, 2021 at 05:50 AM.

  25. #25

    Thread Starter
    Member
    Join Date
    Nov 2012
    Posts
    58

    Re: Reading an xml file in vb6.0

    Quote Originally Posted by baka View Post
    I never sent u anything like that, u confuse with someone else.

    what I sent u is this:

    - open the .xml file using the "Open" method and read the entire file into a "string"
    - search the string using InStr
    - you will get numbers where the different text is found in the string (using InStr) and you can "copy" that part using Mid$ method.

    so what you need to do for now is:

    - Learn Open filename as Input as #freefile, with Line Input # and Close #
    so first you need to learn how to open and read a file

    - Learn Len() method
    this is good to know how to get the length of a string, that in many cases is good to have

    - Learn the InStr() method
    this method will return the position of a specific text by searching another text, this you will use to allocate StreetName

    - Learn the Mid$() method
    this method is used to copy parts of the text into a new text, so we can cut out parts that we want

    this should be fundamental and "a most" know if you want to learn how to program, otherwise it will always be confusing and you will always need someone else make coding for you.
    I shouldn't even need to do this post, this should be obvious and mandatory when you learn a language.

    (now you need to LEARN each one. AND NOT BY ASKING HERE, for each one, you the search, theres thousands of examples for each method)

    I am sorry for confusing you with someone else. I guess that I didn't send you the right question.
    I don't have the data in an xml file.
    I get a response from REST API in an xml format like the following.
    This response is in memory. I know how to use the above functions that you mentioned.
    My problem is how to read this response and maybe save it to a file or to a variable?
    Then it is easy to read each line and find what I want.
    Can you please tell me how to read this response which resides in memory?

    <?xml version="1.0" encoding="utf-8"?>
    <Country xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <city>
    <index>1</index>
    <StreetName>Street name</StreetName>
    <StreetNumber>1050</StreetNumber>
    <status>Success</status>
    </city>
    </Country>

  26. #26
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,731

    Re: Reading an xml file in vb6.0

    so its not about reading a xml file, but to read the memory or download a file.

    if so, you need to tell, what memory.
    usually we get that info from a website. if so, you need to learn how to "download" a file.
    theres many examples for that as well.

    first thing:
    - search how to download a file from a website.

    its illogical that you have a xml file in memory.

    so important that you write the RIGHT question.

  27. #27
    Frenzied Member
    Join Date
    May 2014
    Location
    Kallithea Attikis, Greece
    Posts
    1,289

    Re: Reading an xml file in vb6.0

    DimBil,
    I give you the solution: https://www.vbforums.com/showthread....=1#post5549786
    You don't understand the Replace() function. Ok that isn't bad. Lets explain:
    For my demo for you, I have to include the xml (in UTF16LE encoding) in one string. But inside xml there are some quotes (character code 34), the string delimiters for VB6. So I replace them with this "'" and for the demo I restore these " using a replace function. Is that clear?
    If you don't understand that, there is nothing else I can do, but to abandon you. Is that clear?

    An xml object is a pack of smaller xml object's. In a string format we say that is in serialized mode or type (pick either). But to walk through the xml object, you can do it the Baka's way, using string manipulation, or a ready made object which load the string and keep inside a tree of objects, each one is like the top one.
    For each tree there is a number of nodes. Each node has something, maybe just text, or another xml object, means another tree. For each node there are some attributes. So with xml node you can search by tag (the name of the node) and maybe you have to reject some of them based on attributes. For your example there are attributes in Country tag, like xmlns:xsi.

    My solution was very simple. Extract all city tags as separate xml objects in a collection (Vb6 collection, read about this). So for each "city" i read the text for specific tag (in the city).

    If you want to get every city as xml serialized as string, you can.
    But I suspect you have another problem, even you learn how to get "information", you have to realize how these can organized as records to process them as a batch in your program. Use arrays, use UDT, think about, depends of the process you want to apply to that data.

    GK from Greece

  28. #28
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,259

    Re: Reading an xml file in vb6.0

    @dimbil (in response to a PM)

    Your problem is indeed (as baka already pointed out), that you do not desribe your problem properly.

    It seems, your real problem is, how to "talk to the WebAPI".

    And for us to help with that, we need "something" to "play with" (e.g. a Link , which describes that WebAPI).

    You still did not really provide something useful in that regard - what you gave us (after georgekar decoded it for us),
    is the description of a greek Tax-WebAPI, which in no way matches with the result-XML you gave us.

    That's another part which is totally confusing for the guys in this thread, who are trying to help you.

    ------------------------------------------------------

    That said, all that remains (to show some "good-will", despite the confusing info we've got),
    is to show you a complete example, which incorporates the 3 "main-parts" in such a task:

    1) - how to prepare the Input-Params for an existing, well-documented WebAPI,
    which hands out a few address-infos, a bit similar to the ones you've shown in your XML-string.
    I've choosen the "search-for-places"-functionality, of the OpenStreatMap-WebAPI -
    which is described here: https://nominatim.org/release-docs/latest/api/Search/

    2) - how to use the (in 1, parameter-wise) prepared API-URL, to perform a http(s)-request,
    which will finally respond with an XML-String (so, we deal with that "in-memory",... no need for "a file")

    3) - how to feed the result-string we got from 2 into a proper XML-DOM-Object, to be able to parse the contents conveniently
    and without any decoding-errors (otherwise, when you parse XML "by hand", the probability to make decoding- or parsing-mistakes is very high).

    Please use the following code (which incorporates all 3 steps above) -
    as a reference when you have further questions - after studying it in detail.

    Code:
    Option Explicit
    
    Private Sub Form_Load()
      'step 1 - study the WebAPI first - and prepare a proper URL-string accordingly (param-wise)
      Dim sURL As String
          sURL = PrepareAPIURL("Eiffel Tower", "xml", True, "ca")
      Debug.Print sURL; vbLf
      
      'step 2 - pass the prepared URL to a little Helper, which performs the http(s)-request
      Dim sXML As String
          sXML = wget_String(sURL) 'this helper-routine will return a String in XML-format
      Debug.Print sXML; vbLf
      
      'step 3 - load the sXML-string into an XML-DOM-Object - and parse it according to its structure
      With CreateObject("MSXML2.DOMDocument")
          .LoadXML sXML 'load the sXML-String-Content into the XML-DOM-Object
          
          Dim Place As Object 'enumerate all Place-Elements via .selectNodes (plural)
          For Each Place In .selectNodes("searchresults/place")
              Dim Lat$, Lon$, State$, Country$
              'first we read two attributes out of the currently enumerated Place-Element
              Lat = Place.getAttribute("lat")
              Lon = Place.getAttribute("lon")
              
              'now we read two Sub-Node-Contents out of the currently enumerated Place-Element
              State = Place.selectSingleNode("state").Text
              Country = Place.selectSingleNode("country").Text
              
              Debug.Print State, Country, Lat, Lon
          Next
      End With
    End Sub
    
    'this is specific to a certain WebAPI (here: the openstreetmap-API one can usem to "search for places")
    'the documentation for it is here: https://nominatim.org/release-docs/latest/api/Search/
    Public Function PrepareAPIURL(Query$, Optional ReturnFormat$ = "xml", Optional ByVal InclAddressInfo As Boolean = True, Optional RestrictToCountries$) As String
      Const BaseURL = "https://nominatim.openstreetmap.org/search?format=" 'define the Base-URL for this API
      PrepareAPIURL = BaseURL & IIf(Len(ReturnFormat), ReturnFormat, "xml") '<- "json" would also be possible
      
      'prolong the URL about all the optional Parameters
      If InclAddressInfo Then PrepareAPIURL = PrepareAPIURL & "&addressdetails=1"
      If Len(RestrictToCountries) Then PrepareAPIURL = PrepareAPIURL & "&countrycodes=" & RestrictToCountries
      
      PrepareAPIURL = PrepareAPIURL & "&q=" & Query 'finally we append the users query-string
    End Function
    
    'this will perform a simple http(s) GET-request with a certain URL (returning the result as a String)
    Public Function wget_String(URL As String) As String
        Dim http As Object
        Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
            http.Open "GET", URL, 0: http.Send
        wget_String = http.ResponseText
    End Function
    HTH

    Olaf

  29. #29

    Thread Starter
    Member
    Join Date
    Nov 2012
    Posts
    58

    Re: Reading an xml file in vb6.0

    Quote Originally Posted by georgekar View Post
    DimBil,
    I give you the solution: https://www.vbforums.com/showthread....=1#post5549786
    You don't understand the Replace() function. Ok that isn't bad. Lets explain:
    For my demo for you, I have to include the xml (in UTF16LE encoding) in one string. But inside xml there are some quotes (character code 34), the string delimiters for VB6. So I replace them with this "'" and for the demo I restore these " using a replace function. Is that clear?
    If you don't understand that, there is nothing else I can do, but to abandon you. Is that clear?

    An xml object is a pack of smaller xml object's. In a string format we say that is in serialized mode or type (pick either). But to walk through the xml object, you can do it the Baka's way, using string manipulation, or a ready made object which load the string and keep inside a tree of objects, each one is like the top one.
    For each tree there is a number of nodes. Each node has something, maybe just text, or another xml object, means another tree. For each node there are some attributes. So with xml node you can search by tag (the name of the node) and maybe you have to reject some of them based on attributes. For your example there are attributes in Country tag, like xmlns:xsi.

    My solution was very simple. Extract all city tags as separate xml objects in a collection (Vb6 collection, read about this). So for each "city" i read the text for specific tag (in the city).

    If you want to get every city as xml serialized as string, you can.
    But I suspect you have another problem, even you learn how to get "information", you have to realize how these can organized as records to process them as a batch in your program. Use arrays, use UDT, think about, depends of the process you want to apply to that data.

    GK from Greece
    Thank you very much for your very good explanation and your time.
    I understand now and I think that I will make it.
    I wish you a nice day.
    I wish you a very HAPPY NEW YEAR with good health, joy and happiness !

  30. #30

    Thread Starter
    Member
    Join Date
    Nov 2012
    Posts
    58

    Re: Reading an xml file in vb6.0

    Quote Originally Posted by dimbil View Post
    Thank you very much for your very good explanation and your time.
    I understand now and I think that I will make it.
    I wish you a nice day.
    I wish you a very HAPPY NEW YEAR with good health, joy and happiness !
    Dear friendS !
    I am really very sorry for giving you hard time.
    I solved my problem,unfortunately I faced it in the wrong way at the beginning.
    Thank you very much for your great help !
    I wish to all of you and your familes HAPPY NEW YEAR !

Tags for this Thread

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