1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
package com.cap.xml;
import java.io.*;
import org.w3c.dom.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.*;
import javax.xml.parsers.*;
import java.io.*;
import org.apache.crimson.tree.XmlDocument;
import org.apache.crimson.tree.XmlWriteContext;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import java.util.List;
import java.util.Iterator;
public class TestDom2 {
public static void toModify() {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try
{
builder = factory.newDocumentBuilder();
FileInputStream fis = new FileInputStream(new File("C:\\TESTZIPZIP\\facture.xml"));
Document document = builder.parse(fis);
//Document document = builder.parse("C:\\TESTZIPZIP\\facture.xml");
NodeList elements = document.getElementsByTagName("client");
Element elementCl;
for ( int i=0; i<elements.getLength();i++)
{
elementCl = (Element)elements.item(i);
System.out.println("Client : "+ elementCl.getAttribute("nom"));
if ((elementCl.getAttribute("nom")).equalsIgnoreCase("achille"))
{
Node node = elementCl.getParentNode().removeChild(elementCl);
// sur sortie standard
//((XmlDocument)document).write(System.out);
((XmlDocument)document).write( new FileOutputStream("C:\\TESTZIPZIP\\factureNew2.xml"));
break;
}
else
{
System.out.println("pas d'achille");
}
System.out.println("i =" + i);
}
}
catch (ParserConfigurationException e)
{
e.printStackTrace();
}
catch (SAXException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
//System.exit(0);
}
} |
Partager