Java Java - 6 months ago 25
Java Question

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

A4L A4L
Answer Source

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>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download