x


Parsing large XML files on-the-fly, best practice?

I need to periodically (like every 10 seconds) parse an XML file I get via WWW. Using System.Xml and XmlDocument. When the file is small, it's fine, but larger ones give a noticeable pause in frame rendering. I'm doing it on an Update call. I know, I know, bad.

So what's a better way? Should I just do what I do, but in a coroutine? Different thread (if so, any good examples)? Or is there an XML parser implementation that can 'spread' its work out over multiple frames?

more ▼

asked Apr 19 '11 at 08:08 PM

DaveA gravatar image

DaveA
26.8k 153 171 257

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Well, a coroutine wouldn't make any difference since it's called within the Unity update-loop and a single task can't be interrupted. A different thread would be a way but if it's really much data it can influence the main thread as well (like every other thread).

I've never had a xml parser that support "suspending" in between. If there is one that would be great ;). Another solution would be to write your own parser. XML is not that hard to parse. What data you need? If it's just a subset, the parser don't need to "unwrap" the whole file into nodes. It would be enough to parse through the stream and just wait for the right node.

If you write your own parser you could implement your own time-watchdog that yields if a certain amount of time has passed.

When writing your own parser you have to think about if you want a "general" parser or one that fits your needs in this case. Does the xml file contain CDATA sections? Without those it gets really simple ;)

more ▼

answered Apr 19 '11 at 08:32 PM

Bunny83 gravatar image

Bunny83
46.9k 12 50 210

Right it's not too general, so I could write my own. I was thinking maybe a SAX parser might be targeted to this type of thing, but this data is not in need of any special love like that. Thanks.

Apr 19 '11 at 08:51 PM DaveA
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x548
x491
x263
x36

asked: Apr 19 '11 at 08:08 PM

Seen: 1927 times

Last Updated: Apr 19 '11 at 08:08 PM