I have read the spring documentation for reading/writing the XML documents..(http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/oxm.html)
The options provided using different typs of marshalling(Oxm, Jaxb, jibx, castor, xstream) all seems to be using the conversion of a document based on some mapping file or a java bean or identifying by comparing each individual element using xstream.
I have a requirement, where i will get big xml files and i need to read some of the elements only in it and i dont want to keep the complete xml into the memory.
What are the options that i have in spring?
if any of the options that the spring documentation provides and useful for my requirement, please provide some examples.
BTW, i am using latest spring 4... FYI.
For large XML file you would need a SAX parser which is an event based parser.
Here an example
http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/
Spring Object/XML Mapping is to convert XML to and from an object which is a different use case.
Related
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.
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).
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/.
Please bear with me for this novice question.
I am calling a RESTful web service APIs that returns XML response. Apart from normal XML parsing schemes like DOM based parsing, SAX based parsing, is there a way to transform this XML response directly into some object? What more details/specification from service side would be required to do such transformation?
i can't give a summary of all the options available, but i recently used jaxb to do the opposite (java to xml) and it was simple and easy to use. since jaxb also supports xml to java, as described here, i would suggest giving that a look. it's based on annotations and java beans (or pojos) - you just indicate which attributes correspond to the elements with attributions, and it does the rest.
if you have a schema, it will generate java classes for you. alternatively, here's an example of working without a schema.
ps according to comments in the final link, you don't even need to annotate if the names match the xml!
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.