Advertisement
Mlaszlo95

Untitled

May 24th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.96 KB | None | 0 0
  1. package evo.teszt;
  2.  
  3. import java.awt.List;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6.  
  7. import javax.swing.text.Document;
  8. import javax.xml.parsers.DocumentBuilder;
  9. import javax.xml.parsers.DocumentBuilderFactory;
  10. import javax.xml.parsers.ParserConfigurationException;
  11.  
  12. import org.w3c.dom.Document;
  13. import org.w3c.dom.Element;
  14. import org.w3c.dom.Node;
  15. import org.w3c.dom.NodeList;
  16. import org.xml.sax.SAXException;
  17.  
  18. import evo.classes.*;
  19.  
  20. public class Reader_test {
  21.     //megnyitja a documentumot, ha nem tudja akkor dob Exception-nokat
  22.     public static org.w3c.dom.Document tryOpenTheFile() throws ParserConfigurationException, SAXException, IOException {
  23.         DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
  24.         DocumentBuilder dBuilder = null;
  25.         org.w3c.dom.Document doc = null;        //valamiért nem tudja elérni az ut vonalat ha nincs benne a "org.w3c.dom.Document". Feljebb mutatja, hogy nem használom az importot, passz nem tuduom miért.
  26.         try {
  27.             dBuilder = dbFactory.newDocumentBuilder();
  28.             doc = dBuilder.parse(Products.getFileLocation());
  29.             doc.getDocumentElement().normalize();
  30.         } catch (Exception e) {
  31.             e.printStackTrace();
  32.         }
  33.         return doc;
  34.     }
  35.     //Kiir egy elemetet, amit meghatározunk
  36.     public static void writeDownOnePartOfTheList() {
  37.         org.w3c.dom.Document doc = null;
  38.         try {
  39.             doc = tryOpenTheFile();
  40.         } catch (ParserConfigurationException | SAXException | IOException e) {
  41.             // TODO Auto-generated catch block
  42.             e.printStackTrace();
  43.         }
  44.         Products writer = new ComponentsCPU();
  45.        
  46.         NodeList nList = (NodeList)doc.getElementsByTagName("PC_component").item(0);    //mind kérésre meg lehet változtatni
  47.  
  48.         for (int i = 0; i < nList.getLength(); i++) {
  49.             Node nNode = nList.item(i);
  50.             if (nNode.getNodeType() == Node.ELEMENT_NODE) {
  51.                 Element eElement = (Element) nNode;
  52.                 //Most csak a CPU irja ki
  53.                 writer.readTheFileAndWriteDown(eElement);
  54.             }
  55.         }
  56.     }
  57.     //Kiirja sorbarendezés nélkül az összes elemet
  58.     public static void whiteDownWholeStuff() {
  59.         org.w3c.dom.Document doc = null;
  60.         try {
  61.             doc = tryOpenTheFile();
  62.         } catch (ParserConfigurationException | SAXException | IOException e) {
  63.             // TODO Auto-generated catch block
  64.             e.printStackTrace();
  65.         }
  66.         Products writerCPU = new ComponentsCPU();
  67.         Products writerGPU = new ComponentsGPU();
  68.         Products writerOther = new Other();
  69.        
  70.         NodeList nList = doc.getElementsByTagName("item");  //mind kérésre meg lehet változtatni
  71.  
  72.         for (int i = 0; i < nList.getLength(); i++) {
  73.             Node nNode = nList.item(i);
  74.             if (nNode.getNodeType() == Node.ELEMENT_NODE) {
  75.                 Element eElement = (Element) nNode;
  76.                 //Most csak a CPU irja ki
  77.                 writerCPU.readTheFileAndWriteDown(eElement);
  78.                 writerGPU.readTheFileAndWriteDown(eElement);
  79.                 writerOther.readTheFileAndWriteDown(eElement);
  80.             }
  81.         }
  82.     }
  83.     public static void main(String [] angs) {
  84.         //writeDownOnePartOfTheList();
  85.         System.out.print("--------\n");
  86.         whiteDownWholeStuff();
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement