import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.w3c.dom.Document; import javax.xml.parsers.ParserConfigurationException; import org.xml.sax.SAXException; import java.io.IOException; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.Element; class Program03 { private static Document buildDoc() { Document doc = null; try { doc = tryBuildDoc(); }catch(ParserConfigurationException ex) { System.err.println("Hiba a dokumentum létrehozása során!"); }catch(SAXException ex) { System.err.println("Hiba értelmezés során!"); }catch(IOException ex) { System.err.println("Hiba a fájl elérése során!"); } return doc; } private static Document tryBuildDoc() throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse("dolgozok.xml"); //opcionális, de ajánlott: doc.getDocumentElement().normalize(); return doc; } private static void writeXml(Document doc) { String root = doc.getDocumentElement().getNodeName(); NodeList nodeList = doc.getElementsByTagName("dolgozo"); for(int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); System.out.println("Aktuális elem: " + node.getNodeName()); if( node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; String az = element.getAttribute("id"); String nev = element.getElementsByTagName("nev").item(0).getTextContent(); String telepules = element.getElementsByTagName("telepules").item(0).getTextContent(); String fizetes = element.getElementsByTagName("fizetes").item(0).getTextContent(); System.out.printf("%s:%s:%s:%s\n", az, nev, telepules, fizetes); } } } public static void main(String[] args) { Document doc = buildDoc(); writeXml(doc); } }