I am starting to learn web-services in java EE6.
I did web development before, but never nothing related to web services.
All is new to me and the books and the tutorials i find in the web are to technical.
I started learning about .xsd schemas and also .xml.
In that topic i feel confident, i understand what are the schemas used for and what validation means.
Now my next step is learning about JAX-B(Java Api for XML Binding). I rode some about it and i did also some practice in my IDE. But i have lots of basic doubts, that make me stuck and cannot feel confident to continue to the next topic.
Ill appreciate a lot if someone could explain me well my doubts:
What does it mean mapping and what is a mapping tool?
What does it mean binding and what is a binding tool?
What does it mean parsing and what is a parsing tool?
How is JAX-B related to mapping,binding and parsing?
I am seeking for a good answer built by you, not just a copy paste from google(Ive already been online a few hours and only got confused).
Based on what I understand..
What does it mean mapping and what is a mapping tool?
In case of Java/XML, mapping is nothing but representing a Java object model
in to an XML document representation ( and vice versa. )
A mapping tool will allow you to convert from one format to another. This is just the definition step.
What does it mean binding and what is a binding tool?
Binding is the process of in-memory(as the application is running) conversion of XML document to object represantation.
Binding is achieved through unmarshalling.
What does it mean parsing and what is a parsing tool?
Parsing is reading an input stream of data and checking whether if the stream of data coforms to certain grammar. Parsing tools consume stream of data and generate errors when the data fails to conforms to grammar that the tool is checking. It would also generate events to indicate that it has received certain "tokens" from the stream. In java/xml scenario, there are multiple types of parsers such as DOM, StAx, SAX...
How is JAX-B related to mapping,binding and parsing?
JAXB mapping:
is when you use xjc to generate java class hierachy based on an XSD
The mapping occurs when classes are generated with JAXB annotations
Mapping tool in the scenario is xjc
JAXB Binding :
Occurs when an application unmarshalls an XML document to Object represanation
(JAXBElement) unmarshaller.unmarshal(
new File("some.xml"));
Parsing :
in order to convert XML document to object represantation, the JAXB engine has to
first "parse" the xml document to ensure correctness and then tokenize to instatiate java objects. This happens internally and you do not control ito ensure correctness and then tokenize to instatiate java objects.
Would strongly suggest that you get a copy of Java Web Services Up and Running and start at chapter 1 and start from there. It is not easy or intuitive, get ready for a long learning curve. Note. All the binding stuff will just confuse you unless you have a basic understanding of how all the pieces work.
Related
While exploring the efficient conversion tool for xml to json, I find AtlasMap! (https://atlasmap.io) is a very useful.
AtlasMap standalone application helps to formulate source to target mapping file. It supports transforming data to and from XML, JSON and Java objects.
Most important thing, my project is heavily Apache camel depended and it supports camel component too.
Though it is working fine for small payload, it is giving very bad performance for large payload for xml to json conversion.
I believe it is using DOM parser for xml processing, if so how can I configure STAX or Woodstox parser? Or what to do for large xml to json conversion if I want to use AtlasMap? Thanks
Thank you for your interest in AtlasMap. Unfortunately we haven't supported stream processing for XML/JSON yet, but it's definitely on the table. If you could file an issue on our GitHub repo and attracts other people's vote, it helps to be prioritized.
https://github.com/atlasmap/atlasmap
cf. https://github.com/atlasmap/atlasmap/issues/45
I'm searching for a way to consume soap webservices without using wsimport and tools like that. So far, everyting I found requires the wsdl artifacts generation and I want to avoid it because I don't know wich webservices will be used.
The idea is to give the user the possibility to add several WSDL's urls / methods and the program consume them and send it's response to another url.
I'm not looking for out of the box solutions/code or something like that.
What I need is to know if it is possible or not and how to aproach this problem, ideas and things like that.
Thanks!
SOAP mandates a contract between the Client and Server. The contract is that Client will give a request XML is XYZ format and Server will give response in ABC format. Without this, SOAP does not work. This is what is defined in WSDL. So we have to use WSDL.
Tools like wsimport, wsdl4j convert these WSDL files into an object hierarchy and prepares a JAXB wrapper for you, to make life easy!
Now if you do not want to use the tools which automatically convert WSDL to Java Beans or anything of that sort, note SOAP is based on nothing but an XML. If you know how to parse an XML in java, thats pretty much it. You can use a JAX Parser to achieve this and you can read everything that comes in a SOAP request and response.
The problem that you will face is, as the complexity of the SOAP XML grows, it will be difficult for you to parse the XML properly, identify the nodes, values and relations properly. And add to that the problem where in you cannot, without a proper object hierarchy , relate between nodes which are distance apart. Meaning, from line 100 you cannot go back and relate a line 10 node, unless you start maintaining your own objects, relations etc. (which is not what you want, i guess)
Using Java Beans the object hierarchy is maintained and because of that it is easy for you to relate them between different objects at different nodes easily. But JAXB is comparatively slower than JAX.
Given a pre-described set of tags which I can use(for example, a structure like the AndroidManifest.xml), how can I create an XML document (for a manifest file) from scratch in Java, preferably not using any third party libraries?
Also, what is(if any) the best way to do this?
There are many ways to generate XML without third party framework
SAX and DOM (powerful yet complex)
Jax-B (relatively easy)
The simplest way to generate an XML is to use Jax-B (which comes as a standard from JEE6 onwards).Refer here for more details on Jax-B
But if your trying to implement this in Android(since you mentioned AndroidManifest.xml) then I recommend you to use some third party framework like Simple to avoid unwanted complications, because it is more lightweight than the rest of the above mentioned technologies and easy to use.
If your strict on not using third party library(which I do not recommend), then use SaxParser in Android, refer here for some examples.
Hope this helps
Overview of Java DOM API for XMLType
Oracle XML DB supports the Java Document Object Model (DOM) Application Program Interface (API) for XMLType. This is a generic API for client and server, for both XML schema-based and non-schema-based documents. It is implemented using the Java package oracle.xdb.dom. DOM is an in-memory tree-based object representation of XML documents that enables programmatic access to their elements and attributes. The DOM object and interface are part of a W3C recommendation. DOM views the parsed document as a tree of objects.
http://docs.oracle.com/cd/B19306_01/appdev.102/b14259/xdb11jav.htm
http://www.c-sharpcorner.com/UploadFile/yougerthen/how-to-generate-an-xml-document-programmatically-using-system-xml-xmlwriter-part-iii/
hi
I started learning google data api.
I have one question
What will be the best way of parsing xml google data xml ?
1 > Should I go with manually parsing xml ?
Or
2 > XML to java object mapping technique ?
I am thinking about going with 2 way, As it will require writing less code from my side.
But I don't know how slow it will be in comparison to 1 way.
How slow is xml to java mapping technique ?
Is there any other better way to parse gdata xml ?
How slow is xml to java mapping
technique ?
The answer to this question depends on what you are comparing the xml to Java mapping technique to. Many object-to-XML solutions have been around for quite a while and have learned a number of tricks to do this conversion.
Is there any other better way to parse
gdata xml ?
Note: I'm the EclipseLink JAXB (MOXy) lead, and a member of the JAXB (JSR-222) expert group.
I'm not that familiar with the Google Data API, but after a quick google, it appears to be related to Atom which can easily be handled by any of the JAXB implementations:
Metro (reference implementation included in Java SE 6)
EclipseLink JAXB (MOXy)
Apache JaxMe
For an Atom example see:
http://bdoughan.blogspot.com/2010/09/processing-atom-feeds-with-jaxb.html
With JAXB you can also start with your own object model and add annotations to apply the XML mapping:
http://bdoughan.blogspot.com/2010/11/jaxb-and-inheritance-using-substitution.html
http://bdoughan.blogspot.com/2010/10/jaxb-and-xsd-choice-xmlelements.html
http://bdoughan.blogspot.com/2010/09/jaxb-collection-properties.html
MOXy also contains an XPath mapping extension that makes mapping your object model even easier:
http://bdoughan.blogspot.com/2010/09/xpath-based-mapping-geocode-example.html
Take a look at Caster project. Judging from your question, i don't think you would want to go though the trouble to parse the XML using the correct schema and create POJO's to match them (which is troublesome anyway).
I used Caster sometime ago, it is not the best, but got my problem solved pretty fast.
http://www.castor.org/
Greetings,
I have a complicated scenario to handle. I have a wsdl file which uses a particular XML schema.
The XML schema is actually a handcrafted implementation of a specification. There is also a Java based implementation of the same specification. So the XSD used in WSDL and Java classes at hand are quite similar, but not exactly same.
Almost all web service stacks allow creating classes from WSDL or creating WSDL from Java class annotations.
What I want to do, is to use the WSDL and bind XSD used in the wsdl to existing java classes.
Should/can I do this by manually replacing generated Java classes with existing ones? Is it a matter of changing type names in config files and moving binding annotations to existing classes?
If you know any best practices, or java web service stacks that support this kind if flexibility in a practical way, your response would be much appreciated.
Best Regards
Seref
I suggest Spring's Web Services module, which has no code generation involved, but provides a clean separation of concerns. Different concerns are broken out nicely by allowing you to provide your WSDL and existing schema(s) on one side (contract first), your existing Java-based domain model on the other, and a way to plugin in your OXM (Object-XML Mapping) technology of choice.
Since you have hand-crafted WSDL/schema and hand-crafted Java classes, the real work will be in configuring your OXM. I prefer JiBX as it keeps the concerns separated (no XML annotation garbage mixed into your domain) with JAXB as a backup if the learning curve looks too steep. Spring Web Services supports several other OXM frameworks, and you can even use several different ones at once.
As far as best-practices, I consider hand-crafted code a best practice, though I may be in the minority. If you generate classes from XML you end up with classes that are simple data containers with no behavior (assuming you want to regenerate them whenever your WSDL/XSD changes). This is bad if you favor the object-oriented paradigm because you end up having to place your "business logic" in utilities/helpers/services etc. instead of in the domain objects where it really belongs. This is one reason I favor JiBX. I can make very nice OO objects with behavior, a nice clean schema that doesn't necessarily match the objects, and can manage changes to either side with a mapping file similar to how hibernate does it for ORM (Object-Relational Mapping). You can do the same with JAXB, but that requires embedding XML structure into your object model, and binds a single XML representation to it (whereas with JiBX you can have many).
MOXY (I'm the tech lead) was designed for instances where you have an existing XML Schema and an exsting object model. It accomplishes this through XPath based mapping and can ever handle cases where the models are not that similar:
parse google geocode with xstream
MOXy also has an external binding file:
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/EclipseLink-OXM.XML
MOXy is a JAXB implementation with extensions (A couple of which are mentioned above). IF you go ahead with Spring, MOXy is configured as a JAXB implementation, and you need to add a jaxb.properties file in with your model classes with the following entry:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory