Search code examples
javaxmlsaxjdomjdom-2

Remove entire xml node


I try to remove a node from my xml with jdom2 but i don't find the answer

my xml is like this :

<?xml version="1.0" encoding="UTF-8"?>
<banque>
  <compte idCompte="5646">
    <numCompte>5646</numCompte>
    <nom>Ludo</nom>
    <solde>850</solde>
  </compte>
  <compte idCompte="4546">
    <numCompte>4546</numCompte>
    <nom>Antoine</nom>
    <solde>9999.4</solde>
  </compte>

i think i need to target the attribute idCompte and remove it but i don't know how to do this.

if you have a (simple) solution i'm very pleased to see :)


Solution

  • What you are trying to achieve is complex(multi-step process) in nature,
    I can share with you the steps(algorithm) that you need to follow:
    
    Step:1
    You need to genearte the schema first:
    Generate the schema from this online tool: 
    http://www.xmlforasp.net/CodeBank/System_Xml_Schema/BuildSchema/BuildXMLSchema.aspx
    
    Step:2
    Using these schema generate the Java classes:
    using api tools like XMLBeans or JAXB
    a) scomp -out employeeschema.jar employeeschema.xsd (Ref: http://xmlbeans.apache.org/)
    b) xjc -d out customer.xsd (Ref: http://blog.bdoughan.com/2011/10/jaxb-xjc-imported-schemas-and-xml.html)
    
    Step:3 
    Then follow below tutorial for your reference for in-depth CRUD operation that you want to perform on your XML:
    Jaxb: http://www.mkyong.com/java/jaxb-hello-world-example/
    Xstream: http://x-stream.github.io/tutorial.html
    XMLBeans: http://xmlbeans.apache.org/documentation/tutorial_getstarted.html