0

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

4

3 回答 3

1

从 xml 中,我看到它指的是 XML 模式文件http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv3.xsdhttp ://www.buzone.com.mx/XSD/Sender19877/A/Addenda.xsd。下载这些模式并使用 JAXB 生成 Java 类。使用 JAXB 生成 xml,这应该与预期兼容。否则,它对 xsd 引用没有意义。

如果您无法获得那些 xsd,则选项如下。1. 使用 trang 库从上述 xml 生成 xsd ( http://www.dotkam.com/2008/05/28/generate-xsd-from-xml/ )。2. 清理 XSD。从 trang 生成的 XSD 可能并不总是正确的。3. 使用 xjc 从 xsd 生成 JAXB 类 4. 如果需要,修改 JAXB 类以关联任何命名空间。

于 2013-05-09T14:19:42.540 回答
1

您可以尝试使用namspace.

这是使用标准 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);
}

输出

<?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>
于 2013-05-09T14:55:04.323 回答
1
  1. 您必须从 SAT 页面 ( http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv33.xsd )下载所需的 xsd 版本
  2. 创建一个将在其中创建 java 类的目录。(例如测试)
  3. 从 java jdk 执行命令:xjc -d ./testing/cfdv33.xsd
  4. 你会从枚举中得到一些警告,但基本上你会得到带有 jaxb 注释的 java 类。
  5. 使用之前生成的 java 类创建一个“Comprobante”进行测试
  6. 做元帅
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);
...

我有一个项目,你可以像参考一样使用它,也许可以改进它。

于 2017-08-16T20:50:25.570 回答