Results 1 to 2 of 2
  1. #1
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Unmarshalling Restful Xml response

    I built a Bing Search API client using swing. I get the xml back which I store in a String and write to a JScrollPane.
    Java Code:
    public static String HttpGetString(String urlString) throws IOException
    	{
    		URL url = new URL(urlString);
    		HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    		
    		if(conn.getResponseCode() != 200)
    		{
    			throw new IOException();
    		}
    
    		BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    		StringBuilder builder = new StringBuilder();
    		String line;
    		while((line = reader.readLine()) != null)
    		{
    			builder.append(line);
    		}
    		reader.close();
    		conn.disconnect();
    		return builder.toString();
    		
    	}//end of HttpGetString
    I want to be able to display the info in a "useful" manner in JTextArea Like:

    Store name: Walmart
    address: 123 main st
    city: Whereverville
    State: Confusion
    Zip: 11111

    So do I have to create classes for each element in the hierarchy and then use JAXB? Does JAXB know how to unmarshall into classes given the elements in the XML?
    I have looked at Xstream and JAXB and cannot figure out where to go. The other problem is that they all read from a file not a String. Is there a better implementation for consuming RESTful Xml,
    where a built-in-class/method handles the low level work with the xml, or do I have to examine the xml and write classes from the structure I see? Also how to you parse the initial part of the response:
    Java Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <?pageview_candidate ?>
    -<SearchResponse Version="2.2" xmlns="http://schemas.microsoft.com/LiveSearch/2008/04/XML/element">-<Query><SearchTerms>walmart</SearchTerms></Query>
    thanks in advance!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Unmarshalling Restful Xml response

    Append a newline "\n" after each line

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 0
    Last Post: 04-07-2011, 03:09 PM
  2. JAXB Unmarshalling
    By milan.nosal in forum XML
    Replies: 0
    Last Post: 09-01-2010, 02:15 PM
  3. RESTful Web Service
    By aakinn in forum New To Java
    Replies: 0
    Last Post: 04-13-2010, 04:06 PM
  4. Replies: 0
    Last Post: 02-22-2010, 05:15 AM
  5. JAXB Unmarshalling
    By prateek in forum XML
    Replies: 0
    Last Post: 07-10-2008, 06:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •