Share Your Code

Simple XML Parser

Posted by krystian6, Posted on December 27, 2011, Last updated February 13, 2012

GitHub URL: 
https://github.com/Cluain/Lua-Simple-XML-Parser

Story

I've used XML Parser created by Jonathan Beebe ( http://blog.anscamobile.com/2011/07/how-to-use-xml-files-in-corona/ ) to read small and simple XML files. However for one of my projects I do have a large XMLs to parse which are generated automatically for me. As you know, automatically may mean - random. Using a list of nodes and looping through it was out of the question. This is why I've created this little XML Parser [based on Jonathan's work].

Installation

Please follow the description on github.

Parsing XML

Sample XML:

1
2
3
4
5
<test one="two">
    <three four="five" four="six"/>
    <three>eight</three>
    <nine ten="eleven">twelve</nine>
</test>

You can parse this xml in two ways. Either by using node names and parameter names to access the values, or as with Jonathan's XML Parser, by using children and properties tables.

Using node names:

1
2
3
4
5
6
xml.test["@one"] == "two"
xml.test.nine["@ten"] == "eleven"
xml.test.nine:value() == "twelve"
xml.test.three[1]["@four"][1] == "five"
xml.test.three[1]["@four"][2] == "six"
xml.test.three[2]:value() == "eight"

Using childrend and properties tables:

1
2
3
xml:children()[1]:name() == "test"
xml:children()[1]:children()[2]:value() == "eight"
xml:properties()[1] == {name = "one", value = "two"}

For more examples you can chceck the xmlTest.lua file on github.

If you find it difficult or unintuitive, or if you get some errors for your XML, please write a comment or raise an issue on github.
I can create a small tutorial on how to use this parser if needed.


Replies

thegdog
User offline. Last seen 2 days 8 hours ago. Offline
Joined: 17 Jan 2011

Hey there. Glad I found this today. I was finding it a little difficult to just use the child values as I move to more complex XML files.

I am totally a newbie Corona/Lua developer here.

Currently, I am using version 1.1 of Jonathan's XML parser.

The problem that I am running into is that I don't know if there is an error with the XML file processing.

For instance, I am retrieving an XML file based on the current date. I am using network.download to download the XML file. If the XML file doesn't exist, I believe that it is saving the 404 page with the XML file name.

Then when I use xml:loadFile( xmlFilename, system.TemporaryDirectory ) to load the XML, it errors out:

Runtime error
…PtGu84DtPZ+0Fiik+++TI/-Tmp-/TemporaryItems/3/xml.lua:86: XmlParser: trying to close img with td
stack traceback:
[C]: ?
[C]: in function ‘error’
…PtGu84DtPZ+0Fiik+++TI/-Tmp-/TemporaryItems/3/xml.lua:86: in function ‘ParseXmlText’
…PtGu84DtPZ+0Fiik+++TI/-Tmp-/TemporaryItems/3/xml.lua:113: in function ‘loadFile’
…tGu84DtPZ+0Fiik+++TI/-Tmp-/TemporaryItems/3/main.lua:28: in function

And I don't know how I can trap that error in the application.

Does your version of the xmlParser allow for that?

Thanks!

krystian6
User offline. Last seen 20 hours 1 min ago. Offline
Joined: 6 Dec 2011

hi,

well it seems like error page. HTML looks like xml but it's only the new standards [mainly XHTML] that follow XML rules. So the 404 page that you get might end up with this error you get [trying to close img tag with td tag].
The easiest thing you can do is check the http status before you parse the xml.
To be honest I'm not sure how to do it with ansca network module. I would normally check for status 200. If it's not 200 then it's an error. I can see that the event sent to listener contains some error information, but I;m not sure if it will throw error on any status other than 200 or on connection errors.
Anyway, you need to know that what you want to parse is actual xml before you do.

thegdog
User offline. Last seen 2 days 8 hours ago. Offline
Joined: 17 Jan 2011

Right. I know how to check for that in JS, but don't see a way how to handle it in Corona.

I was hoping that the XML parser would throw an error that I could catch and act on, but it doesn't appear so, which makes using XML a dangerous proposition.

Appreciate your feedback. Thanks!

krystian6
User offline. Last seen 20 hours 1 min ago. Offline
Joined: 6 Dec 2011

In order to catch this error execute parsing using pcall()

thegdog
User offline. Last seen 2 days 8 hours ago. Offline
Joined: 17 Jan 2011

Thank you, krystian6.

I found a way to handle this using pcall() and I am able to get that error and handle it now.

Thanks so much for the suggestion.

krystian6
User offline. Last seen 20 hours 1 min ago. Offline
Joined: 6 Dec 2011

Moved repository to a new location. If you have cloned this repo, either replace it or add new origin.