1 package com.company; 2 3 import org.w3c.dom.Document; 4 import org.w3c.dom.Element; 5 import org.w3c.dom.Node; 6 import org.w3c.dom.NodeList; 7 import org.xml.sax.SAXException; 8 9 import javax.xml.XMLConstants; 10 import javax.xml.parsers.DocumentBuilder; 11 import javax.xml.parsers.DocumentBuilderFactory; 12 import javax.xml.parsers.ParserConfigurationException; 13 import java.io.File; 14 import java.io.IOException; 15 16 /* 17 * Cte XML a vraci Doc; 18 * */ 19 20 public class ReadXmlToDom { 21 private static DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 22 private static String FILENAME = "/Users/jarda/IdeaProjects/untitled1/src/com/test.xml"; 23 24 public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException { 25 // optional, but recommended 26 // process XML securely, avoid attacks like XML External Entities (XXE) 27 28 if(args.length>0) 29 FILENAME = args[0]; 30 31 dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); 32 33 // parse XML file 34 DocumentBuilder db = dbf.newDocumentBuilder(); 35 36 Document doc = db.parse(new File(FILENAME)); 37 38 // optional, but recommended 39 // http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work 40 doc.getDocumentElement().normalize(); 41 42 System.out.println("Root Element :" + doc.getDocumentElement().getNodeName()); 43 System.out.println("------"); 44 45 Document cti = Cti(); 46 47 Vypis vypis = new Vypis(doc); 48 vypis.Vypis1(); 49 // get <staff> 50 NodeList list = doc.getElementsByTagName("staff"); 51 52 53 } 54 55 private static Document Cti() throws ParserConfigurationException, IOException, SAXException { 56 return Cti(FILENAME); 57 } 58 59 public static Document Cti(String FILENAME) throws ParserConfigurationException, IOException, SAXException { 60 // optional, but recommended 61 // process XML securely, avoid attacks like XML External Entities (XXE) 62 63 dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); 64 65 // parse XML file 66 DocumentBuilder db = dbf.newDocumentBuilder(); 67 68 Document doc1 = db.parse(new File(FILENAME)); 69 70 // optional, but recommended 71 // http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work 72 doc1.getDocumentElement().normalize(); 73 74 System.out.println("Root Element :" + doc1.getDocumentElement().getNodeName()); 75 System.out.println("------"); 76 77 return doc1; 78 } 79 80 81 } 82 83 class Vypis { 84 85 public Document doc; 86 87 public Vypis() { 88 System.out.println("--Vypis0----"); 89 } 90 91 public Vypis(Document doc1) { 92 this.doc=doc1; 93 Vypis2(); 94 } 95 96 public void Vypis2() { 97 98 System.out.println("--Vypis1----"); 99 System.out.println(doc.getDocumentElement().getNodeName()); 100 101 // get <staff> 102 NodeList list = doc.getElementsByTagName("staff"); 103 104 for (int temp = 0; temp < list.getLength(); temp++) { 105 106 Node node = list.item(temp); 107 108 if (node.getNodeType() == Node.ELEMENT_NODE) { 109 110 Element element = (Element) node; 111 112 // get staff's attribute 113 String id = element.getAttribute("id"); 114 115 // get text 116 String firstname = element.getElementsByTagName("firstname").item(0).getTextContent(); 117 String lastname = element.getElementsByTagName("lastname").item(0).getTextContent(); 118 String nickname = element.getElementsByTagName("nickname").item(0).getTextContent(); 119 120 NodeList salaryNodeList = element.getElementsByTagName("salary"); 121 String salary = salaryNodeList.item(0).getTextContent(); 122 123 // get salary's attribute 124 String currency = salaryNodeList.item(0).getAttributes().getNamedItem("currency").getTextContent(); 125 126 System.out.println("Current Element :" + node.getNodeName()); 127 System.out.println("Staff Id : " + id); 128 System.out.println("First Name : " + firstname); 129 System.out.println("Last Name : " + lastname); 130 System.out.println("Nick Name : " + nickname); 131 System.out.printf("Salary [Currency] : %,.2f [%s]%n%n", Float.parseFloat(salary), currency); 132 133 } 134 } 135 136 } 137 138 public void Vypis1 () { 139 System.out.println("--Vypis1----"); 140 Vypis1("rrr"); 141 } 142 143 public void Vypis1 (String str) { 144 System.out.println("--Vypis1----"+str); 145 146 } 147 }