Alpha Software Mobile Development Tools:   Alpha Anywhere    |   Alpha TransForm subscribe to our YouTube Channel  Follow Us on LinkedIn  Follow Us on Twitter  Follow Us on Facebook

Announcement

Collapse

The Alpha Software Forum Participation Guidelines

The Alpha Software Forum is a free forum created for Alpha Software Developer Community to ask for help, exchange ideas, and share solutions. Alpha Software strives to create an environment where all members of the community can feel safe to participate. In order to ensure the Alpha Software Forum is a place where all feel welcome, forum participants are expected to behave as follows:
  • Be professional in your conduct
  • Be kind to others
  • Be constructive when giving feedback
  • Be open to new ideas and suggestions
  • Stay on topic


Be sure all comments and threads you post are respectful. Posts that contain any of the following content will be considered a violation of your agreement as a member of the Alpha Software Forum Community and will be moderated:
  • Spam.
  • Vulgar language.
  • Quotes from private conversations without permission, including pricing and other sales related discussions.
  • Personal attacks, insults, or subtle put-downs.
  • Harassment, bullying, threatening, mocking, shaming, or deriding anyone.
  • Sexist, racist, homophobic, transphobic, ableist, or otherwise discriminatory jokes and language.
  • Sexually explicit or violent material, links, or language.
  • Pirated, hacked, or copyright-infringing material.
  • Encouraging of others to engage in the above behaviors.


If a thread or post is found to contain any of the content outlined above, a moderator may choose to take one of the following actions:
  • Remove the Post or Thread - the content is removed from the forum.
  • Place the User in Moderation - all posts and new threads must be approved by a moderator before they are posted.
  • Temporarily Ban the User - user is banned from forum for a period of time.
  • Permanently Ban the User - user is permanently banned from the forum.


Moderators may also rename posts and threads if they are too generic or do not property reflect the content.

Moderators may move threads if they have been posted in the incorrect forum.

Threads/Posts questioning specific moderator decisions or actions (such as "why was a user banned?") are not allowed and will be removed.

The owners of Alpha Software Corporation (Forum Owner) reserve the right to remove, edit, move, or close any thread for any reason; or ban any forum member without notice, reason, or explanation.

Community members are encouraged to click the "Report Post" icon in the lower left of a given post if they feel the post is in violation of the rules. This will alert the Moderators to take a look.

Alpha Software Corporation may amend the guidelines from time to time and may also vary the procedures it sets out where appropriate in a particular case. Your agreement to comply with the guidelines will be deemed agreement to any changes to it.



Bonus TIPS for Successful Posting

Try a Search First
It is highly recommended that a Search be done on your topic before posting, as many questions have been answered in prior posts. As with any search engine, the shorter the search term, the more "hits" will be returned, but the more specific the search term is, the greater the relevance of those "hits". Searching for "table" might well return every message on the board while "tablesum" would greatly restrict the number of messages returned.

When you do post
First, make sure you are posting your question in the correct forum. For example, if you post an issue regarding Desktop applications on the Mobile & Browser Applications board , not only will your question not be seen by the appropriate audience, it may also be removed or relocated.

The more detail you provide about your problem or question, the more likely someone is to understand your request and be able to help. A sample database with a minimum of records (and its support files, zipped together) will make it much easier to diagnose issues with your application. Screen shots of error messages are especially helpful.

When explaining how to reproduce your problem, please be as detailed as possible. Describe every step, click-by-click and keypress-by-keypress. Otherwise when others try to duplicate your problem, they may do something slightly different and end up with different results.

A note about attachments
You may only attach one file to each message. Attachment file size is limited to 2MB. If you need to include several files, you may do so by zipping them into a single archive.

If you forgot to attach your files to your post, please do NOT create a new thread. Instead, reply to your original message and attach the file there.

When attaching screen shots, it is best to attach an image file (.BMP, .JPG, .GIF, .PNG, etc.) or a zip file of several images, as opposed to a Word document containing the screen shots. Because Word documents are prone to viruses, many message board users will not open your Word file, therefore limiting their ability to help you.

Similarly, if you are uploading a zipped archive, you should simply create a .ZIP file and not a self-extracting .EXE as many users will not run your EXE file.
See more
See less

Help Parsing XML File in Xbasic

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Help Parsing XML File in Xbasic

    I'm new to using xml files and the Xbasic that's used to mess with them and I need a little help.

    What I'm trying to do is prepare a pdf file for a user to download. The actual long filename is a decoded hexadecimal path, the short filename being the hexadecimal equivalent to the version id number....USUALLY. However; sometimes (when that file doesn't exist) I'll have to look for the version ref number. Below is an example of when the version id number is not the proper file name.


    Lets say I'm looking for the version 2 file. Well, the file path would end in "00000002.pdf". Unfortunately, that file doesn't actually exist so I will need to search the xml file to find out the "referenced file"

    Here's an example of an xml file:
    Code:
    <?xml version="1.0"?>
    
    -<index version="9.0">
    
    <filename>Part1.pdf</filename>
    
    <version date="2015-07-08 15:05:20" uid="210449" id="1"/>
    
    <version date="2015-07-08 15:05:22" uid="210521" id="2" ref="1"/>
    
    </index>
    So, I need to sort through that and find out that even though I'm on version id 2, it's referencing version 1 of the file and the ending file path should really be "00000001.pdf"

    It can get a little more complicated. Here is the XML file for a file that had a version 3 but was then "rolled back" to a version 2..

    Code:
      <?xml version="1.0" ?> 
    - <index version="9.0">
      <filename>Part1.pdf</filename> 
      <version id="1" uid="210449" date="2015-07-08 15:05:20" /> 
      <version id="2" ref="1" uid="210521" date="2015-07-08 15:05:22" /> 
      <version id="3" uid="212007" date="2015-07-08 15:12:48" /> 
      </index>
    You can see that the reference is on the second to last instead of the last node

    Basically, I need to search for the last occurrence of "version ref" in my xml file, read the value in it, and return that to my Xbasic.

    Here was my first rendition, which I found worked in the case of the first xml file above but didn't work in the case of the second Xml file above because of how I was trying to find the reference number.

    Code:
    	dim jscmd as c
    	DIM requester as C = a5ws_getcurrentuser()
    	dim fileloc AS c = e.HexPath
    	dim status AS c = e.Status
    	dim fnam AS c = word(e.Filename,1,".")
    	dim fext AS c = word(e.Filename,2,".")
    	dim outfolder AS c = "C:\A5Webroot\DBW_Downloads"
    	dim outfile AS c
    
    	
    
    
    if file.exists(fileloc) = .f. then
    	fileloc = left(fileloc,len(fileloc) - len(lastword(fileloc,chr(92))))
    	sm = xmlSchemaManager.Get()
    	xml = get_from_file(fileloc +"index.xml")
    	dom = sm.LoadXML(xml)
    	dim ele as n = dom.all.size()
    		if variable_exists("dom.all["+ele+"].attribute[2].Name") then
    			if dom.all[ele].attribute[2].Name = "ref" then
    				dim hxnm as n = Convert_Type(dom.all[ele].attribute[2].Value,"N")
    				fileloc = fileloc + *to_hex(hxnm) + "." + fext
    			end if
    		end if
    end if
    There's probably a really easy way to do this and I'm just missing it.

    Any help is greatly appreciated.

    Thanks!

    #2
    Re: Help Parsing XML File in Xbasic



    No bites?

    Is my explanation unclear? Not enough info?

    Comment


      #3
      Re: Help Parsing XML File in Xbasic

      http://wiki.alphasoftware.com/Parsin...with+Xbasic+V9
      Alpha Anywhere latest pre-release

      Comment


        #4
        Re: Help Parsing XML File in Xbasic

        The xbasic string functions are pretty strong and fast.

        I would attack this by using the file methods and read each line. You can find if the current line has the ref value with extract_string(txt," ref="," uid="). When you're on that line you can extract the uid with extract_string(txt," uid="," date") or the referenced version with extract_string(txt,"<version id="," ref"). Not clear on which you need.

        Something like

        Code:
        needed_part = ""
        fil = file.open(fileloc +"index.xml",FILE_RW_EXCLUSIVE)
        while .not. fil.eof()
        	ln = fil.readl_ine()
        	if extract_string(ln," ref="," uid=") > ""
        		needed_part = extract_string(txt,"<version id="," ref")
        	end if
        	if needed_part > ""
        		end
        	end if
        end while
        fil.close()
        There can be only one.

        Comment


          #5
          Re: Help Parsing XML File in Xbasic

          Thanks Sarah, I looked at that and that's how I got as far as I did with parsing the xml file; however, I'm having a hard time figuring out how to use the more complex functions described in that documentation. I think I'm just not seeing eye to eye with the way the examples are written.

          Originally posted by Stan Mathews View Post
          The xbasic string functions are pretty strong and fast.

          I would attack this by using the file methods and read each line. You can find if the current line has the ref value with extract_string(txt," ref="," uid="). When you're on that line you can extract the uid with extract_string(txt," uid="," date") or the referenced version with extract_string(txt,"<version id="," ref"). Not clear on which you need.

          Something like

          Code:
          needed_part = ""
          fil = file.open(fileloc +"index.xml",FILE_RW_EXCLUSIVE)
          while .not. fil.eof()
          	ln = fil.readl_ine()
          	if extract_string(ln," ref="," uid=") > ""
          		needed_part = extract_string(txt,"<version id="," ref")
          	end if
          	if needed_part > ""
          		end
          	end if
          end while
          fil.close()
          Thanks Stan,
          That may be the ticket. For some reason I was fixated on the fact that it was an XML file instead of just treating it as text. I'll see if this works for me. I may have to run it in reverse, so to speak, since upon the file not existing I must look for the most recent line with a "ref" variable and then read that variable (or whatever you call it in an xml file)

          Comment


            #6
            Re: Help Parsing XML File in Xbasic

            Not sure if you can run it in reverse but if you take out

            if needed_part > ""
            end
            end if

            then needed_part will take on the value of the last line where extract_string(ln," ref="," uid=") > ""

            Would that be the one you want?
            There can be only one.

            Comment


              #7
              Re: Help Parsing XML File in Xbasic

              Originally posted by Stan Mathews View Post
              Not sure if you can run it in reverse but if you take out

              if needed_part > ""
              end
              end if

              then needed_part will take on the value of the last line where extract_string(ln," ref="," uid=") > ""

              Would that be the one you want?
              Yup, I think so. I'll give it a try. I'm editing my Xbasic right now.

              Comment


                #8
                Re: Help Parsing XML File in Xbasic

                fil.read_line() reads the file like this:

                Code:
                <?xml version="1.0" ?> 
                <index version="9.0"><filename>Part1.pdf</filename><version id="1" uid="210449" date="2015-07-08 15:05:20"/><version id="2" ref="1" uid="210521" date="2015-07-08 15:05:22"/><version id="3" uid="212007" date="2015-07-08 15:12:48"/></index>
                So, it seems to think that "<?xml version="1.0" ?> " is one line, but that the rest of the file is one line. So, it only sees it as being two lines. That's throwing a wrench in the works. Otherwise it would work. Trying to figure out a solution now.

                Comment


                  #9
                  Re: Help Parsing XML File in Xbasic

                  fil.read_line() should read start to crlf(). Not well versed in xml structure. Actually current line offset position to crlf() as you are able to read/reposition in byte increments as well.
                  There can be only one.

                  Comment


                    #10
                    Re: Help Parsing XML File in Xbasic

                    Originally posted by Stan Mathews View Post
                    fil.read_line() should read start to crlf(). Not well versed in xml structure. Actually current line offset position to crlf() as you are able to read/reposition in byte increments as well.
                    For some reason the file only appears to have one crlf in it though it looks to have more. It appears to be that when I double click to open it, the browser recognizes it as XML and makes it look to have line breaks, but if I open it in a text editor, it only has one. So, my bad for posting an incorrect format. The file is actually a two line file.

                    Comment


                      #11
                      Re: Help Parsing XML File in Xbasic

                      I think I figured out something that works:

                      Code:
                      if file.exists(fileloc) = .f. then
                      	fileloc = left(fileloc,len(fileloc) - len(lastword(fileloc,chr(92))))
                      	sm = xmlSchemaManager.Get()
                      	xml = get_from_file(fileloc +"index.xml")
                      	dom = sm.LoadXML(xml)
                      		for i = 1 to dom.all.size()
                      			if variable_exists("dom.all["+i+"].attribute[2].Name")
                      				if dom.all[i].attribute[2].Name = "ref" then
                      					dim hxnm as n = Convert_Type(dom.all[i].attribute[2].Value,"N")
                      					fileloc = fileloc + *to_hex(hxnm) + "." + fext
                      				end if
                      			end if
                      		next
                      end if
                      There's probably a much more refined method for doing this but my brain has been in slow motion since an hour ago.
                      Last edited by -Jinx-; 07-09-2015, 04:22 PM.

                      Comment


                        #12
                        Re: Help Parsing XML File in Xbasic

                        Try getting the xml into a variable as you originally did.
                        xml = get_from_file(fileloc +"index.xml")

                        Then use extract_all_strings()
                        txt = extract_all_strings(xml,"<",">")

                        Then save that to a text file and process it.
                        There can be only one.

                        Comment


                          #13
                          Re: Help Parsing XML File in Xbasic

                          There's a QueryElement function you can use to query the xml document like a SQL table. The "all" array also has a function called AttributeGet, which you can use to get the 'ref' value. Here's a short test with an example (note, I changed the value of ref in each "file" so you could see the different output):
                          Code:
                          dim file as c =<<%xml%
                            <?xml version="1.0" ?> 
                          - <index version="9.0">
                            <filename>Part1.pdf</filename> 
                            <version id="1" uid="210449" date="2015-07-08 15:05:20" /> 
                            <version id="2" ref="3" uid="210521" date="2015-07-08 15:05:22" /> 
                            <version id="3" uid="212007" date="2015-07-08 15:12:48" /> 
                            </index>
                          %xml%
                          
                          dim file2 as c =<<%xml%
                          <?xml version="1.0"?>
                          -<index version="9.0">
                          <filename>Part1.pdf</filename>
                          <version date="2015-07-08 15:05:20" uid="210449" id="1"/>
                          <version date="2015-07-08 15:05:22" uid="210521" id="2" ref="12"/>
                          </index>
                          %xml%
                          
                          dom = *XML_Document(file)
                          dom2 = *XML_Document(file2)
                          
                          
                          ? dom.QueryElement("*tag = 'version' .and. ref <> ''").all[1].AttributeGet("ref")
                          = "3"
                          
                          ? dom2.QueryElement("*tag = 'version' .and. ref <> ''").all[1].AttributeGet("ref")
                          = "12"
                          For files with multiple versions with "ref", just loop over all the entries in all - probably want to cache that in a variable. You can get the values of the other attributes, like date or uid, using the AttributeGet function as well. QueryElement uses syntax similar to XBasic Filter Syntax - probably with the exception that you need the * before tag.

                          Good luck
                          Last edited by TheSmitchell; 07-09-2015, 07:27 PM.
                          Alpha Anywhere latest pre-release

                          Comment


                            #14
                            Re: Help Parsing XML File in Xbasic

                            Thanks Sarah!

                            I'll tinker around with that later and see if it's a better solution for me. Why is it that when you give an example it's much more understandable than the documentation? Haha.

                            Comment


                              #15
                              Re: Help Parsing XML File in Xbasic

                              Originally posted by -Jinx- View Post
                              Thanks Sarah!

                              I'll tinker around with that later and see if it's a better solution for me. Why is it that when you give an example it's much more understandable than the documentation? Haha.
                              'cause I know how to help you guys ;)

                              ...Also, I didn't write my example in blinding blue text on a white background. Haaaa.

                              FYI: In that link to the XML parsing article, there's a section titled "XML Parser Expression Reserved Words" with a table containing the list of words you can use in the QueryElement function.
                              Alpha Anywhere latest pre-release

                              Comment

                              Working...
                              X