How to parse wsdl and xsd files? - java

I've been looking for a way to programmatically parse WSDL and associated XSD files to get values from annotation / documentation tags. I managed to get values from wsdl using wsdl4j, but how do i do this for XSD files? I tried to use XSOM but for some reason i always get null.

1). Hope this link will be useful for you to choose the best parser, Parse WSDL Effectively.
I have tried using Apache Woden, WSDL4J and Membrane SOA. Among these, Membrane SOA seems to be developer friendly.
2). Place the dependent XSD's in the folder where you have placed the WSDL. Then try parsing your WSDL, it should work fine.

Maybe the JWSDL help will be of service. It also makes calls to the services.
Here a discussion on the subject

Related

Java to Wsdl Mapping in Windup Tools

I am trying to deploy a sample weblogic application in JBoss 7.1.1 final. To do this I needed to use Windup tools. In the windup tool it shows **Java to Wsdl Mapping ** for the following code. I really don't know what that is. I googled it, still no progress. Please help me out here. It is a very lengthy code, more than thousand lines. So I have given just a sample.
<?xml version='1.0' encoding='UTF-8'?>
<j2ee:java-wsdl-mapping xmlns:j2ee='http://java.sun.com/xml/ns/j2ee' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
<j2ee:package-mapping>
<j2ee:package-type>spclink</j2ee:package-type>
<j2ee:namespaceURI>a url here</j2ee:namespaceURI>
</j2ee:package-mapping>
It's a mapping which tells the webservices provider how to de/serialize Java objects from/to XML. Usually it's generated from Java, or the other way, from WSDL you can generate Java objects. I guess it's rarely done manually. You should be able to find the matching Java classes in the source.
I think nowadays you can rely on annotations, and also on default mapping. Depends if you can change the WSDL schema or not.

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!

What is best practice in converting XML to Java object?

I need to convert XML data to Java objects. What would be best practice to convert this XML data to object?
Idea is to fetch data via a web service (it doesn't use WSDL, just HTTP GET queries, so I cannot use any framework) and answers are in XML. What would be best practice to handle this situation?
JAXB is a standard API for doing this: http://java.sun.com/developer/technicalArticles/WebServices/jaxb/
Have a look at XStream. It might not be the quickest, but it is one of the most user friendly and straightforward converters in Java, especially if your model is not complex.
For a JMS project we were marshalling and unmarshalling (going from java to xml and xml to java) XML embedded in TextMessages (string property). We tried JAXB, Jibx, and XMLBeans. We found that XMLBeans worked best for us. Fast, easily configurable, good documentation, and easy Maven integration.
I have used and will continue to use JDOM -> www.jdom.org
Another option is a Sax Parser. It is procedural - i.e. a visitor pattern - but if the xml is fairly lightweight, (and even medium weight) I have found it to be very useful for this.
JAXB API which comes in Java(In built).
I have used JIBX in MQ module. It works very well. Ant config is simple. Used Xsd2Jibx converter to generate the binding files and Java beans from XML schema. Marshalling and un-marshalling allow to specify character-set parameter. It was useful in my project to handle custom character-set. But I found an issue in the binding compiler. If the Java bean has lengthier path name, it generates class file with lengthier file name which will cause issue in Windows XP(it has a maximum file length limit).
I haven't used other APIs. So I am not trying to compare with others. If you decided to use JIBX, I hope this will be helpful.
More details, please refer JIBX website
I've used XStream as well, it is easy to use and customizable. You can add your own custom converters and that was very handy for me...
So surprised more people have not mentioned Jibx. Amazing lib and i think a lot simpler to use than Jaxb. Performance is also fab!
For this you can also consider apache's bitwixt and simple framework for xml

How to extract data from a SOAP response in Java?

I have a client set up to send a request to the National Weather Service SOAP server. I am receiving the response that I expect, but I am unsure as to the best way to extract the data from it that I need.
For example, there is a lot of extra data in the XML (in the SOAPBody), but I only want to grab the data for the parameters that I set (such as temperature) to my POJO.
What's the best way to extract this data?
I started out trying to consume SOAP Web Services by hand like you describe - there are better ways.
There are libraries out there that will do all the work for you - no need to parse anything by hand.
Check out JAX-WS. Most Modern IDEs (Certainly Netbeans and Eclipse) also provide point and click support for building web service clients given a WSDL.
The biggest potential problem down this route is if there's no WSDL, or the WSDL is wrong, in which case the tooling I've linked might struggle.
The next safest thing would be to use an XML Parser like JAXP's SAX & DOM etc (they're right there in your JRE) to parse the response and then walk the data structures involved.
Finally you could go the string hacking route using splits or regexes but down that path lies a great deal of potential pain - there's more to the XML spec then nested tags.
It's strongly receommended that you not try and decode SOAP by hand :)
Just to expand on what #Brabster said,
Netbeans has extensive Web Service support, especially using the JAX-WS library.
http://www.netbeans.org/kb/60/websvc/jax-ws.html#Exercise_3_1

How to create WSDL file given SOAP WSDL operations

I haven't had any experience with web service related development. So, any ideas will be greatly appreciated.
Suppose, I have a file listing draft specification of WSDL operations. Following is one example. How would I go about creating the WSDL file. Is notepad sufficient or do I need to have WSDL editor?
getHostSystemInfo
Returns detailed information about host systems specified via given IDs.
input HostSystemIdCollection(Collection of Strings)
Output HostSystemInfoCollection
HostSystemInfo
Id: mandatory
Properties: Following properties should be provided for host systems
HostSystemName
HostSystemProperty1
HostSystemProperty2
HostSystemProperty3
....
....
If the question is just "how do I create the WSDL" then you could indeed use Notepad and just write it, it's only XML after all. However, writing syntactically correct XML by hand is pretty dull, and error prone. So I would recommend using WSDL aware tooling for example an Eclipse editor
An alternative is to write some Java which expresses the interface, and from it generate the WSDL. There are many ways of doing this, including starting with an EJB and annotating it accordingly. A few googles should help you find what you need.
My experience is that simple POC situations tend to work well starting at the Java. Larger scale projects benfit from considered designs starting at the WSDL.
coding WSDL by hand is a big pain! i used a XML editor for creation of and then generated the stubs with JAXWS. It is important to understand and differences of the WSDL styles, which is not trivial (have a look at WSDL styles). a good help is to import the WSDL schema to your IDE (eclipse, idea) and then work with autocompletion.
just for interest, why are you using WSDL + SOAP. if you have a choice and you use anyway HTTP, have a look at REST. It can make implementation of web-api a LOT easier, both on server side and for api-clients.
If you haven't done any web services before, I would strongly recommend a WSDL Editor. The Netbeans has a plugin that should help.
The other way of doing it, which may be easier is by using the Java annotations defined in JSR 181.
Of course you could use the worst text editor in the world (!) but I'd seriously consider using any decent XML editor or IDE (Eclipse's WSDL support is pretty decent). This will save you a lot of pain and suffer.
Or, if this is an option, you could just annotate a Java class with JAX-WS annotations and have your WSDL dynamically generated from the Java code. Personally, I prefer the WSDL-first approach, the Java-first approach is just a suggestion to get you started.
You could use Axis2 to create that for you.

Categories