How to extract specific data from xml format data in j2me??
I am new in j2me app development & I am developing Location based j2me app for my final year. For that I am using Text Search Feature of Google Places API to search places. In this, by URL we query a place name of our desire & in response URL return result of place(s) name in xml format(Its did not send .xml file, only send data in xml format). Which is as follows-
[HTML]<PlaceSearchResponse>
<status>OK</status>
<result>
<name>The Taj Mahal Hotel</name>
<type>lodging</type>
<type>establishment</type>
<formatted_address> Apollo Bunder, Opposite Gateway Of India, P J Ramchandani Marg, Colaba, Mumbai, MH, India </formatted_address>
<geometry>
<location>
<lat>18.9223380</lat>
<lng>72.8333470</lng>
</location>
</geometry>
<rating>4.6</rating>
<icon> http://maps.gstatic.com/mapfiles/place_api/icons/lodging-71.png </icon>
<reference> CoQBcgAAAOPaRox3TgxkziRCx3GXjgCgx4--UKVRRqKX_V3yMcqxgHfmT_PXwryIh3wjFXvh4cq6Tw9FjFKiNr4pypyIhslOqodRAsAacSO0xhk1LvVqd_WGeda7vaUmImB8sVcxgBpDOvHe1wZXvAiGITBzTa2f6RU4cyi5AKb-Wev4_WHdEhAw2X4zrDX8QUK8niwWrjCDGhSnjoZcIOexbrcWMWP9_fp9p3VIyw </reference>
<id>aa5ee0d398a7f5c3787a638deaa2e87ffb38436a</id>
<photo>
<photo_reference> CnRvAAAAMaSdYSqi7SCjk-CKBOld9QI86Ev_Hqt387TNCynY4iFRutgP8Uw9W-uAlKrIsfrRiQpq3GPwe4Cglk1vNQmnX7-NTgvf_wsW4wPGtGg6rckXrwf9dQ3CyKiZxWkSHaNm9e4jTS6b-5N_RQqGc57xjRIQrnLMro1PpymPTno-9nE7RxoU9EbIYca-2kLc2oSAIVZxw4MtirI </photo_reference>
<width>960</width>
<height>1280</height>
<html_attribution>From a Google User</html_attribution>
</photo>
</result>
</PlaceSearchResponse>[/HTML]
From this, I want to retrieve data from <status> tag, <name> tag, <formatted_address> tag, <lat> and <lng> tag. How should I done this.... I am stucked here...plz help as soon as possible.
Re: How to extract specific data from xml format data in j2me??
[QUOTE=Rahul More;911578]I am new in j2me app development & I am developing Location based j2me app for my final year. For that I am using Text Search Feature of Google Places API to search places. In this, by URL we query a place name of our desire & in response URL return result of place(s) name in xml format(Its did not send .xml file, only send data in xml format). Which is as follows-
[HTML]<PlaceSearchResponse>
<status>OK</status>
<result>
<name>The Taj Mahal Hotel</name>
<type>lodging</type>
<type>establishment</type>
<formatted_address> Apollo Bunder, Opposite Gateway Of India, P J Ramchandani Marg, Colaba, Mumbai, MH, India </formatted_address>
<geometry>
<location>
<lat>18.9223380</lat>
<lng>72.8333470</lng>
</location>
</geometry>
<rating>4.6</rating>
<icon> http://maps.gstatic.com/mapfiles/place_api/icons/lodging-71.png </icon>
<reference> CoQBcgAAAOPaRox3TgxkziRCx3GXjgCgx4--UKVRRqKX_V3yMcqxgHfmT_PXwryIh3wjFXvh4cq6Tw9FjFKiNr4pypyIhslOqodRAsAacSO0xhk1LvVqd_WGeda7vaUmImB8sVcxgBpDOvHe1wZXvAiGITBzTa2f6RU4cyi5AKb-Wev4_WHdEhAw2X4zrDX8QUK8niwWrjCDGhSnjoZcIOexbrcWMWP9_fp9p3VIyw </reference>
<id>aa5ee0d398a7f5c3787a638deaa2e87ffb38436a</id>
<photo>
<photo_reference> CnRvAAAAMaSdYSqi7SCjk-CKBOld9QI86Ev_Hqt387TNCynY4iFRutgP8Uw9W-uAlKrIsfrRiQpq3GPwe4Cglk1vNQmnX7-NTgvf_wsW4wPGtGg6rckXrwf9dQ3CyKiZxWkSHaNm9e4jTS6b-5N_RQqGc57xjRIQrnLMro1PpymPTno-9nE7RxoU9EbIYca-2kLc2oSAIVZxw4MtirI </photo_reference>
<width>960</width>
<height>1280</height>
<html_attribution>From a Google User</html_attribution>
</photo>
</result>
</PlaceSearchResponse>[/HTML]
From this, I want to retrieve data from <status> tag, <name> tag, <formatted_address> tag, <lat> and <lng> tag. How should I done this.... I am stucked here...plz help as soon as possible.[/QUOTE]
Check out the link
[url]http://www.developer.nokia.com/Community/Discussion/showthread.php?161111-J2ME-SAX-Parser[/url]
hope this helps you in resolving your issue
Re: How to extract specific data from xml format data in j2me??
Thanx Vankatesh for your suggestion. But Now I am facing new problem. I am facing an error(java.lang.IllegalArgumentException) at parser.parse(is,srh); and I think this is because of parse() input format. parse() method only accept input in Inputstream or Inputsource format. So how I convert result in method's input format. To pasre result, parse() method only accept input in[B] Inputstream or Inputsource format[/B]. So how I convert result in method's input format. The code I developed To take result & for xml parsing is pasted below-
ContentConnection con = null;
DataInputStream instrm = null;
ByteArrayOutputStream bytarroutstrm = null;
byte [] searchdata = null;
String result = null;
String url = "https://maps.googleapis.com/maps/api/place/textsearch/xml?query="+stringEncode(SearchTxt.getString())+"&sensor=true&key= *API KEY*";
try
{
con = (ContentConnection) Connector.open(url);
instrm = con.openDataInputStream();
bytarroutstrm = new ByteArrayOutputStream();
int c;
while((c = instrm.read()) != -1)
{
bytarroutstrm.write(c);
}
searchdata = bytarroutstrm.toByteArray();
result = new String(searchdata, 0, searchdata.length);
XmlParsing xp = new XmlParsing(result);
}
catch(Exception ex)
{
ex.printStackTrace();
}
class XmlParsing implements Runnable
{
String result;
InputStream is;
public XmlParsing(String result)
{
this.result = result;
threadRun();
}
public void threadRun()
{
(new Thread(this)).start();
}
public void run()
{
try
{
SearchResponseHandler srh = new SearchResponseHandler();
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
is = getClass().getResourceAsStream(result);
try
{
parser.parse(is, srh);
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
is.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
Plzzz help me I stuck here because of this problem.
Re: How to extract specific data from xml format data in j2me??
If you check the documentation, [url]http://docs.oracle.com/javame/config/cldc/opt-pkgs/api/ws/jsr172/javax/xml/parsers/SAXParser.html#parse(java.io.InputStream, org.xml.sax.helpers.DefaultHandler)[/url] that exception comes when you provide a null stream.
And after checking how that stream comes, getResourceAsStream is a slightly different thing, it is about reading resource files packed with your application.
You already use ByteArrayOutputStream for storing the bytes you receive. ByteArrayInputStream would be quite ok to pass them forward to the XML parser.
In fact you do not really need the bytes, I guess you just wanted to check if you get correct XML from the server. However if you checked it once, and you assume it is going to be fine in the future, you can directly pass the DataInputStream to the parser.