2016-12-30 8 views
-2

XMLノードを独立したXMLとして処理し、ノードドキュメントに新しいタグを追加し、元のXMLドキュメントを新しいノード情報で更新する必要があります。助け、アドバイス、チュートリアルは大歓迎です。XMLドキュメントを独立したXMLドキュメントとして処理し、元の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> 

今、私は新しいXMLのDOMにノード "FirmaDonante" を抽出必要があります。その後

<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要素が追加された、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> 

最後に、私は新しいタグで、ノードとして、元の文書内の同じ位置にあるノードの文書を追加必要があります。

<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オブジェクトに新しい要素またはノードを追加できることをテストすることです。

ありがとうございます!!!!!

答えて

1

私は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); 
関連する問題