Parsing rest XML response and find specific entity in java - java

I am new to RestAPI hence need help in parsing rest XML response and find specific entity in java.
From Below response I am looking for <objectId>virtualwire-3560</objectId> where this can be multiple entry in the response.
<virtualWires>
<dataPage>
<pagingInfo>
<pageSize>10</pageSize>
<startIndex>0</startIndex>
<totalCount>4660</totalCount>
<sortOrderAscending>true</sortOrderAscending>
</pagingInfo>
<virtualWire>
<objectId>virtualwire-3560</objectId>
<objectTypeName>VirtualWire</objectTypeName>
<vsmUuid>420BCD52-2C18-7300-BC69-B8E92908084D</vsmUuid>
<nodeId>cf73616c-1fde-40a8-896f-c4f8e4270972</nodeId>
<revision>1</revision>
<type>

Related

Android HTTPPost body parsing

I am having a really challenging time parsing some XML data returned to my Android app.
The data is sent as XML but printing it on my mobile screen, it comes out as the following:
{"sessid":"5eed0b52c6953b52e262b559b5557be4","session_name":"SESS6cbf091341a26e4687fa7850b465755a,"user":{"uid":"15","name":"guest","pass":"084e0343a0486ff05530df6c705c8bb4","mail":"adeoduye#hotmail.com", "mode":"0","sort":"0","threshold":"0","theme":"","signature":"","signature_format":"0","created":"1306008217","access":"1306094503","login":"1306134979","status":"1","timezone":"3600","language":"","picture":"","init":"adeoduye#hotmail.com","data":a:1:{s:13:\"form_build_id\";s:37:\"form-49ea7a4ef10a8a2b31478696f17e8dee\";","form_build_id":"form-49ea7a4ef10a8a2b31478696f17e8dee","roles":{"2":"authenticated user","3":"guest"}}}
Can anyone please help a newbie and give me some ideas on how to parse this type of output and/or plain XML?
This isn't XML but JSON. You have to parse that string using the JSON API.
Basically you create a JSONObject by feeding the string into a JSONTokenizer. You can now query the values from the JSONObject as described in the API reference example.
The String you're seeing here is in JSON format. You can parse this in Andriod using the following library : http://code.google.com/p/google-gson/
For more info on json, checkout http://json.org.

Jersey: How to log HTTP POST data?

I have a code block like
final Invocation.Builder builder = webTarget.request();
final Entity<IFSRequestPresentation> entity = Entity.entity(ifsRequestPresentation, MediaType.APPLICATION_JSON);
final Response response = builder.post(entity);
The server (which is external to me and I can't see logs) is not returning me the data I expect. I believe that the json payload is not good
The IFSRequestPresentation is quite complex and I would like to see how it is represented as json String.
I am using Spring(4.0.3) and Jersey(2.8).
Is there a way I can log builder.post(entity) method? or at least see what entity looks like as json String?
I wrote it to file like
mapper.writeValue(new File("my.json"), IFSRequestPresentation);
as per Jackson documentation and got the json written to that file

Reading lots of data returned by API

I am reading from an API provided by a company, but the problem is that one of the accounts from which I am getting the data has around 22000 json objects, it reads fine with small amounts of data, i would say up to 8000 records, but then I get issues like the json is not well formatted besides the problem of being able to read the response.
The response comes this way:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://ywers.com">
[{"Name":"Edward", "LastName":"Jones", "Address":"{accepted}"}
,{"Name":"Carlos", "LastName":"Ramirez", "Address":"{Rejected}"}, ....... 22k more records here]</string>
I asked for some help earlier on here for the best way to do this, and i got a response about reading it using the xml parser and then a json parser, i am using GSON.
String XML = "<Your XML Response>";
XPathExpression xpath = XPathFactory.newInstance()
.newXPath().compile("/*[local-name()='string']");
String json = xpath.evaluate(new InputSource(new StringReader(XML)));
and then
JSONArray jsonRoot = new JSONArray(json.trim());
System.out.println(jsonRoot.getJSONObject(0).getString("Address")); // {accepted}
The problem with this is approach i am having is that it throws errors when reading the XML, it starts reading but after a while it stops with errors like:
java.lang.OutOfMemoryError
at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractBuilder.java:94)
at java.lang.StringBuffer.append(StringBuffer.java:219)
at org.apache.harmony.xml.dom.CharacterDataImpl.appendData(CharacterDataImpl.java:43)
......
I would appreciate any advise on how to proceed with this, I am kind of new to android.
I don't know who would wrap 22k objects inside a xml string, but apparently someone is doing that. From my experience, your out of memory is because the you try to convert all the response to string but the response is too big to be handled. I recommend you to stream the JSON data. You can do stream the JSON data from the inputstream response that you get from the your HTTP post, but you need to skip the XML part by creating another input stream from the original response input stream and skip the XML part
Before I use the streaming API from google GSON I also got OOM error because the JSON data I got is very big data (many images and sounds in Base64 encoding) but with GSON streaming I can overcome that error because it reads the data per token not all at once. And for alternative you can also use Jackson JSON library I think it also have streaming API and how to use it almost same with my implementation with google GSON. I hope my answer can help you and if you have another question about my answer feel free to ask in the comment :)

How to extract a value of a http xml answer using android java

Well I'm kinda new to android and I'm trying to get a value of a http answer. But I never worked with that XML stuff before.
Example of web service : http://freegeoip.net/xml/123.123.123.123
this is the result here
<Response>
<Ip>123.123.123.123</Ip>
<CountryCode>CN</CountryCode>
<CountryName>China</CountryName>
<RegionCode>22</RegionCode>
<RegionName>Beijing</RegionName>
<City>Beijing</City>
<ZipCode/>
<Latitude>39.9289</Latitude>
<Longitude>116.388</Longitude>
<MetroCode/>
</Response>
how to get the value of countrycode, regioncode etc...
You should use SAX API (Simple Api for XML) to parse the xml from the webservice and handle it with the DefalutHandler class.Here's the documentation of DefaultHandler class: http://developer.android.com/reference/org/xml/sax/helpers/DefaultHandler.html
and here's a nice tutorial from 0 which should help you: http://www.codeproject.com/Articles/334859/Parsing-XML-in-Android-with-SAX

How to get xml file over http?

A xml file with data will be sent over http to server my job is to get that xml file and parse it using web service and I am using Netbeans 6.9. How can i get that xml file can anyone tell me ?
Looking for something like this?
URL xmlUrl = new URL("http://www.rekindle.co.za/rss.xml");
InputStream in = xmlUrl.openStream();
Document doc = parse(in);
Taken from a similar question about parsing at http://www.java-forums.org/xml/22674-get-xml-data-url.html
Assuming you're responsible for writing the service using servlets and the xml file will be POSTed to the server, you can access it using HttpServletRequest.getParameter(paramName).
Use HttpClient for the same to recieve the XML , After recieving the xml use JAXB/XMLbeans parser to parse the xml.
You must have to sure about xml's xsd before parsing the same.
If already you are having xsd against the incoming xml then just created the classes by using using any above mentioned tool.

Resources