B4R Question Parsing a small xml string

Kevin

Well-Known Member
Licensed User
Longtime 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.

<result>
<status>OK</status>
<message/>
<countryCode>US</countryCode>
<countryName>United States</countryName>
<zoneName>America/Chicago</zoneName>
<abbreviation>CDT</abbreviation>
<gmtOffset>-18000</gmtOffset>
<dst>1</dst>
<dstStart>1520755200</dstStart>
<dstEnd>1541314799</dstEnd>
<nextAbbreviation>CST</nextAbbreviation>
<timestamp>1527884091</timestamp>
<formatted>2018-06-01 20:14:51</formatted>
</result>
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
value in B4A or B4J, the simplest way to me would be to search for "<timestamp>"
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.
 
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime 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?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0
Top