Last active
May 24, 2018 20:52
-
-
Save fmbenhassine/f84fdf029cbcbae5216c to your computer and use it in GitHub Desktop.
XML Sax sample #lab
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<company name="... Applied Physics Laboratory" | |
shortName="JHU/APL" | |
mission="Enhancing national security ..."> | |
<head name="Richard Roca" phone="410-778-1234"/> | |
<department name="Air and Missile Defense" | |
mission="Enhance the operational ..."> | |
<group name="A1E" numStaff="20"/> | |
<group name="A1F" numStaff="15"/> | |
<group name="A1G" numStaff="25"/> | |
</department> | |
<department name="Research and Technology..." | |
mission="Assure that ..."> | |
<group name="RSI" numStaff="15"/> | |
<group name="RSS" numStaff="10"/> | |
</department> | |
</company> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.benas.labs.javase.xml; | |
import org.w3c.dom.Document; | |
import org.w3c.dom.Element; | |
import org.w3c.dom.NodeList; | |
import org.xml.sax.SAXException; | |
import javax.xml.parsers.DocumentBuilder; | |
import javax.xml.parsers.DocumentBuilderFactory; | |
import javax.xml.parsers.ParserConfigurationException; | |
import java.io.File; | |
import java.io.IOException; | |
public class XmlMain { | |
public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException { | |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | |
DocumentBuilder builder = factory.newDocumentBuilder(); | |
String sourceFile = args[0]; | |
Document document = builder.parse(new File(sourceFile)); | |
Element root = document.getDocumentElement(); | |
System.out.println(root.getTagName()); | |
System.out.printf(" name: %s%n", root.getAttribute("name")); | |
System.out.printf(" short name: %s%n", root.getAttribute("shortName")); | |
System.out.printf(" mission: %s%n", root.getAttribute("mission")); | |
NodeList departments = root.getElementsByTagName("department"); | |
for (int i = 0; i < departments.getLength(); i++) { | |
Element department = (Element) departments.item(i); | |
System.out.println(department.getTagName()); | |
System.out.printf(" name: %s%n", department.getAttribute("name")); | |
System.out.printf(" mission: %s%n", department.getAttribute("mission")); | |
System.out.printf(" staff: %s%n", countStaff(department)); | |
} | |
} | |
public static int countStaff(Element department) { | |
int departmentStaff = 0; | |
NodeList groups = department.getElementsByTagName("group"); | |
for (int i = 0; i < groups.getLength(); i++) { | |
Element group = (Element) groups.item(i); | |
int groupStaff = Integer.parseInt(group.getAttribute("numStaff")); | |
departmentStaff = departmentStaff + groupStaff; | |
} | |
return (departmentStaff); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment