Marshaller使用org.w3c.dom.Document元素

时间:2018-04-06 18:42:58

标签: java dom jaxb digital-signature

在使用JaxB的情况下,我将不得不寻求帮助。

我有一个使用xml文档(org.w3c.dom.Element)转换(marshal)的类,当我调用jaxb的marshal函数时,它将我的XML"的签名标记加倍。签名:签名",通过我的测试,文档正确生成

我的xsd类是:

@XmlAnyElement
protected Element any;

我的元帅方法是:

    private void transformXmlOut(final ArquivoEFinanceira arquivoVO, final OutputStream outputStream, final String encoding) {
    try {
        JAXBContext context = JAXBContext.newInstance(arquivoVO.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
        marshaller.marshal(arquivoVO, outputStream);

    } catch (JAXBException e) {
        throw new AmplisException("Erro durante a geração do arquivo XML.", e);
    }
}

这是我的重复标签:

<Signature:Signature xmlns:Signature="http://www.w3.org/2000/09/xmldsig#" xmlns="http://www.w3.org/2000/09/xmldsig#">

有人有这个吗?处理此行为的最佳方法是什么 感谢您的支持

编辑#1

我认为问题实际上是在父类中引用的,我在package-info.java中添加了一个Item并得到了更好的结果:

@javax.xml.bind.annotation.XmlSchema(
    namespace = "http://www.eFinanceira.gov.br/schemas/envioLoteEventos/v1_2_0", 
    elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED,
    xmlns = {
            @javax.xml.bind.annotation.XmlNs(prefix = "xsi", namespaceURI = "http://www.w3.org/2001/XMLSchema-instance"),
            @javax.xml.bind.annotation.XmlNs(prefix = "xsd", namespaceURI = "http://www.w3.org/2001/XMLSchema"),
            @javax.xml.bind.annotation.XmlNs(prefix = "", namespaceURI = "http://www.w3.org/2000/09/xmldsig#"),
    })

package br.com.ymf.amplis.legais.sped.financeira.layouts.v1_2_0.lote;

新结果:

我现在拥有签名标签:

<ns5:Signature xmlns="http://www.w3.org/2000/09/xmldsig#">

但是在root元素中添加了新的命名空间:

xmlns:ns5="http://www.w3.org/2000/09/xmldsig#"

我如何删除它? Ns5命名空间?

父类:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "loteEventos"
})
@XmlRootElement(name = "eFinanceira")
@XmlSeeAlso(EFinanceira.class)

public class EFinanceira implements ArquivoEFinanceira {

@XmlElement(required = true, name="loteEventos")
protected EFinanceira.LoteEventos loteEventos;


public EFinanceira.LoteEventos getLoteEventos() {
    return loteEventos;
}


public void setLoteEventos(EFinanceira.LoteEventos value) {
    this.loteEventos = value;
}



@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "evento"
})
public static class LoteEventos {

    @XmlElement(required = true, name="evento")
    protected List<TArquivoeFinanceira> evento;


    public List<TArquivoeFinanceira> getEvento() {
        if (evento == null) {
            evento = new ArrayList<TArquivoeFinanceira>();
        }
        return this.evento;


      }

    }

}

TArquivoeFinanceira.class:

    @XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "TArquivoeFinanceira", propOrder = {
    "any"
})
public class TArquivoeFinanceira {

    @XmlAnyElement
    protected Element any;
    @XmlAttribute(name = "id", required = true)
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    @XmlID
    @XmlSchemaType(name = "ID")
    protected String id;


    public Element getAny() {
        return any;
    }


    public void setAny(Element value) {
        this.any = value;
    }


    public String getId() {
        return id;
    }


    public void setId(String value) {
        this.id = value;
    }

}

1 个答案:

答案 0 :(得分:0)

我能够通过改编书写包装来解决问题:

import java.util.ArrayList;
import java.util.List;

import javax.xml.namespace.NamespaceContext;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

class WrapperXMLStreamWriter implements XMLStreamWriter {

private final XMLStreamWriter writer;

public WrapperXMLStreamWriter(XMLStreamWriter writer) {
    this.writer = writer;
}

private List<String> namespaces = new ArrayList<String>();

public void init() {
    namespaces.clear();
}

@Override
public void writeStartElement(String localName) throws XMLStreamException {
    init();
    writer.writeStartElement(localName);

}

@Override
public void writeStartElement(String namespaceURI, String localName) throws XMLStreamException {
    init();
    writer.writeStartElement(namespaceURI, localName);
}

@Override
public void writeStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {
    init();
    if (prefix.equalsIgnoreCase("xsi") || prefix.equalsIgnoreCase("xs")) {
        writer.writeStartElement(prefix, localName, namespaceURI);
    } else {
        writer.writeStartElement("", localName, namespaceURI);
    }
}

@Override
public void writeNamespace(String prefix, String namespaceURI) throws XMLStreamException {
    if (namespaces.contains(namespaceURI)) {
        return;
    }
    namespaces.add(namespaceURI);
    if (prefix.equalsIgnoreCase("xsi") || prefix.equalsIgnoreCase("xs")) {
        writer.writeNamespace(prefix, namespaceURI);
    } else {
        writer.writeNamespace("", namespaceURI);
    }
}

/*
 * (non-Javadoc)
 * @see javax.xml.stream.XMLStreamWriter#close()
 */
@Override
public void close() throws XMLStreamException {
    this.writer.close();
}

/*
 * (non-Javadoc)
 * @see javax.xml.stream.XMLStreamWriter#flush()
 */
@Override
public void flush() throws XMLStreamException {
    this.writer.flush();

}

/*
 * (non-Javadoc)
 * @see javax.xml.stream.XMLStreamWriter#getNamespaceContext()
 */
@Override
public NamespaceContext getNamespaceContext() {
    return this.writer.getNamespaceContext();
}

/*
 * (non-Javadoc)
 * @see javax.xml.stream.XMLStreamWriter#getPrefix(java.lang.String)
 */
@Override
public String getPrefix(String arg0) throws XMLStreamException {
    return this.writer.getPrefix(arg0);
}

/*
 * (non-Javadoc)
 * @see javax.xml.stream.XMLStreamWriter#getProperty(java.lang.String)
 */
@Override
public Object getProperty(String arg0) throws IllegalArgumentException {
    return this.writer.getProperty(arg0);
}

/*
 * (non-Javadoc)
 * @see javax.xml.stream.XMLStreamWriter#setDefaultNamespace(java.lang.String)
 */
@Override
public void setDefaultNamespace(String arg0) throws XMLStreamException {
    this.writer.setDefaultNamespace(arg0);
}

/*
 * (non-Javadoc)
 * @see javax.xml.stream.XMLStreamWriter#setNamespaceContext(javax.xml.namespace.NamespaceContext)
 */
@Override
public void setNamespaceContext(NamespaceContext arg0) throws XMLStreamException {
    this.writer.setNamespaceContext(arg0);
}

/*
 * (non-Javadoc)
 * @see javax.xml.stream.XMLStreamWriter#setPrefix(java.lang.String, java.lang.String)
 */
@Override
public void setPrefix(String arg0, String arg1) throws XMLStreamException {
    this.writer.setPrefix(arg0, arg1);
}

/*
 * (non-Javadoc)
 * @see javax.xml.stream.XMLStreamWriter#writeAttribute(java.lang.String, java.lang.String)
 */
@Override
public void writeAttribute(String arg0, String arg1) throws XMLStreamException {
    // TODO Auto-generated method stub
    this.writer.writeAttribute(arg0, arg1);
}

/*
 * (non-Javadoc)
 * @see javax.xml.stream.XMLStreamWriter#writeAttribute(java.lang.String, java.lang.String, java.lang.String)
 */
@Override
public void writeAttribute(String arg0, String arg1, String arg2) throws XMLStreamException {
    this.writer.writeAttribute(arg0, arg1, arg2);
}

/*
 * (non-Javadoc)
 * @see javax.xml.stream.XMLStreamWriter#writeAttribute(java.lang.String, java.lang.String, java.lang.String,
 * java.lang.String)
 */
@Override
public void writeAttribute(String arg0, String arg1, String arg2, String arg3) throws XMLStreamException {
    this.writer.writeAttribute(arg0, arg1, arg2, arg3);
}

/*
 * (non-Javadoc)
 * @see javax.xml.stream.XMLStreamWriter#writeCData(java.lang.String)
 */
@Override
public void writeCData(String arg0) throws XMLStreamException {
    this.writer.writeCData(arg0);
}

/*
 * (non-Javadoc)
 * @see javax.xml.stream.XMLStreamWriter#writeCharacters(java.lang.String)
 */
@Override
public void writeCharacters(String arg0) throws XMLStreamException {
    this.writer.writeCharacters(arg0);
}

/*
 * (non-Javadoc)
 * @see javax.xml.stream.XMLStreamWriter#writeCharacters(char[], int, int)
 */
@Override
public void writeCharacters(char[] arg0, int arg1, int arg2) throws XMLStreamException {
    this.writer.writeCharacters(arg0, arg1, arg2);
}

/*
 * (non-Javadoc)
 * @see javax.xml.stream.XMLStreamWriter#writeComment(java.lang.String)
 */
@Override
public void writeComment(String arg0) throws XMLStreamException {
    this.writer.writeComment(arg0);
}

/*
 * (non-Javadoc)
 * @see javax.xml.stream.XMLStreamWriter#writeDTD(java.lang.String)
 */
@Override
public void writeDTD(String arg0) throws XMLStreamException {
    this.writer.writeDTD(arg0);
}

/*
 * (non-Javadoc)
 * @see javax.xml.stream.XMLStreamWriter#writeDefaultNamespace(java.lang.String)
 */
@Override
public void writeDefaultNamespace(String arg0) throws XMLStreamException {
    this.writer.writeDefaultNamespace(arg0);
}

/*
 * (non-Javadoc)
 * @see javax.xml.stream.XMLStreamWriter#writeEmptyElement(java.lang.String)
 */
@Override
public void writeEmptyElement(String arg0) throws XMLStreamException {
    this.writer.writeEmptyElement(arg0);
}

/*
 * (non-Javadoc)
 * @see javax.xml.stream.XMLStreamWriter#writeEmptyElement(java.lang.String, java.lang.String)
 */
@Override
public void writeEmptyElement(String arg0, String arg1) throws XMLStreamException {
    this.writer.writeEmptyElement(arg0, arg1);
}

/*
 * (non-Javadoc)
 * @see javax.xml.stream.XMLStreamWriter#writeEmptyElement(java.lang.String, java.lang.String, java.lang.String)
 */
@Override
public void writeEmptyElement(String arg0, String arg1, String arg2) throws XMLStreamException {
    this.writer.writeEmptyElement(arg0, arg1, arg2);
}

/*
 * (non-Javadoc)
 * @see javax.xml.stream.XMLStreamWriter#writeEndDocument()
 */
@Override
public void writeEndDocument() throws XMLStreamException {
    this.writer.writeEndDocument();
}

/*
 * (non-Javadoc)
 * @see javax.xml.stream.XMLStreamWriter#writeEndElement()
 */
@Override
public void writeEndElement() throws XMLStreamException {
    this.writer.writeEndElement();
}

/*
 * (non-Javadoc)
 * @see javax.xml.stream.XMLStreamWriter#writeEntityRef(java.lang.String)
 */
@Override
public void writeEntityRef(String arg0) throws XMLStreamException {
    this.writer.writeEntityRef(arg0);
}

/*
 * (non-Javadoc)
 * @see javax.xml.stream.XMLStreamWriter#writeProcessingInstruction(java.lang.String)
 */
@Override
public void writeProcessingInstruction(String arg0) throws XMLStreamException {
    this.writer.writeProcessingInstruction(arg0);
}

/*
 * (non-Javadoc)
 * @see javax.xml.stream.XMLStreamWriter#writeProcessingInstruction(java.lang.String, java.lang.String)
 */
@Override
public void writeProcessingInstruction(String arg0, String arg1) throws XMLStreamException {
    this.writer.writeProcessingInstruction(arg0, arg1);
}

/*
 * (non-Javadoc)
 * @see javax.xml.stream.XMLStreamWriter#writeStartDocument()
 */
@Override
public void writeStartDocument() throws XMLStreamException {
    this.writer.writeStartDocument();
}

/*
 * (non-Javadoc)
 * @see javax.xml.stream.XMLStreamWriter#writeStartDocument(java.lang.String)
 */
@Override
public void writeStartDocument(String arg0) throws XMLStreamException {
    this.writer.writeStartDocument(arg0);
}

/*
 * (non-Javadoc)
 * @see javax.xml.stream.XMLStreamWriter#writeStartDocument(java.lang.String, java.lang.String)
 */
@Override
public void writeStartDocument(String arg0, String arg1) throws XMLStreamException {
    this.writer.writeStartDocument(arg0, arg1);
}

}

使用这种方式:

        XMLStreamWriter writer = new WrapperXMLStreamWriter(XMLOutputFactory.newInstance().createXMLStreamWriter(outputStream, encoding));
        final JAXBContext context = JAXBContext.newInstance(arquivoVO.getClass());
        final Marshaller marshaller = context.createMarshaller();
        marshaller.marshal(arquivoVO, writer);
        return outputStream.toByteArray();