1

I need to send a HTTP GET/POST request to an external API which returns XML data and then parse this data. Does an API for this exist in JSP?

If I use that code inside a class and use a method of it in JSP, will there be any problems?

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
Vpp Man
  • 2,008
  • 4
  • 34
  • 63

1 Answers1

2

You can use URLConnection to send a HTTP request and get the HTTP response as an InputStream. You can use JAXB to unmarshal an InputStream containing a XML document into a javabean instance which follows the XML structure.

Imagine that the XML response look like this,

<data>
    <foo>fooValue</foo>
    <bar>barValue</bar>
</data>

and your JAXB javabean look like this,

@XmlRootElement
public class Data {

    @XmlElement
    private String foo;

    @XmlElement
    private String bar;

    // Getters/setters.
}

then you can unmarshal it something like as follows:

InputStream input = new URL("http://example.com/data.xml").openStream();
Data data = (Data) JAXBContext.newInstance(Data.class).createUnmarshaller().unmarshal(input);
String foo = data.getFoo(); // fooValue
// ...

As with every other line of Java code in JSP, doing this in a JSP file instead of a normal Java class doesn't necessarily cause technical problems, but it may end up in maintenance nightmare.

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • thanku. After that how to access a particular element ? data contains unmarshalled content. How to access "username" element form it ? – Vpp Man Feb 18 '12 at 05:20
  • Uh, just add a normal javabean getter method to the `Data` class as in the example. Are you familiar with javabeans? Have you bothered to click the JAXB link? It points you to the JAXB tutorial. – BalusC Feb 18 '12 at 05:21
  • i am newbie. I have checked that manual. But it was hard to understand. So to avoid confusingmyself, i am trying to find a simple and easy to understand tutorial. I do not know much about java bean. Is it creating a class with a property name which is same as the XML element and writing a function to return that variable, that you are talking about ? – Vpp Man Feb 18 '12 at 05:26
  • thanku for the example. JAXB javabean is same as a class. Correct ? – Vpp Man Feb 18 '12 at 05:30
  • Uh, that's just a Java class, yes. I'd suggest to put this all aside and take some days/weeks to go through a basic Java book/tutorial first to familiarize yourself with the basics of the Java language and terminology. – BalusC Feb 18 '12 at 05:31
  • thanku. I have already went to a java class. But the coaching was not good. So i am doing this project myself at home by using online tutorials and help from you and others when i have doubts. – Vpp Man Feb 18 '12 at 05:33
  • do i have to create properties for all XML elements ? Or only the ones that i needed ? – Vpp Man Feb 18 '12 at 05:34
  • Play around with it and find it out yourself :) – BalusC Feb 18 '12 at 05:41
  • yes. i have to include all! When run with a single property, got a exception: `javax.servlet.ServletException: javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"response"). Expected elements are (none)`. I will try adding all elements now. I have only included a getter in the class. – Vpp Man Feb 18 '12 at 05:45
  • first line of XML is: ``. Next line is: ``. Then inside it all elements are listed inside it. So how to create attributes for these ? – Vpp Man Feb 18 '12 at 06:42
  • Solution: http://stackoverflow.com/questions/8214989/how-to-parse-xml-using-jericho-html-parser – Vpp Man Feb 18 '12 at 07:09
  • You don't have to include them all. You apparently did something wrong. – BalusC Feb 18 '12 at 14:39
  • Thank you. But I got it working when above questions solution was used (after making come changes). – Vpp Man Feb 18 '12 at 17:59