Beispiel #1
0
  public static String appointmentListToXML(ArrayList<Appointment> appointmentList) {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder;
    try {
      docBuilder = docFactory.newDocumentBuilder();
      Document doc = docBuilder.newDocument();
      Element rootElement = doc.createElement("appointments");
      doc.appendChild(rootElement);

      int j = 0;
      for (Appointment a : appointmentList) {
        Element appointment = doc.createElement("appointment");
        rootElement.appendChild(appointment);

        Element date = doc.createElement("date");
        appointment.appendChild(date);

        Element month = doc.createElement("month");
        date.appendChild(month);
        month.appendChild(doc.createTextNode(a.getDate().getMonth() + ""));

        Element year = doc.createElement("year");
        date.appendChild(year);
        year.appendChild(doc.createTextNode(a.getDate().getYear() + ""));

        Element dayInMonth = doc.createElement("dayInMonth");
        date.appendChild(dayInMonth);
        dayInMonth.appendChild(doc.createTextNode(a.getDate().getDate() + ""));

        Element startTime = doc.createElement("starttime");
        appointment.appendChild(startTime);
        startTime.appendChild(doc.createTextNode(a.getStart().toString()));

        Element endTime = doc.createElement("endtime");
        appointment.appendChild(endTime);
        endTime.appendChild(doc.createTextNode(a.getEnd().toString()));

        Element subject = doc.createElement("subject");
        appointment.appendChild(subject);
        subject.appendChild(doc.createTextNode(a.getSubject()));

        Element id = doc.createElement("id");
        appointment.appendChild(id);
        id.appendChild(doc.createTextNode(a.getId() + ""));
        if (a.getLocation() != null) {
          Element location = doc.createElement("location");
          appointment.appendChild(location);
          location.appendChild(doc.createTextNode(a.getLocation()));
        }
        if (a.getRoomNumber() != 0) {
          Element roomnr = doc.createElement("roomnr");
          appointment.appendChild(roomnr);
          roomnr.appendChild(doc.createTextNode(a.getRoomNumber() + ""));
        }

        for (int i = 0; i < a.getParticipants().size(); i++) {
          Participant p = a.getParticipants().get(i);
          Element participant = doc.createElement("participant" + j);
          appointment.appendChild(participant);
          Element username = doc.createElement("username");
          participant.appendChild(username);
          username.appendChild(doc.createTextNode(p.getEmployee().getUsername()));
          Element name = doc.createElement("name");
          participant.appendChild(name);
          name.appendChild(doc.createTextNode(p.getEmployee().getName()));
          Element state = doc.createElement("state");
          participant.appendChild(state);
          state.appendChild(doc.createTextNode(a.stateToString(p.getState())));
        }

        Element leader = doc.createElement("leader");
        appointment.appendChild(leader);
        Element username = doc.createElement("lusername");
        leader.appendChild(username);
        username.appendChild(doc.createTextNode(a.getLeader().getUsername()));
        Element name = doc.createElement("lname");
        leader.appendChild(name);
        name.appendChild(doc.createTextNode(a.getLeader().getName()));

        Element description = doc.createElement("description");
        appointment.appendChild(description);
        description.appendChild(doc.createTextNode(a.getDescription()));
        j++;
      }

      DOMSource source = new DOMSource(doc);
      TransformerFactory transformerFactory = TransformerFactory.newInstance();
      Transformer optimusPrime = transformerFactory.newTransformer();
      StringWriter stringWriter = new StringWriter();
      Result result = new StreamResult(stringWriter);
      optimusPrime.transform(source, result);

      return stringWriter.getBuffer().toString();
    } catch (ParserConfigurationException e) {
      e.printStackTrace();
    } catch (TransformerConfigurationException e) {
      e.printStackTrace();
    } catch (TransformerException e) {
      e.printStackTrace();
    }
    return null;
  }
Beispiel #2
0
  // Lager en xml fil med data fra appointment
  public String toXML() throws ParserConfigurationException, TransformerException {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

    Document doc = docBuilder.newDocument();
    Element rootElement = doc.createElement("appointment");
    doc.appendChild(rootElement);

    Element date = doc.createElement("date");
    rootElement.appendChild(date);

    Element month = doc.createElement("month");
    date.appendChild(month);
    month.appendChild(doc.createTextNode(getDate().getMonth() + ""));

    Element year = doc.createElement("year");
    date.appendChild(year);
    year.appendChild(doc.createTextNode(getDate().getYear() + ""));

    Element dayInMonth = doc.createElement("dayInMonth");
    date.appendChild(dayInMonth);
    dayInMonth.appendChild(doc.createTextNode(getDate().getDate() + ""));

    Element startTime = doc.createElement("starttime");
    rootElement.appendChild(startTime);
    startTime.appendChild(doc.createTextNode(getStart().toString()));

    Element endTime = doc.createElement("endtime");
    rootElement.appendChild(endTime);
    endTime.appendChild(doc.createTextNode(getEnd().toString()));

    Element subject = doc.createElement("subject");
    rootElement.appendChild(subject);
    subject.appendChild(doc.createTextNode(getSubject()));

    Element id = doc.createElement("id");
    rootElement.appendChild(id);
    id.appendChild(doc.createTextNode(getId() + ""));

    if (getLocation() != null) {
      Element location = doc.createElement("location");
      rootElement.appendChild(location);
      location.appendChild(doc.createTextNode(getLocation()));
    }

    if (getRoomNumber() != 0) {
      Element roomnr = doc.createElement("roomnr");
      rootElement.appendChild(roomnr);
      roomnr.appendChild(doc.createTextNode(getRoomNumber() + ""));
    }

    Element participants = doc.createElement("participants");
    rootElement.appendChild(participants);
    for (Participant p : this.participants) {
      Element participant = doc.createElement("participant");
      participants.appendChild(participant);
      Element username = doc.createElement("username");
      participant.appendChild(username);
      username.appendChild(doc.createTextNode(p.getEmployee().getUsername()));
      Element name = doc.createElement("name");
      participant.appendChild(name);
      name.appendChild(doc.createTextNode(p.getEmployee().getName()));
      Element state = doc.createElement("state");
      participant.appendChild(state);
      state.appendChild(doc.createTextNode(stateToString(p.getState())));
    }

    Element leader = doc.createElement("leader");
    rootElement.appendChild(leader);
    Element username = doc.createElement("lusername");
    leader.appendChild(username);
    username.appendChild(doc.createTextNode(getLeader().getUsername()));
    Element name = doc.createElement("lname");
    leader.appendChild(name);
    name.appendChild(doc.createTextNode(getLeader().getName()));

    Element description = doc.createElement("description");
    rootElement.appendChild(description);
    description.appendChild(doc.createTextNode(getDescription()));

    TransformerFactory transformerFactory2 = TransformerFactory.newInstance();
    Transformer transformer2 = transformerFactory2.newTransformer();
    // write the content into xml file for testing
    DOMSource source = new DOMSource(doc);
    //		StreamResult toFile = new StreamResult(new File("file.xml"));
    //		transformer2.transform(source, toFile);

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    StringWriter stringWriter = new StringWriter();
    Result result = new StreamResult(stringWriter);
    transformer.transform(source, result);
    return stringWriter.getBuffer().toString();
  }