Android, XML parsing similar to E4X? - android

I am working on a project in Android now that involves parsing XML from a local file.
I have done XML work extensiely in flex and have become used to the power and flexibility of E4X and it's capabilities. I wonder now if I can do something similar in android or will I be forced to parse the XML manually?
Thanks

Quoting #Andreas_D:
E4X is a language extension, XML is treated like a primitive. E4X is not just for parsing XML, it's using XML as real types.
This can't be simulated or done with a Java 'framework', it would require a language extension for Java.
And, since Android does not have JAXB, you don't have that, either.
You have your choice of DOM, SAX, and the XmlPullParser, plus any third-party Java libraries you can find that have been ported to Android and fit whatever size constraints you may have.

check this out-
Working with XML on Android

Related

What is the best way to add external languages to android application in runtime

Our client application has means of updating a "language file" from a server occasionally. Yet it seems quite a challenge to support it on android..
I tried using inflate( XmlPullParser, ... ) but later found it doesn't support non-binary XML files.
The following are given:
Our app uses #string/resource_id resources to support languages. Basically it supports English, but the requirement is to provide language resources dynamically. That is, if we need to support Greek, or provide a change to the Greek language texts, we should be able to provide that on runtime.
So, on runtime our app connects a service and downloads an XML file, with syntax similar to string.xml in the current language.
What remains is how to change the text resources in the application in a sane manner (not one by one..)
I tried using inflate(with the XmlPullParser parameter) but it seems to be intended for use with binary pre-compiled XML files. To use that it would require to binarize the XML before hand, and that would be less compatible, not to say that aapt.exe tool is less documented, especially in my case.
So, any idea?
Don't know if it can be done everywhere in Android but instead of using native resources reference, maybe you can use the Java Properties class and using the device locale configuration to retrieve the wanted strings programmatically?

Why do we use xml language for layouts? [duplicate]

I would like to know why we use XML for the creating user interface layouts in Android. I know that it decouples the business logic from the design but what is the significance of the XML other than that?
Also I would like to know the significance of the auto-generated R.java file in this. All I know that it is generated according to the changes in the resources and that it helps us to access the widgets and resources through their IDs.
It would be great if someone could give a clear idea on these two aspects.
Unlike what everyone said about the XML being easy and efficient. Here is what I read in Hello Android by Ed Brunnette (p. 49) which made sense.
Android is optimized for mobile devices with limited memory and
horsepower, so you may find it strange that it uses XML so
pervasively. After all, XML is a verbose, human-readable format not
known for its brevity or efficiency, right?
Although you see XML when writing your program, the Eclipse plug-in
invokes the Android resource compiler, aapt, to preprocess the XML
into a compressed binary format.**It is this format, not the original
XML text, that is stored on the device.
This was the kind of answer that i was looking for.(sorry if my question meant otherwise).
The reason that XML was chosen is mainly because of its familiarity and the number of IDE tools that natively support it. The developers could have chosen JSON for example and still compiled that to binary.The auto-generated R.java file is a helper for the IDE so that you can get the benefit of autocomplete when you want to access a resource.
XML is easy to parse and manipulate programmatically, it's basically a tree structure and most UI creation tools already use it. It really has nothing to do with decoupling business logic because you can define Java code in Android using a Model-View-Controller pattern just as well.
The auto-generated R.java file is a helper for the IDE so that you can get the benefit of autocomplete when you want to access a resource. It also stops you from making stupid mistakes since the compiler will complain if you try to access a resource you haven't defined. If you were using a simple properties file you wouldn't know until runtime that the 'key' you are using is missing.
Same as why is silverlight with xml the answer is simple xml give power by integration and scalability. R.java is for indexing having things organized is never bad.
Sorry my english
One possible reason is that you need not have any working java underneath in order to be able to see the visual layout of the interface you are working on. The xml ui element/page is essentially a document that you can parse and display. If this were a source file you would have to either carefully parse it or compile and run it (all of which are more complex than parsing xml)
Xml as itself is well readable both by human and machine. Also, it is scalable and simple to develop. Also, you have already mentioned the decoupling.
As for R.java - it is just the way of accessing widgets. The solution is rather good, as it is using ints instead of string to use less memory and in the same time provides well readable names for the simplicity of development.
Android Layouts are tree like structures with some enforced rules. XMLs are perfect fit for this purpose.
JSON also have tree like structure but they are data-oriented while XML is document-oriented.** :
Meaning XML is based on the idea that documents have structural and
other semantic elements that can be described without reference to
how such elements should be displayed.
The actual display of such a document may vary, depending on the
output medium and style preferences.
While JSON was designed to
represent JavaScript and their prime purpose is data exchange . They are well suited in data-oriented areas
because of light weight and simplicity & closer in syntactic form to programming data structures.

Why is XML not HTML used for UI in Android progrmming?

Why can't HTML be used for building interface in Android Programing?
XML enables us to create custom tags.. Consider we created a custom view, we can reference them from xml itself. So the idea of UI separation from logics, stays intact.
HTML is a standard designed to display web pages. The android XML layout files are custom configuration files that are intended to be parsed by the dalvik VM. Two separate worlds. Here are some good reasons not to use HTML to define a configuration.
HTML is not well formed. Parsing is a pain. No schema checking.
How will you style elements ? CSS ?
Its hard enough getting CSS to work
across several browsers.
How will you introduce custom elements into
the HTML configuration ?
I could go on. It just does not make sense to configure a native UI using HTML.
I think the xml is chosen because the xml tags are defined in android according to the UI framework classes and the attributes are defined corresponding to the member variables of the class in the UI framework. This mapping was not possible for the html.
Extensible Markup Language, the extensible part makes it much more versatile. HTML is basically an implementation of the general concept of XML specifically for web browsers.
Also it is overly bloated for the simple task of creating layouts because it contains a lot of tags and attributes (i.e. method, onclick, etc.) that define the behavior of elements not just their layout.
I think they are few reasons for that:
When you have Layout and Source Code separately you can separate your application logic. Thus many XML files can be bound with one piece of source code and Platform (Android, Windows Phone, etc.) will choose automatically the proper XML layout depending for example on: screen denisity, screen orientation etc.
Next thing is answer to your question: Why not html. In my opinion there are few reasons. Most important is that XML has strict well formed structure and XML can be restricted with XML Schema. In fact having XML Schema is great because not also restrict your document but also give an sort of documentation of files inside XML. Someone reading this may think: HTML can have also strict structure as XHTML specification. Yes but XML is designed for that.
Next aspect is that XML is more popular as HTML in configuration/Layout (Java EE, XAML, Android XML's, etc.) stuff. It's like a good practise.
In fact you can build your application almost in HTML/JavaScript but you need to use some specific framework which wrappes your Platform application and transform it to Platfrom Independent Web Application(for example Sencha Framework)

Is it possible to use XHTML design for android application instead of XML design?

Is their any possibility to use XHTML design for android application instead of XML design
XHTML is a specific vocabulary for web pages; XML is a grammar for representing data. Both are standards from http://w3.org/
XHTML is a document markup language, like HTML. (Except, HTML is defined in the SGML grammar, and XHTML limits itself to the XML subset of that grammar.)
XML is a grammar (of sorts; this is a simplification) to define various data and document markup types.
Neither are, per se, used to develop applications; however, the Android SDK has several "small languages" that it uses that are defined in terms of XML for things like the manifest file and user interface layouts. These can not be substituted for another language, even one that looks similar on the surface, like XHTML.
It's roughly analogous to: Both English and French use fairly similar punctuation and the same alphabet, just as all XML dialects have similar notation, but even though they may look the same, it does not mean that one can substitute French for English at will.
You may see reference to "applications built using XHTML" or "...HTML;" typically, in fact, these are applications which produce HTML (or XHTML) output, or interact with/modify existing HTML/XHTML documents. The applications themselves are written in JavaScript if they run in the web browser; or some other language (Perl, PHP, Ruby, Python, Lisp, C++, even JavaScript again) if they run on the web server.
You could always use a WebView and render HTML inside of that, but you'd be much better off just learning Android's XML, or using the drag-and-drop Interface Builder in the Eclipse Plugin, which is actually pretty good since it was revamped earlier this year.
As far as I'm aware, you'd have to use the XML format to create an acutal layout within Android (and then use code within Android to control the interface). However, a good alternative would be to use the WebView class, which allows you to display a webpage within your application. This would likely writing a small amount of XML layout to display that view, but from there you can just make your "app" a webpage and control it however you best see fit (server-side processing if you have it on the actual "web" or perhaps JavaScript).
This may not be the only option, but it should work well.
Read more about WebViews here: http://developer.android.com/guide/webapps/index.html
I suppose you could, although you'd have to define an xslt transform that will translate the xhtml into android's xml language. You'd probably be confined to a small subset of xhtml, though.
XHTML is just one application of XML. XHTML is in fact XML. To answer your second question, yes you must use XML everywhere the documentation tells you you must. Sometimes you can do things programatically - but its probably easier to do it in XML, especially if you use the right tools.

2D tile game map storage

I'm new to game programming, and am trying to write the Map storage portion of my 2D tile based game on android. At first, my plan was to store the maps in XML. I found a tool (http://www.mapeditor.org/) that stores the created maps in a format close to what I was thinking. However, it seems like Android is very limited when it comes to XML. I'm used to working with XML Beans which doesn't work on Android, though I admit XML Beans would probably be too resource heavy for mobile devices anyways. However, after searching around for XML parser options, it seems like even basic XML validation isn't even included by default with the Android SDK. It's starting to look like XML might not be the best answer.
Does anyone know of a good standard way to store map configuration besides XML? Or some strong XML tools available in Android that handle validation (against a defined schema), and preferably simple parsing? I would really like to avoid defining my own flat file text format for maps...
Check out AndEngine. If I remember correctly AndEngine already has the classes needed to load a map from an XML file for rendering.
As far as a standard way to store map configurations, I would say XML is the way to go. I mean you could create your own format, but why re-invent the wheel?
I parse XML in Android with javax.xml.parsers.DocumentBuilderFactory and various other XML parts. It hasn't failed me yet!
For example see: http://www.developerfusion.com/code/2064/a-simple-way-to-read-an-xml-file-in-java/

Categories

Resources