Advertisement
Guest User

Model

a guest
May 19th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.95 KB | None | 0 0
  1. import org.w3c.dom.Document;
  2. import org.w3c.dom.Element;
  3. import org.w3c.dom.Node;
  4. import org.w3c.dom.NodeList;
  5. import org.xml.sax.Attributes;
  6. import org.xml.sax.SAXException;
  7. import org.xml.sax.SAXParseException;
  8. import org.xml.sax.helpers.DefaultHandler;
  9.  
  10. import javax.xml.parsers.DocumentBuilder;
  11. import javax.xml.parsers.DocumentBuilderFactory;
  12. import javax.xml.parsers.SAXParser;
  13. import javax.xml.parsers.SAXParserFactory;
  14. import javax.xml.transform.OutputKeys;
  15. import javax.xml.transform.Transformer;
  16. import javax.xml.transform.TransformerFactory;
  17. import javax.xml.transform.dom.DOMSource;
  18. import javax.xml.transform.stream.StreamResult;
  19. import java.io.File;
  20. import java.text.SimpleDateFormat;
  21. import java.time.LocalDate;
  22. import java.time.format.DateTimeFormatter;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25.  
  26. public class PatientsModel {
  27.  
  28.     private String patientSurname;
  29.     private String town;
  30.  
  31.     private String filePath = "AllPatient.xml";
  32.     private List<PatientsModel> modelArray = new ArrayList<PatientsModel>();
  33.     private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
  34.  
  35.     public String getPatientSurname(){
  36.         return this.patientSurname;
  37.     }
  38.     public void setPatientSurname(String newPatientSurname){
  39.         this.patientSurname = newPatientSurname;
  40.     }
  41.  
  42.     public String getTown(){
  43.         return this.town;
  44.     }
  45.  
  46.     public void setTown(String newTown){
  47.         this.town = newTown;
  48.     }
  49.  
  50.     public List<PatientsModel> getModelArray(){
  51.         return modelArray;
  52.     }
  53.  
  54.  
  55.     private void addNewElArr(){
  56.  
  57.         PatientsModel newModel = new PatientsModel();
  58.  
  59.         newModel.setPatientSurname(patientSurname);
  60.         newModel.setTown(town);
  61.  
  62.         modelArray.add(newModel);
  63.     }
  64.  
  65.     public void readXMLbySAX() { //String strPatientFio,String strAdress) {
  66.         try {
  67.             modelArray.clear();
  68.             SAXParserFactory factory = SAXParserFactory.newInstance();
  69.             SAXParser saxParser = factory.newSAXParser();
  70.  
  71.             DefaultHandler handler = new DefaultHandler() {
  72.  
  73.                 boolean bPatientSurname = false;
  74.                 boolean bTown = false;
  75.  
  76.  
  77.                 public void startElement(String uri, String localName, String qPatientTag,
  78.                                          Attributes attributes) throws SAXException {
  79.  
  80.                     if (qPatientTag.equalsIgnoreCase("patientSurname")) {
  81.                         bPatientSurname = true;
  82.                     }
  83.  
  84.                     if (qPatientTag.equalsIgnoreCase("town")) {
  85.                         bTown = true;
  86.                     }
  87.                 }
  88.  
  89.                 public void endElement(String uri, String localName,
  90.                                        String qPatientTag) throws SAXException {
  91.  
  92.                     if (qPatientTag.equalsIgnoreCase("patient")) {
  93.                         System.out.println("end of tag :");// + maxPatient);
  94.                     }
  95.                 }
  96.  
  97.                 public void characters(char ch[], int start, int length) throws SAXException {
  98.  
  99.                     if(bPatientSurname){
  100.                         System.out.println("PatientSurname : " + new String(ch, start, length));
  101.                         bPatientSurname = false;
  102.                         setPatientSurname(new String(ch, start, length));
  103.                         System.out.println("Текущая фамилия :" + patientSurname);
  104.                     }
  105.  
  106.                     if (bTown) {
  107.                         System.out.println("town : " + new String(ch, start, length));
  108.                         bTown = false;
  109.                         setTown(new String(ch, start, length));
  110.                         System.out.println("Текущий город :" + town);
  111.                         addNewElArr();
  112.                     }
  113.                 }
  114.             };
  115.  
  116.             saxParser.parse(filePath, handler);
  117.  
  118.         } catch (Exception e) {
  119.             e.printStackTrace();
  120.         }
  121.     }
  122.  
  123.     public void writeXML(String strPatientSurname,String strTown) {
  124.         //DOM
  125.         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  126.         DocumentBuilder builder;
  127.         try {
  128.             builder = factory.newDocumentBuilder();
  129.             Document document;
  130.             File file = new File(filePath);
  131.             if(file.length() == 0){
  132.                 document = factory.newDocumentBuilder().newDocument();
  133.                 Element root = document.createElement("root");
  134.                 document.appendChild(root);
  135.             }
  136.             else {
  137.                 document = builder.parse(filePath);
  138.             }
  139.             System.out.println("ХОБА1");
  140.             document.getDocumentElement().normalize();
  141.             SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/YYYY");
  142.             //String birthStr = (dateOfBirth).format(formatter);
  143.             //String receiptStr = localDateOfReceipt.format(formatter);
  144.  
  145.             //addNewTag(document, strPatientSurname,strPatientName,strPatientPatronym, strTown,strStreet,strHouse,
  146.             //        strFlat, strDoctorSurname,strDoctorName,strDoctorPatronym, strConclusion, birthStr, receiptStr);
  147.             addNewTag(document,strPatientSurname,strTown);
  148.             document.getDocumentElement().normalize();
  149.             TransformerFactory transformerFactory = TransformerFactory.newInstance();
  150.             Transformer transformer = transformerFactory.newTransformer();
  151.             DOMSource source = new DOMSource(document);
  152.             StreamResult result = new StreamResult(new File(filePath));
  153.             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  154.             transformer.transform(source, result);
  155.  
  156.  
  157.         }catch (SAXParseException exception){
  158.             System.out.println("ERRROR");
  159.         }
  160.         catch (Exception exc) {
  161.             exc.printStackTrace();
  162.         }
  163.     }
  164.  
  165.     //private void addNewTag(Document doc, String strPatientSurname,String strPatientName,String strPatientPatronym,
  166.     //                       String strTown,String strStreet,String strHouse,String strFlat,
  167.     //                       String strDoctorSurname,String strDoctorName,String strDoctorPatronym,
  168.     //                       String newConcl, String Birthday, String Receiptday) {
  169.  
  170.     private void addNewTag(Document doc,String strPatientSurname,String strTown){
  171.         NodeList patients = doc.getElementsByTagName("root");
  172.         Element lang = null;
  173.  
  174.         int i = patients.getLength();
  175.         lang = (Element) patients.item(i - 1);
  176.  
  177.         Element patient = doc.createElement("patient");
  178.  
  179.         Element patientSurname = doc.createElement("patientSurname");
  180.         //Element patientName = doc.createElement("patientName");
  181.         //Element patientPatronym = doc.createElement("patientPatronym");
  182.  
  183.         Element town = doc.createElement("town");
  184.        // Element street = doc.createElement("street");
  185.         /*Element house = doc.createElement("house");
  186.         Element flat = doc.createElement("flat");
  187.  
  188.         Element doctorSurname = doc.createElement("doctorSurname");
  189.         Element doctorName = doc.createElement("doctorName");
  190.         Element doctorPatronym = doc.createElement("doctorPatronym");
  191.  
  192.         Element conclusion = doc.createElement("conclusion");
  193.         Element dateOfBirth = doc.createElement("dateOfBirth");
  194.         Element dateOfReceipt = doc.createElement("dateOfReceipt");
  195.         */
  196.  
  197.         patientSurname.appendChild(doc.createTextNode(strPatientSurname));
  198.        // patientName.appendChild(doc.createTextNode(strPatientName));
  199.         //patientPatronym.appendChild(doc.createTextNode(strPatientPatronym));
  200.         town.appendChild(doc.createTextNode(strTown));
  201.         /*street.appendChild(doc.createTextNode(strStreet));
  202.         house.appendChild(doc.createTextNode(strHouse));
  203.         flat.appendChild(doc.createTextNode(strFlat));
  204.  
  205.         doctorSurname.appendChild(doc.createTextNode(strDoctorSurname));
  206.         doctorName.appendChild(doc.createTextNode(strDoctorName));
  207.         doctorPatronym.appendChild(doc.createTextNode(strDoctorPatronym));
  208.  
  209.         conclusion.appendChild(doc.createTextNode(newConcl));
  210.         dateOfBirth.appendChild(doc.createTextNode(Birthday));//String to LocalDate
  211.         dateOfReceipt.appendChild(doc.createTextNode(Receiptday));
  212.         */
  213.  
  214.         patient.appendChild((patientSurname));
  215.         //patient.appendChild((patientName));
  216.         //patient.appendChild((patientPatronym));
  217.  
  218.         patient.appendChild((town));
  219.         /*patient.appendChild((street));
  220.         patient.appendChild((house));
  221.         patient.appendChild((flat));
  222.  
  223.         patient.appendChild(doctorSurname);
  224.         patient.appendChild(doctorName);
  225.         patient.appendChild(doctorPatronym);
  226.         patient.appendChild(conclusion);
  227.         patient.appendChild(dateOfBirth);
  228.         patient.appendChild(dateOfReceipt);
  229.         */
  230.  
  231.         lang.appendChild(patient);
  232.     }
  233.  
  234.     public void delTag(int willDel) {
  235.         try {
  236.             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  237.             DocumentBuilder builder;
  238.             builder = factory.newDocumentBuilder();
  239.             Document document = builder.parse(filePath);
  240.             Element element = (Element) document.getElementsByTagName("patient").item(willDel);
  241.  
  242.             Node parent = element.getParentNode();
  243.             parent.removeChild(element);
  244.             parent.normalize();
  245.  
  246.             TransformerFactory transformerFactory = TransformerFactory.newInstance();
  247.             Transformer transformer = transformerFactory.newTransformer();
  248.             DOMSource source = new DOMSource(document);
  249.             StreamResult result = new StreamResult(new File(filePath));
  250.             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  251.             transformer.transform(source, result);
  252.  
  253.  
  254.         } catch (Exception exc) {
  255.             exc.printStackTrace();
  256.         }
  257.     }
  258.  
  259.  
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement