B4R Question Parsing a small xml string

Discussion in 'B4R Questions' started by Kevin, Jun 19, 2018.

Tags:
  1. Kevin

    Kevin Well-Known Member Licensed User

    I'm a bit confused on how you would parse part of a string, such as getting an element from an XML document. I still haven't decided on which source I want to get the current date & time from, but below is a perfect example of the question I am asking so that is what I am using for this post.

    For example, if I wanted to get the <timestamp> value in B4A or B4J, the simplest way to me would be to search for "<timestamp>", get the index of the start of that string and add the known length, then get the index of "</timestamp>" and use those values to get a substring from the XML document.

    However, I'm unsure how I would do this in B4R working with bytes. At least at this point, I don't feel like a have a good grasp on the concept of bytes. So if anyone could please show an example of how I would get this value in B4R, and in the most memory efficient way, I'd really appreciate it as it would help open the door to some other things I would like to do with my B4R project.

     
  2. Erel

    Erel Administrator Staff Member Licensed User

    The correct way in B4A, i or J is to use XmlSax or Xml2Map to parse XML strings.

    XML is not the best option when it comes to microcontrollers. Where does this data come from? How do you receive it?

    Bytes and strings in B4R are almost the same thing. You can use ByteConverter to parse strings or arrays of bytes.
     
    BillMeyer likes this.
  3. Kevin

    Kevin Well-Known Member Licensed User

    I understand that the method described isn't the proper way, but just that it is a quick way to grab an element from a small xml string - and as far as I know would be the only way in B4R.

    The xml would be downloaded from a server. Alternatively I could create a script on my website or home server to retrieve the xml and only return the desired element but I'd prefer to avoid using "middle men".

    If working with bytes, I assume the xml would need to be converted to a string first, correct? Or would a more proper way be to create the search terms as bytes and search for those?

    What throws me off is that I don't fully understand bytes. Is it always one byte per character, no matter what the character is?
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    No.

    In B4R there is no difference between a character and a byte. As long as you are working with ASCII characters then it will work fine.

    https://www.b4x.com/android/forum/threads/strings-and-bytes.66729/#content
     
    Kevin likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice