Java
Java

Reputation: 2489

Xml generatiton using java

I want to generate xml file in this format.

   <?xml version="1.0" encoding="UTF-8"?>  
    <Comprobante xmlns:cfdi="http://www.sat.gob.mx/cfd/3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0"  
        folio="779" fecha="2011-12-05T18:24:42"  
        sello="..."  
        formaDePago="PAGO EN UNA SOLA EXHIBICION" noCertificado="00001000000102160027" condicionesDePago="EFECTOS FISCALES AL PAGO"  
        subTotal="5123.23" total="5942.95" Moneda="USD" tipoDeComprobante="ingreso"  
        xsi:schemaLocation="http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv3.xsd">  
      <Emisor rfc="TIA050408342" nombre="TECNOLOGIAS DE INFORMACION AMERICA S.A. DE C.V.">  
        <DomicilioFiscal calle="Montecito" noExterior="38" noInterior="P 32-5" colonia="Napoles" localidad="Distrito Federal"  
            municipio="Distrito Federal" estado="México" pais="Mex." codigoPostal="03810" />  
      </Emisor>  
      <Addenda>  
        <ElementosClientesNormales xmlns:ext="http://www.buzone.com.mx/XSD/Sender19877/A"  
            xsi:schemaLocation="http://www.buzone.com.mx/XSD/Sender19877/A http://www.buzone.com.mx/XSD/Sender19877/A/Addenda.xsd">  
          <Conector>TEX9302097F3</Conector>  
          <CadenaOriginal>...about 1000 characters...</CadenaOriginal>  
      </Addenda>  
    </Comprobante> 

As we know we ca create xml file using SAX parser or JAXB or dom4j

but still I am not able to generate tags like Comprobante:cfdi and add elements with it like folio="774" and so on.

Also how we can create tags like Emisor:cfdi rfc

How can I create such xml creation using java.? Any blog , tutorial that explains the way to create such xml file.

Thanks, Ran

Upvotes: 0

Views: 598

Answers (3)

estepuma
estepuma

Reputation: 11

  1. You have to download the version of xsd that you want from SAT page (http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv33.xsd)
  2. Create a directory where the java classes will be created. (ex. testing)
  3. Execute the command from java jdk: xjc -d ./testing/ cfdv33.xsd
  4. You'll get some warnings from enums, but basically you'll get java classes with jaxb annotations.
  5. Create a "Comprobante" for test with the java classes generated previously
  6. Do a marshal
ObjectFactory of = new ObjectFactory();
Comprobante comprobante = of.createComprobante();
comprobante.set...
...
DocumentBuilderFactory docBuilderFac = DocumentBuilderFactory.newInstance(); 
docBuilderFac.setNamespaceAware(true);
DocumentBuilder db = docBuilderFac.newDocumentBuilder();
Document doc = db.newDocument();
marshaller.marshal(comprobante,doc);
...

I have a project that you can use like a reference and maybe improve it.

Upvotes: 1

A4L
A4L

Reputation: 17595

You could try to create elements and attributes with namspace.

Here is a sample code with uses the standard java xml api

@Test
public void genXmlWithNamespace() throws ParserConfigurationException, TransformerFactoryConfigurationError, TransformerException {

    String nsURI = "http://example.com/foo";

    DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = docBuilder.newDocument();
    doc.setXmlStandalone(true);
    Element root = doc.createElementNS(nsURI, "foo:company");
    doc.appendChild(root);

    root.setAttributeNS(nsURI, "foo:name", "example");

    Element e = null;

    e = doc.createElementNS(nsURI, "foo:employee");
    e.setAttributeNS(nsURI, "foo:id", "1");
    e.setTextContent("John Doe");
    root.appendChild(e);

    e = doc.createElementNS(nsURI, "foo:employee");
    e.setAttributeNS(nsURI, "foo:id", "2");
    e.setTextContent("John Smith");
    root.appendChild(e);

    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    Result output = new StreamResult(System.out);
    Source input = new DOMSource(doc);
    transformer.transform(input, output);
}

Output

<?xml version="1.0" encoding="UTF-8"?>
<foo:company xmlns:foo="http://example.com/foo" foo:name="example">
    <foo:employee foo:id="1">John Doe</foo:employee>
    <foo:employee foo:id="2">John Smith</foo:employee>
</foo:company>

Upvotes: 1

Rama Krishna Sanjeeva
Rama Krishna Sanjeeva

Reputation: 427

From the xml, i see it refers to XML schema files http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv3.xsd and http://www.buzone.com.mx/XSD/Sender19877/A/Addenda.xsd. Download those schemas and use JAXB for generating the Java classes. Use JAXB for generating the xml and this should be compatible with what's expected. Else, it does not make sense for the xsd references.

If you cannot get those xsd's, then the option will be as follows. 1. Use trang library to generate xsd from the above xml (http://www.dotkam.com/2008/05/28/generate-xsd-from-xml/). 2. Cleanup the XSD. XSD generated from trang may not be always right. 3. Generate JAXB classes from the xsd using xjc 4. Modify the JAXB classes if required to associate any namespace.

Upvotes: 1

Related Questions