Sending large xml data through a socket - java

I'm newbie to XML using Java. I've to write a method to send a large XML data having lots of nodes through a socket to client application.
What is the suitable method to generate XML?
What is the best method to send large XML through sockets?

Since you are using sockets you just need to deal with Java InputStream/OutputStream. This gives you alot of flexibility in your XML handling as almost all XML technologies handle streams as input/output.
You could represent your data as plain old Java objects (POJOs), and then bind them to XML using JAXB. An implementation of JAXB is included in Java SE 6. There are other implementations such as MOXy (I'm the tech lead) and JaxMe.
For an example see:
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted

To generate XML you use DOM implementation provided by any XML DOM parser and generator.
Here is a nice tutorial. But for only generation try to use some small and light-weight parcers e.g. [tinyxml][2] or [qdparcer][3], because the xerces and others are going to be heavy weight for that. But if the parcing is also involved libxml or xerces will be of good choice because they provide nice SAX implementation for parsing, but you need to have schema defined for your data. Again try to serialize the data before sending so you can get rid of other problems.

Related

Is there a standard way of converting a SAX stream into a DOM document?

I have an existing proprietary class that can marshall data into XML using a SAX ContentHandler to capture the events. I'm looking for a class or a technique to subclass that event handler to create a DOM document with the smallest amount of coding. In particular I would prefer not to use any external libraries.
There are a number of old code examples available on various sites around the web, but I'm hoping something like this is now managed in recent standard libraries or Java itself.
Thanks!

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.

Parsing XML response from RESTful service in java

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!

I need some jar for Java and xml

classical way to handle XML in java is really lengthy and scary.
For this purpose i made my own class which can return me result without giving me more detail like,
myXML mx=new myXML("filename");
:
mx.getAll("node name");
mx.getFirst("node name");
:
I had completed it 80%. But unfortunately, i had lost it in PC crash.
is there any jar under GPL or apache license which provides facility to read & write XML in simplest way?
JDOM is simple API for parsing, creating, manipulating, and serializing XML documents in Java. API's you mentioned in your question are supported by JDOM (Other than many more useful API's).
Checkout JDOM documentation/book chapter here for more reading:
http://www.jdom.org/downloads/docs.html
http://www.cafeconleche.org/books/xmljava/chapters/ch14.html
Following are lines from http://www.jdom.org/docs/oracle/jdom-part1.pdf
So what’s the point of JDOM (Java
Document Object Model), and why do
developers need it? JDOM is an open
source library for Java-optimized XML
data manipulations. Although it’s
similar to the World Wide Web
Consortium’s (W3C) DOM, it’s an
alternative document object model that
was not built on DOM or modeled after
DOM. The main difference is that while
DOM was created to be language-neutral
and initially used for JavaScript
manipulation of HTML pages, JDOM was
created to be Java-specific and
thereby take advantage of Java’s
features, including method
overloading, collections, reflection,
and familiar programming idioms. For
Java programmers, JDOM tends to feel
more natural and “right.”
Try Apache Digester.Using digester will really simplify your XML parsing.You can refer this link for an example.
For your use case you may be interested in the javax.xml.xpath APIs available in the JDK. For an example see one of my answers to another question (below):
Remove XML Node using java parser
You may also prefer Service Data Objects (SDO). It is a generic data structure for representing XML data. For more information see:
http://www.eclipse.org/eclipselink/sdo.php
http://bdoughan.blogspot.com/2010/09/processing-atom-feeds-with-sdo.html
When parsing XML I recommend using the standard technologies: StAX, SAX, DOM, and JAXB. An implementation of each is included in the. JDK and alternate open source implementations are available offering improved performance and extended features, such as MOXy JAXB's XPath based mapping:
http://bdoughan.blogspot.com/2010/09/xpath-based-mapping-geocode-example.html
http://bdoughan.blogspot.com/2010/10/how-does-jaxb-compare-to-xstream.html
The advantage of the standard libraries is that they all work together:
StAX, SAX, and DOM are all valid inputs/outputs for JAXB
StAX, SAX, DOM, and JAXB are all compatible with javax.xml.transform libraries
StAX, SAX, DOM, and JAXB are all compatible with javax.xml.xpath libraries
StAX, SAX, DOM, and JAXB are all compatible with javax.xml.validation libraries
JAXB is the binding layer for two Web Service standards: JAX-WS and JAX-RS

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