2012-02-06 4 views
2

私はブラックベリーのxmlで作業しています。 私は現在xml文字列を扱っています。値の1つは別の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); 

wheras、ブラックベリーは、使用しています:私は見ることができ 唯一の違いは、リクエストが見た方法です

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サービスからの応答を取得するために使用されます::

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; 

この変更以外の違いはありません。埋め込まれたXMLがJavaプロジェクトで完全に抽出されたが、BBプロジェクトは、唯一の「<」:-(

任意のヘルプははるかに高く評価されるだろう。

を解析されているxmlファイルはここにある取り出します:(いません完全なものを提供する埋め込まれたXML内の1つの値ではなく、すべてを削除した

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">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt; 
&lt;crossword size=&quot;7&quot;&gt; 
&lt;grid&gt; 

&lt;cell position=&quot;5_6&quot; value=&quot;&amp;#xAB0;&amp;#xABE;&quot;/&gt; 
&lt;cell position=&quot;6_6&quot; value=&quot;&amp;#xAA8;&quot;/&gt; 
&lt;/grid&gt; 
&lt;horizontalkeys&gt; 
&lt;key number=&quot;19&quot; position=&quot;3_6&quot; length=&quot;4&quot; answer=&quot;&amp;#xA9 C;&amp;#xABE;&amp;#xAAB;&amp;#xAB0;&amp;#xABE;&amp;#xAA8;&quot; question=&quot;&amp;#xA95;&amp;#xAC7 ;&amp;#xAB8;&amp;#xAB0;&quot;/&gt; 
&lt;/horizontalkeys&gt; 
&lt;verticalkeys&gt; 
&lt;key number=&quot;17&quot; position=&quot;6_5&quot; length=&quot;2&quot; answer=&quot;&amp;#xAA E;&amp;#xABE;&amp;#xAA8;&quot; question=&quot;&amp;#xA86;&amp;#xAAC;&amp;#xAB0;&amp;#xAC2;, &amp;#xA AA;&amp;#xACD;&amp;#xAB0;&amp;#xAA4;&amp;#xABF;&amp;#xA B7;&amp;#xACD;&amp;#xAA0;&amp;#xABE;&quot;/&gt; 
&lt;/verticalkeys&gt; 
&lt;/crossword&gt; 
</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("&lt;?xml  version=&quot;1.0&quot;encoding=&quot;UTF-8&quot;?&gt;"),       xmlData.indexOf("</Crossword>")); 

は、私は今containes、私は1つの問題は、私が解析する必要がXML文字列を、別の文字列に必要なものを抽出するための管理:「&グラムトン。 "for>、" &1t "; <と" & "の場合; &アンペア;?」...スペースなし...

私は、文字列をデコードし、私が解析できるXML文字列を取り戻す必要がある 誰かが助けることができる

+0

最後のポイント:埋め込まれたXML文字列がCDATAです。 – s5v

答えて

1

OK- は私がようかもしれないと思いましたうまく自分の質問に答える:上記

if (xmlData.indexOf("&lt;?xml version=&quot;1.0&quot; 
    encoding=&quot;UTF-&quot;?&gt;") != -1 && 
    xmlData.indexOf("</Crossword>") != -1) 
{ 
    xmlEmbeddedData = xmlData.substring(
      xmlData.indexOf("&lt;?xml version=&quot;1.0&quot; 
         encoding=&quot;UTF-8&quot;?&gt;"),xmlData.indexOf("</Crossword>")); 
} 

を、私は単純に2つのインデックス間の応答データから文字列を抽出することによって埋め込まれたXMLを抽出しています... 下記の私は適切な値に<を置き換え

System.out.println("embedded--->" + xmlEmbeddedData); 
    xmlEmbeddedData = replace(xmlEmbeddedData, "&lt;", "<", true); 
    xmlEmbeddedData = replace(xmlEmbeddedData, "&gt;", ">", true); 
    xmlEmbeddedData = replace(xmlEmbeddedData, "&amp;", "&", true); 
    xmlEmbeddedData = replace(xmlEmbeddedData, "&quot;", "\"", true); 
    xmlEmbeddedData = replace(xmlEmbeddedData, "\n", "", true); 

そして、これは交換する機能である:

static public String replace(String val, String fnd, String rpl, 
     boolean igncas) { 
    int fl = (fnd == null ? 0 : fnd.length()); 

    if (fl > 0 && val.length() >= fl) { 
     StringBuffer sb = null; // string buffer 
     int xp = 0; // index of previous fnd 

     for (int xa = 0, mi = (val.length() - fl); xa <= mi; xa++) { 
      if (val.regionMatches(igncas, xa, fnd, 0, fl)) { 
       if (xa > xp) { 
        sb = append(sb, val.substring(xp, xa)); 
       } // substring uses private construct which does not dup 
       // char[] 
       sb = append(sb, rpl); 
       xp = (xa + fl); 
       xa = (xp - 1); // -1 to account for loop xa++; 
      } 
     } 

     if (sb != null) { 
      if (xp < val.length()) { 
       sb.append(val.substring(xp, val.length())); 
      } // substring uses private construct which does not dup char[] 
      return sb.toString(); 
     } 
    } 
    return val; 
} 
関連する問題