0

I have a question regarding of reading xml files in delphi. I have read a few articles about it, but didn't find what I need anywhere, at least not that I'd notice. Meanwhile, I read the arcticle on How to read data from xml file and display it over the text box in delphi language

However, two things: first, in the first answer that Remy provided, I believe there is slightly mistaken code: Right after the "try", there is Vehicle := XMLDocument.DocumentElement; but I believe it should be XMLDocument1.DocumentElement instead. Am I right?
[sorry, can't post a comment since I don't have enough reputation.] secondly, I don't quite get how do I change which record to show. In the example above, there were two records of "vehicle" in the xml file, however I only see like it's only called once, so here's my question, how and where do I define which record/node to read?

To be more specific on what I actually need: I'm trying to read off a sms backup file, which is generated as an xml file, and has the structure as below:

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<?xml-stylesheet type="text/xsl" href="sms.xsl"?>
<smses count="3">
  <sms protocol="0" address="~number~" date="1385104712000" type="1" subject="null" body="Message 1 text" toa="145" sc_toa="0" service_center="ˇ~servicecenter~" read="1" status="-1" locked="0" date_sent="0" readable_date="Nov 22, 2013 8:18:32 AM" contact_name="My contact 1" />
  <sms protocol="0" address="~number~" date="1385104912000" type="2" subject="null" body="Message 2 text" toa="145" sc_toa="0" service_center="ˇ~servicecenter~" read="1" status="-1" locked="0" date_sent="0" readable_date="Nov 22, 2013 8:38:32 AM" contact_name="My contact 2" />
  <sms protocol="0" address="~number~" date="1385106412000" type="1" subject="null" body="Message 3 text" toa="145" sc_toa="0" service_center="ˇ~servicecenter~" read="1" status="-1" locked="0" date_sent="0" readable_date="Nov 22, 2013 9:18:32 AM" contact_name="My contact 1" />
</smses>

How could I read these correctly? Notice, that there might be repeatable "address" values. I'd like to add every different contact/number/"address" as an Listbox item, so I can select the contact, and later show the conversation via other elements..

I was thinking of reading the whole xml file to an array of record with length set by the first data in xml, , so I can later access this data directly from the program, as well as maybe write it to database, so the one could make/create organized database of all sms backup xml's he/she has...

With the database etc I don't expect any problems, just need to read the file to the memory first of all.

Any help would be appreciated. Thanks! :)

Community
  • 1
  • 1
That Marc
  • 1,134
  • 3
  • 19
  • 42
  • It depends on file size: if it is a reasonable small, you can open it as a sample in the XML Binding Wizard, create a bindings, load the file and traverse nodes/attributes to pick-up required values and insert them to ListBox.Items – pf1957 Jan 08 '14 at 07:41
  • The file size might be as big as several 10MB, as it may contain a few 10k od messages. I'd still load it in to a memory though, I don't think it's too big, even if 100MB, right? :) Anyway, so far I figured out on why there's smses "count" data; instead of ChildNodes[sms] I use ChildNodes[1] and so on, to read different nodes (I guess, based on the fact that 37 doesn't exists :D), however can't figure on how to read what's inside of a node. Smses.ChildNodes[1].ChildNodes["protocol"] doesn't raise errors but doesn't work either so I'm guessing that it's not child node inside of a child node? :/ – That Marc Jan 08 '14 at 17:56
  • protocol, adres, date and so on are attributes of the current childnode, so use the `Attribute['name of the attribute']` member to read them. ex: Smses.ChildNodes[1].Attribute['protocol'] – whosrdaddy Jan 08 '14 at 19:18
  • One weird idea: go Google Code and download "SuperObject" lib. It is a well known JOHN parser. The reader with examples is the only documentation but usually is enough. Recently a unit was added to read XML into the library. So maybe it would work for you. However that is DOM style parser. For large files SAX style would be harder to use but faster and lighter on RAM – Arioch 'The Jan 08 '14 at 20:23
  • @whosrdaddy a ha! That's what I was missing. GetAttributeNS seems to work just fine! Great! :)) Now next thing I hate is that in Stringlist I can't eliminate doubles unless sorted. So I'll resort afterwards once again, and since I might use a mdb, I think I'll go with query sort anyway. :) Arioch'The - will take a look, however right now, after a few testings this what I already have might do the trick just fine. :) Now gotta go and start figuring on how to open .mbp files as well... If I figure that out, I win. all the way ;0) Thanks guys for now!! – That Marc Jan 08 '14 at 22:36

0 Answers0