JSON parsing in android - 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(".!.!.!.", "&");

Related

How to parse the String with custom tags?

Can anyone please tell me how to parse the below string?
"Testing the parser - <tag><name>ANKIT</name><id>7</id></tag> <tag><name>VIKRAM</name><id>8</id></tag>. Some random text here"
How can I get the name "ANKIT" which is inside the <tag><name> ?
I tried SAX parser.
I think the XML parsers works only when the starting line is <?xml version="1.0"?>.
Is my understanding correct?
Since your text isn't valid XML but reminds more structure like HTML which isn't as strict, consider using HTML parser like https://jsoup.org/. With this library your code can look like
String myXML = "Testing the parser - <tag><name>ANKIT</name><id>7</id></tag> <tag><name>VIKRAM</name><id>8</id></tag>. Some random text here";
Document doc = Jsoup.parse(myXML, "", Parser.xmlParser());
String tag_name_text = doc.select("tag name")//CSS query to find <name> elements inside <tag> elements
.first()//take first result
.text();//get text it would generate in browser
System.out.println(tag_name_text);
Output: ANKIT
If I understand you right, try
STRINGNAME.substring(STRINGNAME.lastIndexOf(""), STRINGNAME.indexOf(""));

Special character coming parsing the xml

When I am parsing xml, I am getting special in good dynamics application char like that
pic.png
but the actual text is like that "If you've got any question or need"
But when I am parsing same text in normal application its working fine.
I am using sharepoint server to get xml. Now I am using general encoding UTF-8 only.
For parsing I am using XMLpullparser.
So how to resolve special character from xml.
There might be two things you might check:
Persister class used to parse the .xml can be initialised with such constructor:
Persister persister = new Persister(new Format(4, "<?xml version=\"1.0\" encoding= \"UTF-8\" ?>"));
You might replace all such characters while parsing:
value = value.replace("'", "\\'");

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 );

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.

Categories

Resources