4

我正在使用 Apache XML-RPC 库从 Bugzilla 获取错误。调用该服务,我收到异常:org.apache.xmlrpc.client.XmlRpcClientException:无法解析服务器的响应:在文档的元素内容中发现了无效的 XML 字符(Unicode:0x8)。

有没有办法了解错误的确切位置。我找到了错误的日期,这会导致错误。但是有很多。我可以打印收到的 xml 或使异常更精确吗?

4

1 回答 1

1

为时已晚,但以防有人偶然发现。

编辑:

我已经研究了两个月了,最后我找到了解决上述问题的可行方法。

出现此问题是因为Bugzilla::Webservice有时响应远程过程调用会在 XML 响应中发送无效字符。

当 Apache 的 XML-RPC 尝试解析该响应时,它会给出以下错误:

XmlRpcClientException: 发现无效的 XML 字符 (Unicode: 0x8)

为了解决这个问题,需要扩展 Apache 的 XML-RPC 客户端以在尝试解析响应之前清除无效的 XML 字符。

在此处找到作为 Eclipse 项目的apache-xmlrpc的源代码。(导入此项目而不是 jar 文件)

为此,我们首先需要扩展BufferedReader类以在返回之前替换任何无效的 XML 字符。

因此,添加/apache-xmlrpc-3.1.3-src/client/src/main/java/org/apache/xmlrpc/client/util/XMLBufferredReader.java,如下所示:

package org.apache.xmlrpc.client.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;

/**
 * @author Ahmed Akhtar
 *
 */
public class XMLBufferredReader extends BufferedReader
{
    /**
     * @param in
     */
    public XMLBufferredReader(Reader in)
    {
        super(in);
    }

    @Override
    public int read(char[] cbuf, int off, int len) throws IOException
    {
        int ret = super.read(cbuf, off, len);

        for(int i = 0; i < ret; i++)
        {
            char current = cbuf[i];

            if(!((current == 0x9) ||
                    (current == 0xA) ||
                    (current == 0xD) ||
                    ((current >= 0x20) && (current <= 0xD7FF)) ||
                    ((current >= 0xE000) && (current <= 0xFFFD)) ||
                    ((current >= 0x10000) && (current <= 0x10FFFF))))
            {
                cbuf[i] = 'r';
            }
        }

        return ret;
    }
}

稍后,我们需要在方法的扩展中XMLBufferedReader发送。InputSourceparse

readResponse文件中的函数/apache-xmlrpc-3.1.3-src/client/src/main/java/org/apache/xmlrpc/client/XmlRpcStreamTransport.java需要改成:

protected Object readResponse(XmlRpcStreamRequestConfig pConfig, InputStream pStream) throws XmlRpcException
{
        BufferedReader in = new XMLBufferredReader(new BufferedReader(new InputStreamReader(pStream, StandardCharsets.UTF_8)));

        InputSource isource = new InputSource(in);
        XMLReader xr = newXMLReader();
        XmlRpcResponseParser xp;
        try {
            xp = new XmlRpcResponseParser(pConfig, getClient().getTypeFactory());
            xr.setContentHandler(xp);
            xr.parse(isource);
        } catch (SAXException e) {
            throw new XmlRpcClientException("Failed to parse server's response: " + e.getMessage(), e);
        } catch (IOException e) {
            throw new XmlRpcClientException("Failed to read server's response: " + e.getMessage(), e);
        }
        if (xp.isSuccess()) {
            return xp.getResult();
        }
        Throwable t = xp.getErrorCause();
        if (t == null) {
            throw new XmlRpcException(xp.getErrorCode(), xp.getErrorMessage());
        }
        if (t instanceof XmlRpcException) {
            throw (XmlRpcException) t;
        }
        if (t instanceof RuntimeException) {
            throw (RuntimeException) t;
        }
        throw new XmlRpcException(xp.getErrorCode(), xp.getErrorMessage(), t);
}

在对 Apache 的 XML-RPC 客户端进行此扩展之后,一切都应该正常工作。

注意:本文的其余部分是我发布的初始解决方案,这是一种解决方法,以防有人不想扩展 Apache 的 XML-RPC 客户端。

旧帖:

如果您使用带有一些和参数以及搜索条件的Bugzilla::Webservice::Bug::search实用程序函数。offsetlimit

您可能会在某些特定值上遇到此异常,例如,x您可以通过在调试模式下运行来找出这些值。offsetylimit

search现在通过保持xas offset 和1as来调用该函数limit,然后循环并递增x,直到它达到x + yas offset 的值,同时仍然保持limitas 1

这样,您将一次提取一个错误并在调试模式下运行,您可以确定导致异常的确切错误。

对于x = 21900y = 100执行以下操作:

for(int i = 21900; i <= 22000; i++)
{
 result = ws.search(searchCriteria, i, 1);
}

在调试模式下运行它,我发现offset导致错误的实际原因是21963这样,然后我编写了代码来避免该特定错误:

if(offset != 21900)
{
 result = obj.search(productNames, offset, limit);
 bugsObj = (Object[])result.get("bugs");
}
else
{
 result = obj.search(productNames, 21900, 63);
 Object[] bugsObj1 = (Object[])result.get("bugs");
 result = obj.search(productNames, 21964, 36);
 Object[] bugsObj2 = (Object[])result.get("bugs");
 bugsObj = new Object[bugsObj1.length+bugsObj2.length];

 for(int i = 0; i < bugsObj1.length + bugsObj2.length; i++)
 {
  bugsObj[i] = i < bugsObj1.length ? bugsObj1[i] : bugsObj2[i - bugsObj1.length];
 }
}
于 2016-04-29T10:54:57.103 回答