Special character coming parsing the xml - android

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("'", "\\'");

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

SoapObject Add property xml string in Webservice using ksoap2

I created XML string using XmlSerializer and it prints out as
<tag> string </tag>
I want to add the xml string to SOAPObject and get response from web service.
So I added and when I print out the requestDump the < and > are being replaced by &lt ; and &gt ;
I assume that it is being encoded by HttpTransportSE.
I read here Android Ksoap2 web service for download/Upload and it says I need to convert my string to binary before uploading? I am confused because the API tells us to upload xml. Currently, there is no error or exception but the result is empty. I think it's the encoding problem.
Any help will be appreciated. Thanks!
When using ksoap2 you don't have to handle the serialization yourself. If you want to add an entry to the xml like this:
<tag>string</tag>
Then you just have to call this when constructing the SoapObject you are sending to the server:
soapObject.addProperty("tag", "string");
Also modifying the requestDump is never a good idea. This output is just for debug purposes. That's the reason why you have to set transport.debug = true; for the requestDump or responseDump to even appear. As I said above just use the addProperty() and addSoapObject() methods to construct your request.
Use SoapObject.setInnerText("..."); to add CDATA text to a Ksoap2 request.

StringBuilder append String breaks UTF 8

Im sending an XML with HttpPost to a server. This used to wotk fine, and im doing it succesfully in other parts of the project.
Im using a StringBuilder to create the xml request but since i am appending strings as Data to the nodes, i am getting an error response from the parser on the server:
Invalid byte 2 of 2-byte UTF-8 sequence.
When i log the request and check it in w3c xml validator there are no errors.
This is an excerpt (whole method would be to big and has sensitive Data) from my Stringbuilder Method:
StringBuilder baseDocument = new StringBuilder();
baseDocument.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><request><setDisposalRequest><customer><company><![CDATA[");
baseDocument.append(company);
baseDocument.append("]]></company>");
baseDocument.append("<firstName><![CDATA[");
baseDocument.append(name);
baseDocument.append("]]></firstName>");
...
As soon as i replace the String vars i append with hardcoded Strings, all works fine
i.e
baseDocument.append(name);
to
baseDocument.append("name");
All the strings have values, non of them a null or are empty!
Before the request i set the StringEntity to xml
se.setContentType("application/xml");
what am i missing?!?
Your XML header claims that it's UTF-8, yet you never mention if you actually write UTF-8. Make sure the actual bytes you send are UTF-8 encoded. The error message suggests that you're using another encoding (probably a ISO-8859-* variant).
This is another reason that manually constructing XML like this is dangerous: there are just too many corner cases to observe and it's much easier to use a real XML handling library. Those tend to get the corner cases correct ;-)
And no: StringBuilder certainly does not break UTF-8. The problem is somewhere else.

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/

XML parse error on ë (crash with accented letters)

I have a crash in an xml file. it occurs on a ë, in this case belgië (dutch for belgium).
I'm busy with searching for an answer but I just can't find a solution.
I'm using the sax parser under Android.
error: org.apache.harmony.xml.ExpatParser$ParseException: At line 2, column 204: not well-formed
xml source: http://biohorma.weatheronyoursite.com/villadm_hooikoortsverwachting_be.xml
Side note, i get the data via a stream, is the only option to put this stream to a temp value, replace the illegal character with a valid one and make a new stream of it or can you add something in the stream to do this?
It seems you should use the String (byte[] bytes, String enc) constructor, assuming what server sends you is encoded in UTF-8:
String properXml = new String(byteArrayIReceivedFromServer, "UTF-8");
The issue is not with the parser - it's acting correctly - but with whatever code is sending the XML. ë needs to be encoded and passed as ë. The same also must be done to other accented characters, ampersands and angle brackets.
You should replace special characters in the xml I think..
See a comprehensive list of chars here: http://www.w3schools.com/tags/ref_entities.asp
it says your umlaut e is like : Ë Ë Ë capital e, umlaut mark
Then also for a brief explanation if u feel like reading.
Hope it helps.
The server sends these headers:
Content-Type: text/xml
Content-Length: 124512
Since no charset is specified for content type, the normally correct assumption is US_ASCII. However, the XML payload seems to be encoded in ISO-8859-1
<?xml version="1.0" encoding="iso-8859-1"?>
and the 'ë' is encoded as 0xEB (235). It is very common for servers to encode text payload in ISO-8859-1, so this is something that one simply has to deal with.
My guess is that if you serve the parser with a byte stream directly, it will detect the encoding an act accordingly. If you use a character stream (not recommended), make sure to specify correct encoding.

Categories

Resources