解析一个 XML 不在父节点和子节点中保留重复的命名空间

Parsing a XML not keeping duplicated namespaces in the parent node and child node

开始之前: 我知道子节点从父节点继承命名空间,这就是我的问题出现的原因。不幸的是,我发送 XML 的网络服务不接受没有命名空间的子节点,并且由于它是政府实体,因此不太可能发生变化。

也就是说,我正在使用 Spring-WS 在我的应用程序和 Web 服务之间进行通信,因此框架以某种方式使用转换器将我的有效负载源解析为框架的有效负载源负载结果:

transformer.transform(Source, Result);

在转换发生之前,我的 XML 有这两个节点,如下所示:

<enviNFe xmlns="http://www.portalfiscal.inf.br/nfe" versao="3.10">
   <NFe xmlns="http://www.portalfiscal.inf.br/nfe">

改造后去掉第二个命名空间(之前说过,我知道原因):

<enviNFe xmlns="http://www.portalfiscal.inf.br/nfe" versao="3.10">
   <NFe>

我也知道我可以使用编组器来实现相同的结果并自己编写解析代码。使用这种方法也可以,并且可以接受,但我不知道除了上面列出的方法之外,还有其他方法可以使用其他方法来实现同样的事情(将 javax.xml.transform.Source 转换为 javax.xml.transform.Result)。

那我有两个问题:

1 - 我可以避免使用默认方法(不使用编组器)时出现的行为吗?

2 - 是否有任何其他工具可以进行相同的转换?

我认为没有其他方法可以实现您的转型。如您所知,编组器是在这些场景中使用的最佳实践。最好使用 JAXB。

我遇到过同样的麻烦。不幸的是(或不是)实现 SOAPMessageFactory(例如 SaajSoapMessageFactory)的 WebServiceTemplate 将尽一切可能确保您发送 well-formed XML 作为请求,方法是将您绑定到从源到结果的转换器,包括永远不要让你在 children 中重复 'xmlns',而你已经在 parent 中重复了。您可以尝试几个优雅的选项 - 但这并不意味着它们是最简单的选项。您可以使用 javax.xml.ws.Service 和 Dispatch 接口在 XML 级别工作,如果您不需要 SSL 身份验证,这将非常容易。检查这些link(第一个写在Pt-BR):

http://www.guj.com.br/t/nfe-v2-00-veja-como-consumir-o-ws/297304

https://alesaudate.wordpress.com/2010/08/09/how-to-dynamically-select-a-certificate-alias-when-invoking-web-services/

您也可以尝试使用其他消息工厂,例如 DomPoxMessageFactory。这个 link 可能有用:

http://forum.spring.io/forum/spring-projects/web-services/128221-webservicetemplate-get-it-to-stop-adding-soap-envelope

但是,如果更改项目结构不是一个选项(我就是这种情况),我为您提供了一个解决方法。是的,一种变通方法,但是一旦目标网络服务预期格式错误 XML,我就原谅自己了 :D

我刚刚创建了 HttpComponentsMessageSender 和 HttpComponentsConnection 的抽象 classes,第二个是通过第一个的方法 createConnection(URI uri) 实例化的。所以我可以像这样创建我的 WebServiceTemplate:

WebServiceTemplate wst = new WebServiceTemplate(new SaajSoapMessageFactory());
wst.setMessageSender(new CustomHttpComponentsMessageSender());

遗憾的是,您需要将 createConnection 方法回复到新的抽象以实例化自定义连接。正如我所说,这是一种解决方法!

@Override
public WebServiceConnection createConnection(URI uri) throws IOException {
    HttpPost httpPost = new HttpPost(uri);
    if (isAcceptGzipEncoding()) {
        httpPost.addHeader(HttpTransportConstants.HEADER_ACCEPT_ENCODING,
                HttpTransportConstants.CONTENT_ENCODING_GZIP);
    }
    HttpContext httpContext = createContext(uri);
    return new CustomHttpComponentsConnection(getHttpClient(), httpPost, httpContext);
}

消息在我抽象的 HttpComponentsConnection class 的方法 onSendAfterWrite(WebServiceMessage message) 中有效发送。令人惊讶的是,'message' 参数并未在方法内部使用。它仅用于继承规则。好消息:这是一种受保护的方法。同样,缺点是我需要复制几乎整个 class 以便仅更改此方法,一旦字段没有 public 可见性,框架将需要它们进行响应处理。所以,我 post 我的整个 class 下来:

public class CustomHttpComponentsConnection extends HttpComponentsConnection {

    private final HttpClient httpClient;

    private final HttpPost httpPost;

    private final HttpContext httpContext;

    private HttpResponse httpResponse;

    private ByteArrayOutputStream requestBuffer;

    protected CustomHttpComponentsConnection(HttpClient httpClient, HttpPost httpPost, HttpContext httpContext) {
        super(httpClient, httpPost, httpContext);

        Assert.notNull(httpClient, "httpClient must not be null");
        Assert.notNull(httpPost, "httpPost must not be null");
        this.httpClient = httpClient;
        this.httpPost = httpPost;
        this.httpContext = httpContext;
    }

    public HttpResponse getHttpResponse() {
    return httpResponse;
    }

    public HttpPost getHttpPost() {
        return httpPost;
    }

    @Override
    protected OutputStream getRequestOutputStream() throws IOException {
        return requestBuffer;
    }

    @Override
    protected void onSendBeforeWrite(WebServiceMessage message) throws IOException {
        requestBuffer = new ByteArrayOutputStream();
    }

    @Override
    protected void onSendAfterWrite(WebServiceMessage message) throws IOException {

        OutputStream out = getRequestOutputStream();

        String str = out.toString();

        str = str.replaceAll("<NFe>", "<NFe xmlns=\"http://www.portalfiscal.inf.br/nfe\">");
        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        bs.write(str.getBytes());

        getHttpPost().setEntity(new ByteArrayEntity(bs.toByteArray()));

        requestBuffer = null;
        if (httpContext != null) {
            httpResponse = httpClient.execute(httpPost, httpContext);
        }
        else {
            httpResponse = httpClient.execute(httpPost);
        }
    }

    @Override
    protected int getResponseCode() throws IOException {
        return httpResponse.getStatusLine().getStatusCode();
    }

    @Override
    protected String getResponseMessage() throws IOException {
        return httpResponse.getStatusLine().getReasonPhrase();
    }

    @Override
    protected long getResponseContentLength() throws IOException {
        HttpEntity entity = httpResponse.getEntity();
        if (entity != null) {
            return entity.getContentLength();
        }
        return 0;
    }

    @Override
    protected InputStream getRawResponseInputStream() throws IOException {
        HttpEntity entity = httpResponse.getEntity();
        if (entity != null) {
             return entity.getContent();
        }
        throw new IllegalStateException("Response has no enclosing response entity, cannot create input stream");
    }

    @Override
    public Iterator<String> getResponseHeaderNames() throws IOException {
        Header[] headers = httpResponse.getAllHeaders();
        String[] names = new String[headers.length];
        for (int i = 0; i < headers.length; i++) {
            names[i] = headers[i].getName();
        }
        return Arrays.asList(names).iterator();
    }

    @Override
    public Iterator<String> getResponseHeaders(String name) throws IOException {
        Header[] headers = httpResponse.getHeaders(name);
        String[] values = new String[headers.length];
        for (int i = 0; i < headers.length; i++) {
            values[i] = headers[i].getValue();
        }
        return Arrays.asList(values).iterator();
    }

同样,这是我在无法更改项目结构时发现的最简单方法。希望这有帮助。