Gerar XML + SOAP

1 resposta
nandobgi

Pessoal boa tarde,

Estou precisando gerar uma solicitação SOAP para o CTe, como estou utilizando JAXB para gerar toda a construção dos xml exigidos pelo CTe tentei usar o mesmo para criar uma mensagem SOAP.
Consegui gerar as classes Envelope, Header, Body etc, não estou sabendo como posso setar informações nas tags Header e Body.

Body

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.1-b02-fcs 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2009.09.04 at 03:20:38 PM GMT-03:00 
//


package br.com.sensatta.cte.schema.soap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAnyAttribute;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.namespace.QName;
import org.w3c.dom.Element;


/**
 * <p>Java class for Body complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * &lt;pre&gt;
 * &lt;complexType name="Body"&gt;
 *   &lt;complexContent&gt;
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"&gt;
 *       &lt;sequence&gt;
 *         &lt;any/&gt;
 *       &lt;/sequence&gt;
 *     &lt;/restriction&gt;
 *   &lt;/complexContent&gt;
 * &lt;/complexType&gt;
 * &lt;/pre&gt;
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Body", propOrder = {
    "any"
})
public class Body {

    @XmlAnyElement(lax = true)
    protected List&lt;Object&gt; any;
    @XmlAnyAttribute
    private Map&lt;QName, String&gt; otherAttributes = new HashMap&lt;QName, String&gt;();

    /**
     * Gets the value of the any property.
     * 
     * <p>
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a &lt;CODE&gt;set&lt;/CODE&gt; method for the any property.
     * 
     * <p>
     * For example, to add a new item, do as follows:
     * &lt;pre&gt;
     *    getAny().add(newItem);
     * &lt;/pre&gt;
     * 
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link Element }
     * {@link Object }
     * 
     * 
     */
    public List&lt;Object&gt; getAny() {
        if (any == null) {
            any = new ArrayList&lt;Object&gt;();
        }
        return this.any;
    }

    /**
     * Gets a map that contains attributes that aren't bound to any typed property on this class.
     * 
     * <p>
     * the map is keyed by the name of the attribute and 
     * the value is the string value of the attribute.
     * 
     * the map returned by this method is live, and you can add new attribute
     * by updating the map directly. Because of this design, there's no setter.
     * 
     * 
     * @return
     *     always non-null
     */
    public Map&lt;QName, String&gt; getOtherAttributes() {
        return otherAttributes;
    }

}

A dúvida está nessa annotation @XmlAnyElement, tentei passar meu Bean que representa um xml de consulta do status do serviço mas não deu certo.

Grato.

[ ]'s

1 Resposta

nandobgi

Usando o método header.getOtherAttributes().put( new QName(""), cabec) consegui isso:

&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
&lt;Envelope xmlns="http://www.w3.org/2003/05/soap-envelope"&gt;
    &lt;Header ="&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
&lt;cabecMsg versao="1.01" xmlns="http://www.portalfiscal.inf.br/nfe"&gt;
    &lt;versaoDados&gt;1.01&lt;/versaoDados&gt;
&lt;/cabecMsg&gt;
"/&gt;
    &lt;Body ="&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
&lt;consStatServCte versao="1.01" xmlns="http://www.portalfiscal.inf.br/cte"&gt;
    &lt;tpAmb&gt;2&lt;/tpAmb&gt;
    &lt;xServ&gt;STATUS&lt;/xServ&gt;
&lt;/consStatServCte&gt;
"/&gt;
&lt;/Envelope&gt;

Valeu

Mas preciso que o meu xml fique com essa estrutura

&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"&gt;&lt;soap12:Header&gt;&lt;cteCabecMsg xmlns="http://www.portalfiscal.inf.br/cte/wsdl/CteStatusServico"&gt;&lt;cUF&gt;51&lt;/cUF&gt;&lt;versaoDados&gt;1.01&lt;/versaoDados&gt;&lt;/cteCabecMsg&gt;&lt;/soap12:Header&gt;&lt;soap12:Body&gt;&lt;cteDadosMsg xmlns="http://www.portalfiscal.inf.br/cte/wsdl/CteStatusServico"&gt;&lt;consStatServCte xmlns="http://www.portalfiscal.inf.br/cte" versao="1.01"&gt;&lt;tpAmb&gt;2&lt;/tpAmb&gt;&lt;xServ&gt;STATUS&lt;/xServ&gt;&lt;/consStatServCte&gt;&lt;/cteDadosMsg&gt;&lt;/soap12:Body&gt;&lt;/soap12:Envelope&gt;
Criado 8 de setembro de 2009
Ultima resposta 8 de set. de 2009
Respostas 1
Participantes 1