将XML节点处理为独立XML文档,并在原始XML文档中更新该节点

MatíasW.

我需要将一个XML节点处理为独立的XML,向Node Document添加一个新标签,并使用新的节点信息更新原始XML文档。欢迎任何帮助,建议或教程。

这是原始的XML:

    <ENVOLVENTE id="ENVOLVENTE">
<FirmaEmpresa>
    <FirmaDonante>
        <Firma>
            <Relacion>
                <RelacionId>32490342093249090234</RelacionId>
            </Relacion>
            <Formulario>
                <Donante>
                    <DonanteNombre>Gloria Robles</DonanteNombre>
                    <DonanteCorreo>[email protected]</DonanteCorreo>
                </Donante>
                <Beneficiado>
                    <BeneficiarioPais>USA</BeneficiarioPais>
                    <BeneficiadoCorreo>[email protected]</BeneficiadoCorreo>
                </Beneficiado>
                <Fabricantes>
                    <Fabricante>
                        <FabricanteNumeroOrden>1</FabricanteNumeroOrden>
                        <FabricantePais>MX</FabricantePais>
                        <FabricanteCorreo>[email protected]</FabricanteCorreo>
                    </Fabricante>
                </Fabricantes>
                <ListaDonaciones>
                    <Donaciones>
                        <DonacionesNumeroOrden>1</DonacionesNumeroOrden>
                        <DonacionesProductoId>nombre</DonacionesProductoId>
                        <DonacionesCantidadDonada>100</DonacionesCantidadDonada>
                        <DonacionesFechaDonacion>2016-12-29T12:21:16</DonacionesFechaDonacion>
                    </Donaciones>
                </ListaDonaciones>
            </Formulario>
        </Firma>
    </FirmaDonante>
    <Empresa>
        <EmpresaPais>MX</EmpresaPais>
        <EmpresaNombre>Donaciones A.C </EmpresaNombre>
        <EmpresaDirecccion>AV. REFORMA 1900</EmpresaDirecccion>
        <EmpresaCiudad>CDXM</EmpresaCiudad>
    </Empresa>
    <PermisoEmpresa>
        <PermisoNumero>329023409324902349023409234</PermisoNumero>
    </PermisoEmpresa>
</FirmaEmpresa>
</ENVOLVENTE>

现在,我需要将节点“ FirmaDonante”提取到新的XML DOM:

 <FirmaDonante>
        <Firma>
            <Relacion>
                <RelacionId>32490342093249090234</RelacionId>
            </Relacion>
            <Formulario>
                <Donante>
                    <DonanteNombre>Gloria Robles</DonanteNombre>
                    <DonanteCorreo>[email protected]</DonanteCorreo>
                </Donante>
                <Beneficiado>
                    <BeneficiarioPais>USA</BeneficiarioPais>
                    <BeneficiadoCorreo>[email protected]</BeneficiadoCorreo>
                </Beneficiado>
                <Fabricantes>
                    <Fabricante>
                        <FabricanteNumeroOrden>1</FabricanteNumeroOrden>
                        <FabricantePais>MX</FabricantePais>
                        <FabricanteCorreo>[email protected]</FabricanteCorreo>
                    </Fabricante>
                </Fabricantes>
                <ListaDonaciones>
                    <Donaciones>
                        <DonacionesNumeroOrden>1</DonacionesNumeroOrden>
                        <DonacionesProductoId>nombre</DonacionesProductoId>
                        <DonacionesCantidadDonada>100</DonacionesCantidadDonada>
                        <DonacionesFechaDonacion>2016-12-29T12:21:16</DonacionesFechaDonacion>
                    </Donaciones>
                </ListaDonaciones>
            </Formulario>
        </Firma>
    </FirmaDonante>

之后,我将节点修改为新的XML文档,类似这样,在原始Node之后添加新的XML元素。

        <FirmaDonante>
        <Firma>
            <Relacion>
                <RelacionId>32490342093249090234</RelacionId>
            </Relacion>
            <Formulario>
                <Donante>
                    <DonanteNombre>Gloria Robles</DonanteNombre>
                    <DonanteCorreo>[email protected]</DonanteCorreo>
                </Donante>
                <Beneficiado>
                    <BeneficiarioPais>USA</BeneficiarioPais>
                    <BeneficiadoCorreo>[email protected]</BeneficiadoCorreo>
                </Beneficiado>
                <Fabricantes>
                    <Fabricante>
                        <FabricanteNumeroOrden>1</FabricanteNumeroOrden>
                        <FabricantePais>MX</FabricantePais>
                        <FabricanteCorreo>[email protected]</FabricanteCorreo>
                    </Fabricante>
                </Fabricantes>
                <ListaDonaciones>
                    <Donaciones>
                        <DonacionesNumeroOrden>1</DonacionesNumeroOrden>
                        <DonacionesProductoId>nombre</DonacionesProductoId>
                        <DonacionesCantidadDonada>100</DonacionesCantidadDonada>
                        <DonacionesFechaDonacion>2016-12-29T12:21:16</DonacionesFechaDonacion>
                    </Donaciones>
                </ListaDonaciones>
            </Formulario>
        </Firma>
    </FirmaDonante>
    <Signature>
            <SignedInfo/>
            <KeyInfo/>
    </Signature>

最后,我需要使用新标签将Node文档添加到原始文档中与Node相同的位置:

    <ENVOLVENTE id="ENVOLVENTE">
<FirmaEmpresa>
    <FirmaDonante>
        <Firma>
            <Relacion>
                <RelacionId>32490342093249090234</RelacionId>
            </Relacion>
            <Formulario>
                <Donante>
                    <DonanteNombre>Gloria Robles</DonanteNombre>
                    <DonanteCorreo>[email protected]</DonanteCorreo>
                </Donante>
                <Beneficiado>
                    <BeneficiarioPais>USA</BeneficiarioPais>
                    <BeneficiadoCorreo>[email protected]</BeneficiadoCorreo>
                </Beneficiado>
                <Fabricantes>
                    <Fabricante>
                        <FabricanteNumeroOrden>1</FabricanteNumeroOrden>
                        <FabricantePais>MX</FabricantePais>
                        <FabricanteCorreo>[email protected]</FabricanteCorreo>
                    </Fabricante>
                </Fabricantes>
                <ListaDonaciones>
                    <Donaciones>
                        <DonacionesNumeroOrden>1</DonacionesNumeroOrden>
                        <DonacionesProductoId>nombre</DonacionesProductoId>
                        <DonacionesCantidadDonada>100</DonacionesCantidadDonada>
                        <DonacionesFechaDonacion>2016-12-29T12:21:16</DonacionesFechaDonacion>
                    </Donaciones>
                </ListaDonaciones>
            </Formulario>
        </Firma>
    </FirmaDonante>
    <!--NEW TAG -->
    <Signature>
            <SignedInfo/>
            <KeyInfo/>
    </Signature>
    <!--NEW TAG -->
    <Empresa>
        <EmpresaPais>MX</EmpresaPais>
        <EmpresaNombre>Donaciones A.C </EmpresaNombre>
        <EmpresaDirecccion>AV. REFORMA 1900</EmpresaDirecccion>
        <EmpresaCiudad>CDXM</EmpresaCiudad>
    </Empresa>
    <PermisoEmpresa>
        <PermisoNumero>329023409324902349023409234</PermisoNumero>
    </PermisoEmpresa>
</FirmaEmpresa>
</ENVOLVENTE>

实际上,我可以提取节点,但是当尝试在节点文档中添加新元素时出现错误:

            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);

        DocumentBuilder builder = dbf.newDocumentBuilder();
        InputStream stream = new ByteArrayInputStream(
                xmlFile.getBytes(StandardCharsets.UTF_8));
        Document document = builder.parse(stream);


         Element elementFirmaDonante =  (Element) document.getElementsByTagName("FirmaDonante").item(0);



        DocumentBuilder documentBuilder = dbf.newDocumentBuilder();       
        Document documentoCODExporterMasEH = documentBuilder.newDocument();


        Node newNode = documentoCODExporterMasEH.importNode(elementFirmaDonante, true);
        documentoCODExporterMasEH.appendChild(newNode);



        /*In this point all is OK, a can serialize de Document, but now, a can't add a new Item to the node document*/


        /*
         * This block, throws error:
         * HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted.
         */
        Element anotherElement =  (Element) document.getElementsByTagName("Empresa").item(0);
        Node anotherNewNode = documentoCODExporterMasEH.importNode(anotherElement, true);
        documentoCODExporterMasEH.insertBefore(anotherNewNode, newNode);

上面的代码只是为了测试我是否可以向DOM对象添加新的Elements或节点。

有什么建议么 ??

提前致谢!!!!!

阿米特·巴蒂(Amit Bhati)

参见下面的代码,我能够在Empresa节点之前插入一个新节点:

Element anotherElement  = (Element)document.getElementsByTagName("Empresa").item(0);
    Element newTag = document.createElement("Signature");
    Element childElem1=document.createElement("SignedInfo");
    Element childElem2=document.createElement("KeyInfo");
    newTag.appendChild(childElem1);newTag.appendChild(childElem2);
    anotherElement.getParentNode().insertBefore(newTag, anotherElement);

尝试更改您的代码,如下所示:-

documentoCODExporterMasEH.insertBefore(newNode,anotherNewNode);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何过滤 XML 文档中的 XML 节点及其子节点?

在VB中从XML文档中删除节点

XML文档中的节点选择

遍历XML文档中的子节点

阅读XML文档并删除节点

通过xml文档中的内部文本查找xml节点

如何使用XQuery将XML文档中的多个节点包装到新节点?

如何使用python将嵌套的子节点添加到xml文档中的父节点?

将现有 XML 文件中的节点附加到新的 XML 文档中

如何将节点从xml文档附加到现有的xml文档

XML:将xml文档附加到另一个文档的节点中

将xml节点/文档/片段作为参数传递给xslt

从文档中选择单个XML节点

如何从此XML文档获取祖先节点?

根据节点在xml文档中的位置查找元素的值

使用Spring Integration无法从文档中获取xml节点数

根据名称从 XML 文档中删除整个节点

使用linq从XML文档中删除“最近30天”节点

在 XML 文档中搜索最高节点级别

如何在C#中更新XML文档的特定子节点值

如何使用Maven将新节点附加到现有XML文档中?

XML 文档 - 检查父节点,然后遍历文档

XML文档中的错误。意外的XML声明。XML声明必须是文档中的第一个节点

以节点 js 中的 xml 节点为目标

从旧文档运行时错误VBA,XML将节点导入到新文档

使用 DOMDocument 将新节点及其子节点添加到 XML 文档

XPATH:将xml节点插入另一个XML文档

如何将xml节点作为第一个子节点插入Java中的另一个xml文档中?

C#获取xml文档中的所有节点,但忽略嵌套节点