ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Article:
  Generating an XML Document with JAXB
Subject:   Root element not generated
Date:   2006-12-14 08:26:48
From:   jeroenBos
Hi all,


i first was using jaxb 2.0 for marshalling to xml. This worked fine a produced a nice xml string with a root element. the problem is that i can not use jre 1.5 but have to use 1.4x instead so i also need a older jaxb impl.


well no worries. now using an older impl. and still getting a XML string only without a root element.


i can not do this:
JAXBElement<nl.waternet.schemas.wml_soap_debiteur_v0001.EnvelopeType> oElement=oDebFactory.createEnvelope((nl.waternet.schemas.wml_soap_debiteur_v0001.EnvelopeType)oDebEnvelop);
because oDebFactory.createEnvelope does not allow any input params.


Please give some help here.


here is the rest of my code:
package coda.nl.wxml.helpers;


import java.io.File;
import java.io.StringReader;
import java.io.StringWriter;
import java.math.BigInteger;
import java.rmi.server.UID;
import java.util.Calendar;
import java.util.List;


import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;



import nl.waternet.schemas.wgd_types_v0001.impl.AdresImpl;
import nl.waternet.schemas.wgd_types_v0001.impl.RekeningNummerImpl;
import nl.waternet.schemas.wgd_types_v0001.impl.AdresImpl.HuisNummerToevoegingTypeImpl;
import nl.waternet.schemas.wml_crediteur_v0001.WMLCrediteur;
import nl.waternet.schemas.wml_crediteur_v0001.impl.WMLCrediteurTypeImpl.ContactPersoonTypeImpl;


import nl.waternet.schemas.wml_debiteur_v0001.impl.WMLDebiteurTypeImpl;


import nl.waternet.schemas.wml_header_v0001.impl.WMLHeaderTypeImpl;


import nl.waternet.schemas.wml_soap_crediteur_v0001.Envelope;
import nl.waternet.schemas.wml_soap_crediteur_v0001.EnvelopeType;
import nl.waternet.schemas.wml_soap_crediteur_v0001.ObjectFactory;
import nl.waternet.schemas.wml_soap_crediteur_v0001.EnvelopeType.BodyType;
import nl.waternet.schemas.wml_soap_crediteur_v0001.EnvelopeType.HeaderType;
import nl.waternet.schemas.wml_soap_crediteur_v0001.impl.EnvelopeTypeImpl;
import nl.waternet.schemas.wml_soap_debiteur_v0001.impl.EnvelopeImpl;


import coda.nl.creddeb.DebCred;
import coda.nl.general.GenericFunctions;
import coda.nl.general.ValidationEventHandlerHelper;
import coda.nl.properties.Properties;



/* -------------------------------------------------------------------------------------------------------------------------------------------------
* Version Date Description
* -------------------------------------------------------------------------------------------------------------------------------------------------
* 1.0 08-12-2006 this class create a wmlenvelop
*/
/**
* @Description this class create a wmlenvelop
* @author bossenbroekj
* @Date 08-12-2006
* @version 1.0
**/
public class WMLEnvelopeHelper
{
private String GENEVELOPEERROR="Error generating evelope from DebCred Object. Message: ";
private String VALIDATIONERROR="Error validating xml WMLEnvelope. Message: ";
private DebCred m_oDebCred=null;
private Properties m_oProperties=null;

public WMLEnvelopeHelper(DebCred p_oDebCredObj, Properties p_oProperties)
{
m_oDebCred=p_oDebCredObj;
m_oProperties=p_oProperties;
}

public String generateEnvelopeFromDebCredObj()throws Exception
{
String sDebCredEnvelopeXMLStr="";
UID oUID = new UID();
AdresImpl oVestigingsAdres=null;
AdresImpl oPostAdres=null;
HuisNummerToevoegingTypeImpl oVestHuisnummerToevoeging=null;
HuisNummerToevoegingTypeImpl oPostHuisnummerToevoeging=null;
RekeningNummerImpl oRekeningNummer =null;

ContactPersoonTypeImpl oCreditorContactPersoon=null;
nl.waternet.schemas.wml_debiteur_v0001.impl.WMLDebiteurTypeImpl.ContactPersoonTypeImpl oDebitorContactPersoon=null;
BigInteger biVHuisnummer = null;
BigInteger biPHuisnummer = null;
WMLCrediteurHelper oCrediteurHelper=null;
WMLDebiteurHelper oDebiteurHelper=null;
WMLCrediteur oCrediteur=null;
WMLDebiteurTypeImpl oDebiteur=null;
WMLHeaderTypeImpl oWmlHeader=null;
String sMinorVersieDeb=m_oProperties.getMinorVersieDeb();
String sMajorVersieDeb=m_oProperties.getMajorVersieDeb();
String sMajorVersieCred=m_oProperties.getMajorVersieCred();
String sMinorVersieCred=m_oProperties.getMinorVersieCred();
String sExternid=m_oProperties.getExternId();
String sInterId=oUID.toString();//maybe property?
String sAppNaamOntvanger=m_oProperties.getAppNaamOnt();
String sAuthenticate=m_oProperties.getAutentificatieOnt();
String sGebruikerOntvanger=m_oProperties.getGebruikerOnt();
String sAppNaamVerzender=m_oProperties.getAppNaamVer();
String sGebruikerVerzender=m_oProperties.getGebruikerVer();
Calendar oStartDate =null;
Calendar oEndDate =null;

JAXBContext oContext= null;
Marshaller oMarshaller=null;
StringWriter oWriter=null;


try
{
//set startdate
oStartDate=GenericFunctions.oCreateCalendarFromSystemDate();
//set enddate after object has been generated
//generate wmlheade

oWmlHeader=new WMLHeaderTypeImpl();
WMLHeaderHelper oWMLHeaderHelper=new WMLHeaderHelper();
oWMLHeaderHelper.setOntvanger(oWmlHeader,sAppNaamOntvanger,sAuthenticate,sGebruikerOntvanger);

//generate body
//set addresses
//vestigingsadres
oVestigingsAdres=new AdresImpl();
//set bigint huisnummer
biVHuisnummer = new BigInteger(m_oDebCred.getVHUISNUMMER());
oVestigingsAdres.setHuisNummer(biVHuisnummer);
if(!(m_oDebCred.getVHUISNUMMERTOEVOEGING()==null||m_oDebCred.getVHUISNUMMERTOEVOEGING().equals("")))
{
oVestHuisnummerToevoeging=new HuisNummerToevoegingTypeImpl();
oVestHuisnummerToevoeging.setWaternetToevoeging(m_oDebCred.getVHUISNUMMERTOEVOEGING());
oVestigingsAdres.setHuisNummerToevoeging(oVestHuisnummerToevoeging);
}
oVestigingsAdres.setLandCode(m_oDebCred.getVLANDCODE());
oVestigingsAdres.setPlaatsNaam(m_oDebCred.getVPLAATSNAAM());
oVestigingsAdres.setPostCode(m_oDebCred.getVPOSTCODE());
oVestigingsAdres.setStraatNaam(m_oDebCred.getVSTRAATNAAM());
//postadres
oPostAdres=new AdresImpl();
//set bigint huisnummer
biPHuisnummer = new BigInteger(m_oDebCred.getPHUISNUMMER());
oPostAdres.setStraatNaam(m_oDebCred.getPSTRAATNAAM());
oPostAdres.setHuisNummer(biPHuisnummer);
if(!(m_oDebCred.getPHUISNUMMERTOEVOEGING()==null||m_oDebCred.getPHUISNUMMERTOEVOEGING().equals("")))
{
oPostHuisnummerToevoeging=new HuisNummerToevoegingTypeImpl();
oPostHuisnummerToevoeging.setWaternetToevoeging(m_oDebCred.getPHUISNUMMERTOEVOEGING());
oPostAdres.setHuisNummerToevoeging(oPostHuisnummerToevoeging);
}
oPostAdres.setLandCode(m_oDebCred.getPLANDCODE());
oPostAdres.setPlaatsNaam(m_oDebCred.getPPLAATSNAAM());
oPostAdres.setPostCode(m_oDebCred.getPPOSTCODE());
//set accountnumber
//is postbank or bank
oRekeningNummer=new RekeningNummerImpl();
if(m_oDebCred.getPOSTBANK().equalsIgnoreCase("J"))
{
oRekeningNummer.setGiroRekeningNummer(m_oDebCred.getREKENINGNUMMER());
}
else
{
oRekeningNummer.setBankRekeningNummer(m_oDebCred.getREKENINGNUMMER());
}


//append to debitor or creditor
//wmlcreditor or wmldebtor
if(m_oDebCred.getBERICHTTYPE().equalsIgnoreCase("Crediteur"))
{
ObjectFactory oFactory=new ObjectFactory();
EnvelopeTypeImpl oEnvelop=new EnvelopeTypeImpl();
BodyType oBody=oFactory.createEnvelopeTypeBodyType();

oCrediteurHelper=new WMLCrediteurHelper();
//check contactpersoon
if(!(m_oDebCred.getCONTACTPNAAM()==null||m_oDebCred.getCONTACTPNAAM().equals("")))
{
oCreditorContactPersoon=new ContactPersoonTypeImpl();
oCreditorContactPersoon.setFaxNummer(m_oDebCred.getCONTACTPFAXNUMMER());
oCreditorContactPersoon.setWerkTelefoonNummer(m_oDebCred.getCONTACTPWERKTELEFOON());
oCreditorContactPersoon.setNaam(m_oDebCred.getCONTACTPNAAM());
}

oCrediteur=oCrediteurHelper.generateWMLCreditor(m_oDebCred.getCREDDEBNUM(),m_oDebCred.getCREDDEBNAAM(),m_oDebCred.getWERKTELEFOON(),m_oDebCred.getFAXNUMMER(),oVestigingsAdres,oPostAdres,oRekeningNummer,oCreditorContactPersoon,m_oDebCred.getINDGEBLOKKEERD());

//set body
oBody.setWMLCrediteur(oCrediteur);
//set header
HeaderType oHeader=oFactory.createEnvelopeTypeHeaderType();

//set berichtgegevens
oWMLHeaderHelper.setBerichtGegevens(oWmlHeader,"PUBLISH","Crediteur",sExternid,sInterId,sMajorVersieCred,sMinorVersieCred,m_oDebCred.getMUTATIESOORT());
//first set enddate
oEndDate=GenericFunctions.oCreateCalendarFromSystemDate();
oWMLHeaderHelper.setVerzender(oWmlHeader,sAppNaamVerzender,oStartDate,oEndDate,sGebruikerVerzender);
oHeader.setWMLHeader(oWmlHeader);
oEnvelop.setHeader(oHeader);
oEnvelop.setBody(oBody);
//end header
//set sDebCredEnvelopeXMLStr
oContext = JAXBContext.newInstance("nl.waternet.schemas.wml_soap_crediteur_v0001");
oMarshaller = oContext.createMarshaller();
oMarshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
oWriter = new StringWriter();
oMarshaller.marshal(oEnvelop,oWriter) ;
sDebCredEnvelopeXMLStr=oWriter.toString();
oWriter.close();
}
else
{
nl.waternet.schemas.wml_soap_debiteur_v0001.ObjectFactory oDebFactory=new nl.waternet.schemas.wml_soap_debiteur_v0001.ObjectFactory();
//nl.waternet.schemas.wml_soap_debiteur_v0001.impl.EnvelopeTypeImpl oDebEnvelop=new nl.waternet.schemas.wml_soap_debiteur_v0001.impl.EnvelopeTypeImpl();
nl.waternet.schemas.wml_soap_debiteur_v0001.EnvelopeType.BodyType oDebBody=oDebFactory.createEnvelopeTypeBodyType();
//nl.waternet.schemas.wml_soap_debiteur_v0001
oDebiteurHelper=new WMLDebiteurHelper();

nl.waternet.schemas.wml_soap_debiteur_v0001.impl.EnvelopeTypeImpl oDebEnvelop=(nl.waternet.schemas.wml_soap_debiteur_v0001.impl.EnvelopeTypeImpl)(oDebFactory.createEnvelopeType());


//check contactpersoon
if(!(m_oDebCred.getCONTACTPNAAM()==null||m_oDebCred.getCONTACTPNAAM().equals("")))
{
oDebitorContactPersoon=new nl.waternet.schemas.wml_debiteur_v0001.impl.WMLDebiteurTypeImpl.ContactPersoonTypeImpl();
oDebitorContactPersoon.setFaxNummer(m_oDebCred.getCONTACTPFAXNUMMER());
oDebitorContactPersoon.setWerkTelefoonNummer(m_oDebCred.getCONTACTPWERKTELEFOON());
oDebitorContactPersoon.setNaam(m_oDebCred.getCONTACTPNAAM());
}
oDebiteur =oDebiteurHelper.generateWMLDebtor(m_oDebCred.getCREDDEBNUM(),m_oDebCred.getCREDDEBNAAM(),m_oDebCred.getWERKTELEFOON(),m_oDebCred.getFAXNUMMER(),oVestigingsAdres,oPostAdres,oRekeningNummer,oDebitorContactPersoon,m_oDebCred.getINDGEBLOKKEERD());

//set body
oDebBody.setWMLDebiteur(oDebiteur);
//set header
nl.waternet.schemas.wml_soap_debiteur_v0001.EnvelopeType.HeaderType oDebHeader=oDebFactory.createEnvelopeTypeHeaderType();

oWMLHeaderHelper.setBerichtGegevens(oWmlHeader,"PUBLISH","DEBITEUR",sExternid,sInterId,sMajorVersieDeb,sMinorVersieDeb,m_oDebCred.getMUTATIESOORT());

//first set enddate
oEndDate=GenericFunctions.oCreateCalendarFromSystemDate();
oWMLHeaderHelper.setVerzender(oWmlHeader,sAppNaamVerzender,oStartDate,oEndDate,sGebruikerVerzender);
oDebHeader.setWMLHeader(oWmlHeader);
oDebEnvelop.setHeader(oDebHeader);
oDebEnvelop.setBody(oDebBody);

//end header
//set sDebCredEnvelopeXMLStr
oContext = JAXBContext.newInstance("nl.waternet.schemas.wml_soap_debiteur_v0001");
oMarshaller = oContext.createMarshaller();
oMarshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
oWriter = new StringWriter();

//JAXBElement<nl.waternet.schemas.wml_soap_debiteur_v0001.EnvelopeType> oElement=oDebFactory.createEnvelope((nl.waternet.schemas.wml_soap_debiteur_v0001.EnvelopeType)oDebEnvelop);
//marshaller.marshal(catalogElement, new FileOutputStream(xmlDocument));



oMarshaller.marshal(oDebEnvelop,oWriter) ;
sDebCredEnvelopeXMLStr=oWriter.toString();
oWriter.close();
}

//end body


}
catch (Exception oEx)
{
throw new Exception(GenericFunctions.generateLogMessage("WMLEnvelopeHelper","generateEnvelopeFromDebCredObj",GENEVELOPEERROR+oEx.getMessage()));
}
return sDebCredEnvelopeXMLStr;
}
public boolean validateXmlEnvelope(String p_sEnvelope, String p_sBerichtTypeInstance, String p_sSchemaLocation)throws Exception
{
boolean bIsValid=true;
Unmarshaller oUnmarshaller=null;
JAXBContext oContext=null;
try
{
//validate xml
oContext = JAXBContext.newInstance(p_sBerichtTypeInstance);
oUnmarshaller = oContext.createUnmarshaller();
SAXParserFactory oSAXParserFactory = SAXParserFactory.newInstance();
SchemaFactory oSf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema oSchema = oSf.newSchema(new File(p_sSchemaLocation));
oSAXParserFactory.setSchema(oSchema);
oSAXParserFactory.setValidating(true);

oUnmarshaller.setSchema(oSchema);
ValidationEventHandlerHelper oValidateEventHandler=new ValidationEventHandlerHelper();


oUnmarshaller.setEventHandler(oValidateEventHandler);
oUnmarshaller.unmarshal(new StreamSource(new StringReader(p_sEnvelope)));

if(!oValidateEventHandler.bIsValid)
{

throw new Exception(oValidateEventHandler.sValidationMessage);
}
}
catch (Exception oEx)
{

throw new Exception(GenericFunctions.generateLogMessage("WMLEnvelopeHelper","validateXmlEnvelope",VALIDATIONERROR+oEx.getMessage()));
}
return bIsValid;
}
}




1 to 2 of 2
  1. Root element not generated
    2006-12-14 08:33:20  Deepak Vohra | [View]

  2. Root element not generated
    2006-12-14 08:31:00  Deepak Vohra | [View]

    • Root element not generated
      2006-12-14 09:05:44  jeroenBos [View]

1 to 2 of 2