Converting JSON Format String to XML String throws an Exception - android

I am trying for a long time to find a solution about the following problem that I am facing. I get a special json format string from my Database through a Http Request as follows:
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n
<resources>
<string> name=\"Test.Company\">Company</string>\r\n
<string> name=\"Test.Point\">Point</string>\r\n
</resources>"
A format as shown above contains only a string daten type.This format must be used as an XML file for an Android application. The Responed data from server is working properly.
The main point is to convert this special jsonformat to XML.
I am using the following code for convertion to XML as follows:
JSONObject jsonObject = new JSONObject("jsonStr_responded_from_server"); // throws an exception **of type java.lang.String cannot be converted to JSONObject**
String xml = XML.toString(jsonObject);
Did anyone has similar problem???

your server returns the normal string. because json object should be in {your object} format.example json object is {"user":"uservalues","empcode":"emp0041"}. but your string returned from server not in the format it will throws the error. have a look on this link
String xml = jsonStr_responded_from_server;//string xml returns from server. here no need json object

Related

I have XML parsing response in Android how do i want to implement JSON parse on that xml response it's possible?

I have XML parsing response in Android ,on that i want to implement JSON parse on that xml response it's possible?
What I understood from your question you want to parse XML to JSON and if that is the case you can use JAVA-JSON library check it out at GITHUB.
Quick example
JSONObject xmlJSONObj = XML.toJSONObject(XML_STRING);
I would suggest you to use json-lib, a library which adds JSON support to any Java program. json-lib can take XML and convert it into a JSON object.
Json-lib
Eg.
String xml = "your xml string";
XMLSerializer xmlSerializer = new XMLSerializer();
JSON json = xmlSerializer.read( xml );

JSON string is weird

I send JSON at node : response.json(object);
and I get json at android :
String json_string = EntityUtils.toString(response.getEntity());
And json_string from android :
"{\"query\":{\"count\":4,\"created\":\"2014-11-07T15:10:16Z\",\"lang\":\"en-US\",\"diagnostics\":{\"publiclyCallable\":\"true\", ~ }
It's different from what I generally got.
character '"' and '\' is added to json string
So I'm troubling in getting the result from json string.
Is there something wrong?
Its okk, You are getting just raw json.
Do one thing cast this string in StringBuilder and again StringBuilder to string. Its remove all "\ character. Try this its works for me
Your string is just escaped, to keep special characters like double quotes. If there was no escaping, then your string would look just like that:
"{\"
So you need to indicate that some of double quotes shouldn't be treaded like end of the string.
Refer to http://docs.oracle.com/javase/tutorial/java/data/characters.html

how to get the values from soap response in android?

the following xml tag for webservice response ,i dont know how to parse it..help me..
<NewDataSet>
<JOBLIST>
<CSIDNO>CS13224</CSIDNO>
<PName>Selva</PName>
<HouseID>G 34</HouseID>
<NAME>Dilipan</NAME>
<Address>Coimbatore</Address>
</JOBLIST>
<JOBLIST>
<CSIDNO>CS13224</CSIDNO>
<PName>Selva</PName>
<HouseID>G 35</HouseID>
<NAME>Kanrupannan</NAME>
<Address>Coimbatore</Address>
</JOBLIST>
</NewDataSet>
i am using the following java code .....
for(int i=0;i<result.getPropertyCount();i++)
{
SoapObject ob=(SoapObject)result.getProperty(i);
homeid[i]=String.valueOf(ob.getProperty("HouseID"));
namearr[i]=String.valueOf(ob.getProperty("NAME"));
address[i]=String.valueOf(ob.getProperty("Address"));
csidno[i]=String.valueOf(ob.getProperty("CSIDNO"));
}
how to get the values form xml tag..
Why using Soap there are different kinds of parsing methods you can use one of them. Parsing methods are SAXParser, Json parser, DOM parser, XML pull parser. In yourt situation SAX parser will be good to parse. see this and this tutorial. It will help you in parsing the xml you are receiving. Let me know if they helps you
Its pretty simple you have to get the respose as a string and use pattern matches to extract the detail you needed>Please see this post hope it helps you and you understand it.
String r = NameArray.columncount("userid", limitstart, loadNumber,loggername);
String temp = r.replaceAll(";\\s", ",").replaceAll("string=", " ")
.replace("anyType{", "").replace(",}", "");
enter code here
check this link for step by step instruction
http://amalan008.blog.com/2013/02/07/how-to-process-an-array-returned-by-a-wsdl-android/

Android XML parsing issue using DOM parser

I am trying to parse the following using Dom parser in Android.
<offerURL>
http://statTest.dealtime.com/DealFrame/DealFrame.cmp?bm=553&BEFID=93767&aon=%5E1&MerchantID=434524&crawler_id=1909400&dealId=TCk4NTG97Aa3wSQgh2U3FQ%3D%3D&url=http%3A%2F%2Frover.ebay.com%2Frover%2F1%2F707-64686-24023-0%2F2%3Fipn%3Dpsmain%26icep_item_id%3D190622592957%26icep_vectorid%3D260601%26kwid%3D1%26mtid%3D637%26crlp%3D1_260601%26kw%3D%7Bquery%7D%26query%3D%7Bquery%7D%26linkin_id%3D%7Blinkin_id%7D%26sortbid%3D%7Bbidamount%7D%26fitem%3D190622592957%26mt_id%3D637&linkin_id=7000251&Issdt=120323134700&searchID=p2.77722a731149145f60fa&DealName=Samsung+B2100+Outdoor+In+Schwarz+%28black%29+Orig.+Neuware&dlprc=89.95&crn=&istrsmrc=1&isathrsl=0&AR=1&NG=3&NDP=6&PN=1&ST=7&DB=sdcprod&MT=phx-pkadu-intl-dc20&FPT=DSP&NDS=&NMS=&MRS=&PD=95929320&brnId=14863&IsFtr=0&IsSmart=0&DMT=&op=&CM=&DlLng=7&RR=1&cid=&semid1=&semid2=&IsLps=0&CC=0&SL=0&FS=1&code=&acode=538&category=&HasLink=&frameId=&ND=&MN=&PT=&prjID=&GR=&lnkId=&VK=
</offerURL>
For parsing I am using following code :
Node node = .....
String nodeName = node.getNodeName();
if (nodeName.equalsIgnoreCase("offerURL")) {
String offerUrl = node.getFirstChild().getNodeValue()
Log.d("offerUrl => " + offerUrl);
}
It works fine but the value of <offerURL> tag is getting truncated.
The value of variable offerUrl printted in log cat is "http://statTest.dealtime.com/DealFrame/DealFrame.cmp?bm=553"
Not sure what exactly the issue is. Please help.
& is a predefined entitiy in XML and must be represented in a special way. In the URL, if you change all the & to & that should work.
Predefined entities in XML will tell you all the predefined entities in XML and how to represent them.

JSON parsing in android

I am new to android development,
I want to parse xml i have tried using sax xml parser but as it consist of "&" in it so that URL part is not getting completed parsed.SAX parser parsing upto "&' the rest of the part it is neglacting so i have to use json parser.
But for json parser i need to parse xml into json then from json to string which can be used by the JSON parser.
if any one is having idea wheather it will solve my problem of "&" in xml?
thnx for any help.......
As I see, you want to parse non standard xml. If you can change xml structure, use CDATA for links. If not, you can do some hack, like replace '&' before parsing onto some unique string. And after successful parsing you can replace it back.
DEMO:
String xmlString = "<link>linkwith&</link>"; //some xml with &
xmlString = xmlString.replaceAll("[&]", ".!.!.!.");
XMLHandler xml = new XMLHandler(); //your sax parse handler
xml.parseXML(output); //your parse function
After that, you get your parsed link somehow and make replace back:
String parsedLink = "linkwith.!.!.!.";
parsedLink = parsedLink.replaceAll(".!.!.!.", "&");

Categories

Resources