i am working with xml in blackberry.
i am currently working with a xml string, where, one of the values, is another xml string.
the problem is that , while the other values are being extracted neatly, the xml value is not.
only the “<” is being extracted from the node.
the same, seems to be working in normal java.
the only difference i can see is the way in which the request is seen:
in java project:
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
connection.setRequestProperty("Content-Length", "" + Integer.toString(resultText.getBytes().length));
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
wheras, the blackberry uses:
http.setRequestMethod(HttpConnection.POST);
http.setRequestProperty("User-Agent","Blackberry 8320/4.2.2 Profile/MIDP-2.0 Configuration/CLDC-1.1");
http.setRequestProperty("x-rim-transcode-content", "none");
http.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
http.setRequestProperty("Content-Length","" + Integer.toString(resultText.getBytes().length));
and the following code is used to get the response from the web service::
in Java:
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
in BB
InputStream inStream = http.openInputStream();
// Get the length and process the data
int len = (int) http.getLength();
if (len > 0)
{
int actual = 0;
int bytesread = 0;
byte[] data = new byte[len];
while ((bytesread != len) && (actual != -1)) {
actual = inStream.read(data, bytesread, len - bytesread);
bytesread += actual;
}
String recd = new String(data, "UTF-8");
responseData = recd;
other than this change, i cant see any other differences. the embedded xml gets extracted perfectly in a java project, but the bb project extracts only a “<” 🙁
any help would be much appreciated.
the xml file being parsed is here:( have not provided the full one. have removed all but one value in the embedded xml.
response:::::<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas .xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.o rg/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://sche mas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:glwsdl"><SOAP-ENV:Body><return xsi:type="SOAP-ENC:Arr ay" SOAP-ENC:arrayType="tns:CrosswordItem[1]"><item xsi:type="tns:CrosswordItem"><Date xsi:type="xsd :string">2012-01-04</Date><Crossword xsi:type="xsd:stri ng"><?xml version="1.0" encoding="UTF-8"?>
<crossword size="7">
<grid>
<cell position="5_6" value="&#xAB0;&#xABE;"/>
<cell position="6_6" value="&#xAA8;"/>
</grid>
<horizontalkeys>
<key number="19" position="3_6" length="4" answer="&#xA9 C;&#xABE;&#xAAB;&#xAB0;&#xABE;&#xAA8;" question="&#xA95;&#xAC7 ;&#xAB8;&#xAB0;"/>
</horizontalkeys>
<verticalkeys>
<key number="17" position="6_5" length="2" answer="&#xAA E;&#xABE;&#xAA8;" question="&#xA86;&#xAAC;&#xAB0;&#xAC2;, &#xA AA;&#xACD;&#xAB0;&#xAA4;&#xABF;&#xA B7;&#xACD;&#xAA0;&#xABE;"/>
</verticalkeys>
</crossword>
</Crossword><Gridsize xsi:type="xsd:string">7</Gridsize><Id xsi:type="xsd:string">63</Id></item></re turn></SOAP-ENV:Body></SOAP-ENV:Envelope>
edit:
i partially solved the problem:
using :
xmlEmbeddedData = xmlData.substring(xmlData.indexOf("<?xml version="1.0"encoding="UTF-8"?>"), xmlData.indexOf("</Crossword>"));
i managed to extract what i needed into another string, with one problem, the xml string that i need to parse, now containes: ” & g t ; ” for >,” & l t ;” for < and “& q u o t ; & a m p ;”…without the spaces…
i need to decode the string and get back a xml string that i can parse.
can someone help?
ok-
thought i might as well answer my own question:
Above, i have extracted the embedded xml by simply extracting the string from the response data between the two indexes…
below i replaced the < with the appropriate value.
And this is the replace function: