Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 解析不在父节点和子节点中保留重复名称空间的XML_Java_Xml_Xml Parsing_Xml Namespaces_Spring Ws - Fatal编程技术网

Java 解析不在父节点和子节点中保留重复名称空间的XML

Java 解析不在父节点和子节点中保留重复名称空间的XML,java,xml,xml-parsing,xml-namespaces,spring-ws,Java,Xml,Xml Parsing,Xml Namespaces,Spring Ws,在开始之前:我知道子节点从父节点继承了名称空间,这就是我出现问题的原因。不幸的是,我正在发送XML的Web服务不接受没有名称空间的子节点,而且,由于它是一个政府实体,它们的部分不太可能发生更改 也就是说,我正在使用Spring WS在我的应用程序和Web服务之间进行通信,因此框架以某种方式使用转换器将我的有效负载源解析为框架的有效负载结果: transformer.transform(Source, Result); 在进行转换之前,我的XML有以下两个节点: <enviNFe xmln

在开始之前:我知道子节点从父节点继承了名称空间,这就是我出现问题的原因。不幸的是,我正在发送XML的Web服务不接受没有名称空间的子节点,而且,由于它是一个政府实体,它们的部分不太可能发生更改

也就是说,我正在使用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">

转换后,第二个名称空间被删除(正如我前面所说,我知道原因):


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

那么,我有两个问题:

1-我是否可以避免使用默认方法时的行为(不使用封送拆收器)


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

我认为没有其他方法可以进行转换。正如您所知,封送员是在这些场景中使用的最佳实践。最好使用JAXB。

我也遇到过同样的麻烦。不幸的是(或不是),带有SOAPMessageFactory实现的WebServiceTemplate(如SaajSoapMessageFactory)将尽一切可能确保您通过将您从源到结果绑定到转换器来发送格式良好的XML作为请求,包括当您已经在父级中重复“xmlns”时,绝不允许您在子级中重复“xmlns”。你可以尝试一些优雅的选择——但这并不意味着它们是最简单的。您可以通过使用javax.XML.ws.Service和Dispatch接口在XML级别工作,如果您不需要SSL身份验证,这非常容易。检查这些链接(第一个链接用Pt BR书写):

您还可以尝试其他消息工厂,例如DomPoxMessageFactory。此链接可能有用:

但是,如果改变项目的结构不是一个选项(我就是这样),我有一个变通方法。是的,这是一个解决办法,但一旦目标Web服务期望一个格式错误的XML,我就开脱了:D

我刚刚创建了HttpComponentMessageSender和HttpComponentsConnection类的抽象,第二个类通过第一个类的方法createConnection(URI)实例化。因此,我可以像这样创建我的WebServiceTemplate:

WebServiceTemplate wst = new WebServiceTemplate(new SaajSoapMessageFactory());
wst.setMessageSender(new CustomHttpComponentsMessageSender());
遗憾的是,您需要将createConnecion方法回复到新的抽象,以便实例化自定义连接。正如我所说,这是一个解决办法

@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类的onSendAfterWrite方法(WebServiceMessage消息)中发送。令人惊讶的是,“message”参数没有在方法中使用。它只适用于继承规则。好消息是:这是一种受保护的方法。同样,缺点是,一旦字段没有公共可见性,我需要复制几乎整个类,以便只更改此方法,框架将需要它们来处理响应。所以,我会把我的全班记录下来:

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();
    }
公共类CustomHttpComponentConnection扩展了HttpComponentConnection{
私有最终HttpClient HttpClient;
私有最终HttpPost HttpPost;
私有最终HttpContext HttpContext;
私人HttpResponse HttpResponse;
私有ByteArrayOutputStream请求缓冲区;
受保护的CustomHttpComponents连接(HttpClient HttpClient、HttpPost HttpPost、HttpContext HttpContext){
super(httpClient、httpPost、httpContext);
Assert.notNull(httpClient,“httpClient不能为null”);
Assert.notNull(httpPost,“httpPost不能为null”);
this.httpClient=httpClient;
this.httpPost=httpPost;
this.httpContext=httpContext;
}
公共HttpResponse getHttpResponse(){
返回httpResponse;
}
公共HttpPost getHttpPost(){
返回httpPost;
}
@凌驾
受保护的OutputStream getRequestOutputStream()引发IOException{
返回请求缓冲区;
}
@凌驾
受保护的void onSendBeforeWrite(WebServiceMessage消息)引发IOException{
requestBuffer=newbytearrayoutputstream();
}
@凌驾
受保护的void onSendAfterWrite(WebServiceMessage消息)引发IOException{
OutputStream out=getRequestOutputStream();
String str=out.toString();
str=str.replaceAll(“,”);
ByteArrayOutputStream bs=新建ByteArrayOutputStream();
写(str.getBytes());
getHttpPost().setEntity(新的ByteArrayEntity(bs.toByteArray());
requestBuffer=null;
if(httpContext!=null){
httpResponse=httpClient.execute(httpPost,httpContext);
}
否则{
httpResponse=httpClient.execute(httpPost);
}
}
@凌驾
受保护的int getResponseCode()引发IOException{
返回httpResponse.getStatusLine().getStatusCode();
}
@凌驾
受保护的字符串getResponseMessage()引发IOException{
返回httpResponse.getStatusLine().getReasonPhrase();
}
@凌驾
受保护的长getResponseContentLength()引发IOException{
HttpEntity entity=httpResponse.getEntity();
如果(实体!=null){
返回实体.getContentLength();
}
返回0;
}
@凌驾
受保护的InputStream getRawResponseInputStream()引发IOException{
HttpEn
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();
    }