xml to sqlitle loop in android - android

Hello all my second question here so be gentle :)
I'm still learning java and android, I'm learning it on my little project but I hit kind of logical wall.
I have XML file
<shift_plan>
<plan>
<agent data="John Smith"/>
<date data="6 Jan"/>
<shift data="M-3"/>
</plan>
<plan>
<agent data="John Smith"/>
<date data="7 Jan"/>
<shift data="M-3"/>
</plan>
<plan>
<agent data="John Smith"/>
<date data="8 Jan"/>
<shift data="M-3"/>
</plan>
</shift_plan>
on a web and I want to make a loop that would paste each data to function that put it to db. I have the function and working db so it looks like updateBD(agent, date, shift ) but how would I go about parsing the xml but to parse the 3 variables than put them in that function and than go again for the next 3 etc...until end of xml
I might be not making much sense I know, this must be something very simple I'm sure but I need really kick in the right direction..
Thanks for any answer,
Vlad

Android supports the XmlPullParser API right out of the box. It is an excellent solution for parsing XML documents in Android apps because you can start pulling information from the document right away (it's a streaming XML parser) and its pull-style API is a lot easier to use than that of a push-style streaming XML parser when all you need to do is parse the document into objects.
I am not sure if this is still true in Android 3 and 4, but the Android 2.3.x and earlier implementation of XmlPullParser (from the Apache Harmony project) does not support getPrefix() or getAttributePrefix(int index). Though, this shouldn't affect you because you are not using XML namespaces.
EDIT: Examining the git trees corresponding to the platform_frameworks_base tags, it appears that Android 3.2.4 and earlier have the XmlPullParser implementation from the Harmony project whereas beginning with Android 4.0, the implementation switched to kXML2.

Related

How can I specify which XML parser implementation to use?

I am currently trying to write an app (a game) that will, I hope, run on both desktop and Android. The data format I have chosen to use is XML because it's more flexible and powerful than JSON, it performs better than a database and I am familiar with it and like it.
However, I'm beginning to wonder if it's going to actually be possible to use XML.
In addition to plain old XML I'm also using XInclude in my data structures and XPath to locate the relevant nodes. This seems to require that I also validate my XML during parse, which is fine because I had already written the XML Schema when developing the XML in the first place.
I've managed to get everything working on the desktop, however, as soon as I tried to run it on Android it failed. It seems that the XML parser used by default on Android doesn't support XInclude (source).
I've added the following line to my build.gradle:
compile "xerces:xercesImpl:2.12.0"
and it seems to have worked in that the SchemaFactory seems to be resolving to the Xerces one as evidenced by the debugging output:
08-21 14:19:35.488 8254-8332 W/System.err: JAXP: Reading jar:file:/data/app/uk.co.redfruit.gdx.wobbegong-geDT8AfzN-vHBlDA-lE0_g==/base.apk!/META-INF/services/javax.xml.validation.SchemaFactory
JAXP: instantiating org.apache.xerces.jaxp.validation.XMLSchemaFactory
But the DocumentBuilderFactory appears to be using the default Android one because it still throws the error:
java.lang.UnsupportedOperationException: This parser does not support specification "Unknown" version "0.0"
Which is what is always thrown when you call setXIncludeAware on the DocumentBuilderFactory (See above link).
I had assumed that including it in my gradle build file would do the trick and it seems like it partly has, but not enough.
I'm hoping that all crosss platform XML parsing problems will be solved by using the same parser implementation on all platforms, but it doesn't seem as straight forward as that...
Take a look at the javadocs for DocumentBuilderFactory.newInstance()
Use the javax.xml.parsers.DocumentBuilderFactory system property.
I suggest you set the javax.xml.parsers.DocumentBuilderFactory system property to the classname of the xerces DocumentBuilderFactory implementation
I'm guessing it's this is the class you want
eg:
System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
doFunkyXmlStuff();
To specify the parser to be used as Xerces-2, I would suggest to pass the parser classname when instantiating the DocumentBuilderFactory as follows:
DocumentBuilderFactory docFactory = DocumentBuilderFactory
.newInstance("org.apache.xerces.jaxp.DocumentBuilderFactoryImpl", null);
docFactory.setXIncludeAware(true);

Xsd2Code using Mono for Android

I am trying to read XML->POCO using an xsd, so I use the Xsd2Code generator, giving me a simple Xml Serializer code. When I dry-test this in the regular Windows world, with my XML I have no issues getting around 18 POCOs from the xml-file.
The fun begins in the Android world. I see that the loading of the XML file is done perfect, no issues there I have a perfectly sound XML string. When the Deserialization is complete I only get the first element in the xml file, missing 17 POCOs.
What is wrong here? Is the Xml Serializer/Deserializer working diffrently in Mono?
I guess you should find a reduced testcase (simple program) that works on .NET and fails in MonoDroid, and attach it to a new bug in http://bugzilla.xamarin.com/

Trying to pull API into LinearLayout Android APP

I am a beginner of Android apps and am using Eclipse. I have found some larger samples of pulling APIs but I cannot find a simple one to get started with. I simply want to pull from an XML file on the web (by using an API KEY) and throw it in a LinearLayout (Vertical). I can then go from there, anyone know of any? Below is a sample of my XML:
<xmldata>
<Products>
<ProductCode>ITI-GR12</ProductCode>
<ProductName>Granada 9-3/4" Narrow Rim Platter</ProductName>
<ProductPrice>64.4000</ProductPrice>
</Products>
</xmldata>
I am assuming you are looking for mechanisms which help you to parse XML and extract data from it...Voila here is a link to get you started with different ways of doing it...
http://www.ibm.com/developerworks/opensource/library/x-android/
Good luck!
your question is not easy as you will have quite a lot of programming to achieve what you want.
Here are the steps :
read your data using sax
fill a data structure of your own (build a
class, data members will be filled by
previous parsing)
build an activity, use a layout you define to
show all UI fields you think you need
to display your data
fill the content of each view using the data
structure you filled on step 2.
If you are a beginner, I suggest you first understand steps 3 and 4, having fun with UIs, then try to understand how you could download your file, parse it and fill some data class to provide content for the views.
Regards,
Stéphane

Are there any StAX implementation for android?

I want to use StAX API implementation in android 1.6 and above devices. Are there any implementations out there ? I cannot use the jar file directly since it gives issues regarding inner class. If its not available, is there any way I can recompile the implementation ? Is there an alternate way for POJO class to be mapped into XML and vice versa directly, please exclude SAX parser and DOM parser.
I think it is possible for POJO class to be mapped into XML and vice versa using JAXB. But the situation is like this. Consider this example,
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cars>
<car registration="abc123">
<brand>BMW</brand>
<description>Sedan</description>
</car>
<car registration="abc123">
<brand>Ferrari</brand>
<description>SportsCar</description>
</car>
</cars>
Now in the result I want List which has the 2 cars in it.
Also how does JAXB parser fare against StAX ?
As far as I know, both Woodstox and Aalto should work on Android. Aalto is the single fastest conforming XML parser on Java platform, if that matters; and Woodstox supports widest range of XML constructs (from full DTD handling to RelaxNG/XML Schema validation).
For binding POJOs to XML, you could also consider Jackson extension jackson-xml-databind: while Jackson is mainly JSON processor, extension supports JAXB-style data binding for XML. And does it faster than JAXB reference implementation (see jvm-serializers benchmark).
This also should work on Android (Jackson itself is nr 1 JSON parser on Android).
So what you really want to "map POJOs into the XML and vice versa" is the Simple XML Library. You can use it with every version of Android from 1.5 up.
I even wrote a blog post explaining how to include it in one of your projects.

Getting values from XML in Android (API Level 3)

I have a string that contains a xml structure and there are two pieces of data in separate tags that I am after.
xpath has been added since API level 8, and with me being stuck with API level 3 (old phone for you ;-)) I need a way to get the data.
Would using a regular expression commit a huge sin? ;-) The xml isn't that big...
Looking for some guidance on what to.
Many thanks!
It might help if you added the XML and what you were hoping to achieve with it, but you should be able to parse the XML string using DocumentBuilder, this will return a Document and with that you should be able to call getElementsByTagName()

Categories

Resources