=====Java===== ====DOM==== Ressource : {{lang:java:xml:atelier.xml|}} package org.llgc; import java.io.File; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class XMLDOM { public static void readXML () { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance (); try { DocumentBuilder parser = factory.newDocumentBuilder (); File XFile = new File ("atelier.xml"); Document root = parser.parse (XFile); NodeList childs = root.getElementsByTagName ("customers").item (0).getChildNodes (); for (int i = 0; i < childs.getLength (); i++) { Node node = childs.item (i); if ( (node.getNodeType () == org.w3c.dom.Node.ELEMENT_NODE) && (node.getNodeName ().equals ("customer"))) { Element el = (Element) node; System.out.println (el.getAttribute ("id") + " " + el.getTextContent ()); } } } catch (SAXException | IOException | ParserConfigurationException e) { e.printStackTrace (); return; } } public static void writeXML () { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance (); try { DocumentBuilder parser = factory.newDocumentBuilder (); Document document = parser.newDocument (); Element root = document.createElement ("contacts"); document.appendChild (root); String[][] contacts = { { "Dupont", "Pierre", "192.168.1.100" }, { "Dupond", "Paul", "192.168.1.101" }, { "Ducont", "Jack", "192.168.1.102" }, { "Ducond", "Peter", "192.168.1.103" }, { "Dutont", "Arf", "192.168.1.104" } }; int i = 0; for (String[] contact : contacts) { ++i; Element contactXML = document.createElement ("contact"); contactXML.setAttribute ("id", Integer.toString (i)); Element contactNom = document.createElement ("nom"); contactNom.setTextContent (contact[0]); contactXML.appendChild (contactNom); Element contactPrenom = document.createElement ("prenom"); contactPrenom.setTextContent (contact[1]); contactXML.appendChild (contactPrenom); Element contactIP = document.createElement ("ip"); contactIP.setTextContent (contact[2]); contactXML.appendChild (contactIP); root.appendChild (contactXML); } TransformerFactory transformerFactory = TransformerFactory.newInstance (); Transformer transformer = transformerFactory.newTransformer (); DOMSource source = new DOMSource (document); StreamResult sortie = new StreamResult (new File ("contact.xml")); transformer.setOutputProperty (OutputKeys.VERSION, "1.0"); transformer.setOutputProperty (OutputKeys.ENCODING, "UTF-8"); document.setXmlStandalone (false); // Ne marche pas : transformer.setOutputProperty (OutputKeys.STANDALONE, // "yes"); transformer.setOutputProperty (OutputKeys.INDENT, "yes"); transformer.setOutputProperty ("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.transform (source, sortie); } catch (ParserConfigurationException | TransformerException e) { e.printStackTrace (); return; } } public static void main (String[] args) { readXML (); writeXML (); } } ====SAX==== Ressource : {{lang:java:xml:contact.xml|}} package org.llgc; import java.io.File; import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.helpers.DefaultHandler; public class XMLSAX extends DefaultHandler { private boolean close = false; private int deep = 0; private Deep0 pos0 = Deep0.UNKNOWN; private Deep1 pos1 = Deep1.UNKNOWN; private Deep2 pos2 = Deep2.UNKNOWN; public enum Deep0 { UNKNOWN, CONTACTS }; public enum Deep1 { UNKNOWN, CONTACT }; public enum Deep2 { UNKNOWN, NOM, PRENOM, IP }; @Override public void startDocument () throws SAXException { System.out.println ("Liste des documents"); close = false; deep = 0; } @Override public void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException { close = false; switch (deep) { case 0: { if (qName.equals ("contacts")) { pos0 = Deep0.CONTACTS; } else { fatalError (new SAXParseException ("Fichier invalide. Élément racine incorrect : " + qName + ".", null)); } break; } case 1: { if (qName.equals ("contact")) { pos1 = Deep1.CONTACT; } else { fatalError (new SAXParseException ("Fichier invalide. Élément niveau 1 incorrect : " + qName + ".", null)); } break; } case 2: { if (qName.equals ("nom")) { pos2 = Deep2.NOM; } else if (qName.equals ("prenom")) { pos2 = Deep2.PRENOM; } else if (qName.equals ("ip")) { pos2 = Deep2.IP; } else { fatalError (new SAXParseException ("Fichier invalide. Élément niveau 2 incorrect : " + qName + ".", null)); } break; } default: { fatalError (new SAXParseException ("Fichier invalide. Niveau d'arborescence (3) trop grand.", null)); break; } } if ( (pos0 == Deep0.CONTACTS) && (pos1 == Deep1.CONTACT) && (pos2 == Deep2.UNKNOWN)) { System.out.println (qName + " - " + attributes.getValue ("id") + " : "); } deep++; } @Override public void characters (char[] ch, int start, int length) throws SAXException { if (close) { return; } if ( (ch[start] != '\n') && (pos0 == Deep0.CONTACTS) && (pos1 == Deep1.CONTACT)) { switch (pos2) { case NOM: System.out.print ("Nom : "); break; case PRENOM: System.out.print ("Prénom : "); break; case IP: System.out.print ("IP : "); break; default: fatalError (new SAXParseException ("Fichier invalide. Type inconnu.", null)); // Erreur // impossible // normalement. break; } System.out.println (new String (ch, start, length)); } } @Override public void endElement (String uri, String localName, String qName) throws SAXException { deep--; switch (deep) { case 0: pos0 = Deep0.UNKNOWN; break; case 1: pos1 = Deep1.UNKNOWN; break; case 2: pos2 = Deep2.UNKNOWN; break; default: // Erreur impossible normalement. fatalError (new SAXParseException ("Fichier invalide. Type inconnu.", null)); break; } close = true; } @Override public void endDocument () throws SAXException { close = true; } @Override public void fatalError (SAXParseException e) throws SAXException { throw new SAXException (e); } public static void main (String[] args) { SAXParserFactory factory = SAXParserFactory.newInstance (); try { SAXParser parser = factory.newSAXParser (); parser.parse (new File ("contact.xml"), new XMLSAX ()); } catch (ParserConfigurationException | SAXException | IOException e) { e.printStackTrace (); return; } } }