which XML write/parse/modify is good for android - java

I need to parse and modify the XML in android ..Can any one suggest which XML parser is better to parse and modify the XML in android ..?
Currently I'm using XMLpullparser but using this i'm not able to modify the XML...

Xpath is available for Android developers I believe. I use that all the time for any XML parsing really.

If the XML has a simple structure then you can deserialize the file into an object. You modify some properties of that object and serialize it to XML afterwards.
XStream is a simple library to serialize objects to XML and back again. It can be found here.
I think this is a clean way, but it isn't the easiest way if the XML file is very complex (because you have to map its structure to a Java class).

XMLPullParser isn't really designed for complex tasks AFAIK. SAX Parser is much better for advanced tasks, but it's also not as easy to use.
For manipulating XMLs, you could use SAX Filters. Look at this tutorial (IBM tutorials are great!): http://www.ibm.com/developerworks/xml/library/x-tipsaxfilter/

Related

map csv / xml files to java objects

can you please help me for choice the best java APIs to map CSV file and XML file into java objects in the context of spring boot application and micro services?
- OpenCSV, CommonApache JAXB ...?
what are the best API for csv and XML to java objcts for a
Thanks you.
I used OpenCSV a lot, without any issue. You can get a good feel of it from this article.
You will need a different library for XML. You need to choose first between DOM and SAX. The most important criteria is size - does it fit in memory with ease? If so, use a DOM one, as it's faster. Otherwise SAX.
A good recommendation for DOM parsing is dom4j.

Other replacements for Serialization

I have XML Configuration file and I need to load/save it so What are best ways to Parse XML Files for Load/Save configurations other than Serialization as I don't like the option of Serialization
For a Java application I serialized a pretty complex application state using XStream. For the most part this works extremely good. If the object is really simple this should not be a problem. Another simple alternative I often use to transfer data from Object <-> XML is JAXB with annotations on the Objects, or if the XML structure is the master with an XML Schema from which the classes can be generated.
For C# you could use IXmlSerializable, or DataContract. Multiple answers can be found on SO using both these classes. See this to see how to use DataContract.

CRUD on XML using Java

I am developing a java application and i am using XML to store the settings and other data in the application.I have read about Java preferences manager API but i felt storing in XML is more convenient in my application.I started usng JAXB first but then i dint find any tutorials to modify the XML once it is created.My application involves storing the Email account details of the users.As the user adds his accounts dynamically , i need to add them to XML as well.So i dint find JAXB convenient(or rather i dint find any tutorials to update or modify the XML ).The only other option i found was DOM parser here http://docs.oracle.com/javase/tutorial/jaxp/dom/readingXML.html .But i feel it is too complicated for such a simple application.speed , memory etc doesnt matter to me.Are there any other alternatives to do this?
This can be a good (java-oriented) solution. Besides being Berkeley DB enables the development of custom data management solutions, without the overhead traditionally associated with such custom projects. yet based on XML files. It's has a very complete documentation for integration with Java lang.
With JAXB you can:
Unmarshal the XML into Java objects.
Manipulate the Java objects to modify the XML.
Marshal the objects producing a new XML document which you can use to replace the original XML.
Or:
Parse the XML into a DOM
Unmarshal the XML using a JAXB Binder
Modify the object
Use the Binder to apply the changes back to the DOM.
Write out the DOM replacing the original XML
I like to point you to XMLBeam (Disclosure: I'm affiliated with that project). This library is ideal to store configuration values in XML and while the application grows maintain the XML format without changing your Java API.
This is how IO operations look like:
Projection projection = projector.io().file(file).read(Projection.class);
projector.io().file(file).write(projection);
You get one liner IO handling and static typed access to the XML content. A pattern for configuration data would be to include a default value template in your JAR, read this if no config file was found on disk and write it back to disk on config changes. But of course you can also create documents from scratch (described in tutorials).

Parse xml file in java and convert results to javabean

i would like to know how to parse xml file in java and convert results to javabean from it. Can anyone guide me thanks?
A really simple and convenient way: http://simple.sourceforge.net/
You can find many examples there for using it, too.
JAXB allows you to write an XSD to which your XML file should conform and then generate the java beans automatically. You don't need to hand-code the beans yourselves.
Check out the tutorial at http://docs.oracle.com/javase/tutorial/jaxb/intro/.

When and why would you use Apache commons-digester?

Out of all the libraries for inputing and outputting xml with java, in which circumstances is commons-digester the tool of choice?
From the digester wiki
Why use Digester?
Digester is a layer on top of the SAX
xml parser API to make it easier to
process xml input. In particular,
digester makes it easy to create and
initialise a tree of objects based on
an xml input file.
The most common use for Digester is to
process xml-format configuration
files, building a tree of objects
based on that information.
Note that digester can create and
initialise true objects, ie things
that relate to the business goals of
the application and have real
behaviours. Many other tools have a
different goal: to build a model of
the data in the input XML document,
like a W3C DOM does but a little more
friendly.
and
And unlike tools that generate
classes, you can write your
application's classes first, then
later decide to use Digester to build
them from an xml input file. The
result is that your classes are real
classes with real behaviours, that
happen to be initialised from an xml
file, rather than simple "structs"
that just hold data.
As an example of what it's NOT used for:
If, however, you are looking for a direct representation of the input xml document, as
data rather than true objects, then digester is not for you; DOM, jDOM or other more
direct binding tools will be more appropriate.
So, digester will map XML directly into java objects. In some cases that's more useful than having to read through the tree and pull out options.
My first take would be "never"... but perhaps it has its place. I agree with eljenso that it has been surpassed by competition.
So for good efficient and simple object binding/mapping, JAXB is much better, or XStream. Much more convenient and even faster.
EDIT 2019: also, Jackson XML, similar to JAXB in approach but using Jackson annotations
If you want to create and intialize "true" objects from XML, use a decent bean container, like the one provided by Spring.
Also, reading in the XML and processing it yourself using XPath, or using Java/XML binding tools like Castor, are good and maybe more standard alternatives.
I have worked with the Digester when using Struts, but it seems that it has been surpassed by other tools and frameworks for the possible uses it has.

Categories