How do set the XML Parser?

JumboDumbo 11 Reputation points
2021-12-05T18:51:45.153+00:00

package eu.;

import eu.DataStorage.Equipment;
import eu.DataStorage.Route;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.Scanner;

public class Main {

public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException, ElementNotExists {
    TrainingXMLParser parser = new TrainingXMLParser("src/eu//data.xml");
    parser.createAll();
    parser.printAllWorkouts();
    Equipment.printRoutes();

    System.out.print("\nSearch: ");
    Scanner sc = new Scanner(System.in);
    System.out.println(Route.searchRoute(sc.nextLine()));
}

}

import eu.DataStorage.Category;
import eu.DataStorage.Equipment;
import eu.DataStorage.Route;
import eu.DataStorage.Workout;
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;
import java.time.LocalDateTime;

public class TrainingXMLParser {
private final Document doc;

public TrainingXMLParser(String filename) throws ParserConfigurationException, IOException, SAXException {
    //Read the XML-File
    File inputFile = new File(filename);
    DocumentBuilderFactory dbFactory =
            DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    this.doc = dBuilder.parse(inputFile);
}

public void createAll() throws ElementNotExists {//Generates all instances of the XML-File
    createCategories();
    createEquipments();
    createRoutes();
    createWorkouts();
}

public void createCategories() {
    NodeList categories = doc.getElementsByTagName("category");
    for (int i = 0; i < categories.getLength(); i++) {//Go through all elements
        Element currItem = (Element) categories.item(i);
        new Category(Integer.valueOf(currItem.getAttributeNode("id").getValue()), currItem.getElementsByTagName("name").item(0).getTextContent());
    }
}

public void createEquipments() throws ElementNotExists {
    NodeList equipments = doc.getElementsByTagName("equipment");
    for (int i = 0; i < equipments.getLength(); i++) {//Go through all elements
        Element currItem = (Element) equipments.item(i);
        //Find Category and if not found an Exception will be thrown
        Category category = findCategoryByID(Integer.valueOf(currItem.getElementsByTagName("categoryID").item(0).getTextContent()));
        if (category == null) {
            throw new ElementNotExists("Category with the ID " + currItem.getElementsByTagName("categoryID").item(0).getTextContent() + " doesn't exist!");
        } else {
            new Equipment(Integer.valueOf(currItem.getAttributeNode("id").getValue()), category, currItem.getElementsByTagName("name").item(0).getTextContent());
        }
    }
}

public void createRoutes() throws ElementNotExists {
    NodeList routes = doc.getElementsByTagName("route");
    for (int i = 0; i < routes.getLength(); i++) {//Go through all elements
        Element currItem = (Element) routes.item(i);
        //Find Equipment and if not found an Exception will be thrown
        Equipment equipment = findEquipmentByID(Integer.valueOf(currItem.getElementsByTagName("equipmentID").item(0).getTextContent()));
        if (equipment == null) {
            throw new ElementNotExists("Equipment with the ID " + currItem.getElementsByTagName("categoryID").item(0).getTextContent() + " doesn't exist!");
        } else {
            Route r = new Route(Integer.valueOf(currItem.getAttributeNode("id").getValue()), currItem.getElementsByTagName("name").item(0).getTextContent(), equipment);
            NodeList waypoints = currItem.getElementsByTagName("waypoint");
            for (int j = 0; j < waypoints.getLength(); j++) {//Go through all waypoints
                Element currWaypoint = (Element) waypoints.item(i);
                r.addWaypoint(currWaypoint.getElementsByTagName("postitionX").item(0).getTextContent(), currWaypoint.getElementsByTagName("postitionY").item(0).getTextContent());
            }
        }
    }
}

public void createWorkouts() throws ElementNotExists {
    NodeList workouts = doc.getElementsByTagName("workout");
    for (int i = 0; i < workouts.getLength(); i++) {//Go through all elements
        Element currItem = (Element) workouts.item(i);
        //Find Route and if not found a Exception will be thrown
        Route route = findRouteByID(Integer.valueOf(currItem.getElementsByTagName("routeID").item(0).getTextContent()));
        //Find Equipment and if not found a Exception will be thrown
        Equipment equipment = findEquipmentByID(Integer.valueOf(currItem.getElementsByTagName("equipmentID").item(0).getTextContent()));
        if (route == null) {
            throw new ElementNotExists("Route with the ID " + currItem.getElementsByTagName("routeID").item(0).getTextContent() + " doesn't exist!");
        } else if (equipment == null) {
            throw new ElementNotExists("Equipment with the ID " + currItem.getElementsByTagName("categoryID").item(0).getTextContent() + " doesn't exist!");
        } else {
            Workout r = new Workout(Integer.valueOf(currItem.getAttributeNode("id").getValue()), LocalDateTime.parse(currItem.getElementsByTagName("start").item(0).getTextContent()), LocalDateTime.parse(currItem.getElementsByTagName("end").item(0).getTextContent()), equipment, route);
            NodeList waypoints = currItem.getElementsByTagName("waypoint");
            for (int j = 0; j < waypoints.getLength(); j++) {//Go through all waypoints
                Element currWaypoint = (Element) waypoints.item(i);
                r.addWaypoint(currWaypoint.getElementsByTagName("postitionX").item(0).getTextContent(), currWaypoint.getElementsByTagName("postitionY").item(0).getTextContent(), Integer.valueOf(currWaypoint.getElementsByTagName("heartRate").item(0).getTextContent()), Double.valueOf(currWaypoint.getElementsByTagName("speed").item(0).getTextContent()));
            }
        }
    }
}


public void printAllCategories() {
    System.out.println(Category.categories);
}

public void printAllEquipments() {
    System.out.println(Equipment.equipments);
}

public void printAllRoutes() {
    System.out.println(Route.routes);
}

public void printAllWorkouts() {
    System.out.println(Workout.workouts);
}

private Route findRouteByID(int id) {
    for (Route r : Route.routes) {
        if (r.getID() == id) {
            return r;
        }
    }
    return null;
}

private Category findCategoryByID(int id) {
    for (Category c : Category.categories) {
        if (c.getID() == id) {
            return c;
        }
    }
    return null;
}

private Equipment findEquipmentByID(int id) {
    for (Equipment c : Equipment.equipments) {
        if (c.getID() == id) {
            return c;
        }
    }
    return null;
}

}

public class ElementNotExists extends Exception {
public ElementNotExists(String message) {
super(message);
}
}

Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,365 questions
0 comments No comments
{count} votes