从黑莓中的另一个 XML 字符串中提取嵌入的 XML 并在黑莓中解码 xml

发布于 2025-01-02 09:40:56 字数 4721 浏览 2 评论 0原文

我正在黑莓中使用 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">&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>"));

我设法将我需要的内容提取到另一个字符串中,但有一个问题,我需要解析的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="&#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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鼻尖触碰 2025-01-09 09:40:56

好的-
我想我不妨回答我自己的问题:

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

上面,我通过简单地从两个索引之间的响应数据中提取字符串来提取嵌入的xml...
下面我替换了 <具有适当的值。

System.out.println("embedded--->" + xmlEmbeddedData);
    xmlEmbeddedData = replace(xmlEmbeddedData, "<", "<", true);
    xmlEmbeddedData = replace(xmlEmbeddedData, ">", ">", true);
    xmlEmbeddedData = replace(xmlEmbeddedData, "&", "&", true);
    xmlEmbeddedData = replace(xmlEmbeddedData, """, "\"", 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;
}

ok-
thought i might as well answer my own question:

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

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.

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

And this is the replace function:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文