从黑莓中的另一个 XML 字符串中提取嵌入的 XML 并在黑莓中解码 xml
我正在黑莓中使用 xml。 我当前正在使用 xml 字符串,其中值之一是另一个 xml 字符串。 问题是,虽然其他值都被整齐地提取出来,但 xml 值却没有。 只有“<”正在从节点中提取。
一样的,似乎在正常的java中工作。 我能看到的唯一区别是看到请求的方式:
在java项目中:
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);
而黑莓使用:
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));
并且以下代码用于从Web服务获取响应:: 在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();
在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;
除了这个变化之外,我看不到任何其他差异。嵌入的 xml 在 java 项目中被完美提取,但 bb 项目仅提取“<” :-(
任何帮助将不胜感激。
正在解析的 xml 文件在这里:( 没有提供完整的文件。已删除嵌入 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>
编辑: 我部分解决了这个问题: 使用:
xmlEmbeddedData = xmlData.substring(xmlData.indexOf("<?xml version="1.0"encoding="UTF-8"?>"), xmlData.indexOf("</Crossword>"));
我设法将我需要的内容提取到另一个字符串中,但有一个问题,我需要解析的xml字符串现在包含:“ & gt ;” for >,“ & lt ;”对于<和“& quot ; & amp ;”...没有空格...
我需要解码字符串并返回一个我可以解析的xml字符串。 有人可以帮忙吗?
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="રા"/>
<cell position="6_6" value="ન"/>
</grid>
<horizontalkeys>
<key number="19" position="3_6" length="4" answer="© C;ાફરાન" question="કે ;સર"/>
</horizontalkeys>
<verticalkeys>
<key number="17" position="6_5" length="2" answer="ª E;ાન" question="આબરૂ, 
 AA;્રતિ
 B7;્ઠા"/>
</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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的-
我想我不妨回答我自己的问题:
上面,我通过简单地从两个索引之间的响应数据中提取字符串来提取嵌入的xml...
下面我替换了 <具有适当的值。
这是替换功能:
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: