Oberflächen
  Vorlesungen     Download     DBS     Sprachen     File Formats     Impressum     Datenschutz  
C#- GUI-Programme mit Quellcode
Java-Programme mit Quellcode
C#
WPF
HTML
Java FX
Java (Swing)
Winforms
Visual Studio (MFC)
ArcView
QGis
ArcView .net
IDW
Allgemein:
Startseite
Vorlesungen
Labore
Sinnvolle Programme
Lineare Regression
GUI-Entwurfsarbeit
Single-Format
Design Pattern-Termine
Observer1
Bsp2
Json-Array
Json-Dialogelemente
Webtechnologien

ReadXML

ReadXML


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;  // Exception

// Opendialog
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.*;
//import java.util.Hashtable;  // Filter
//import java.util.Enumeration; // Filter

// InternalFrame
import javax.swing.event.*;
import javax.xml.XMLConstants;
import javax.xml.parsers.*;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.*;

import org.w3c.dom.*;
import org.xml.sax.SAXException;



public class ReadXML extends JFrame  {
    // Menü Variablen
    private JButton bnOpen = new JButton("Oeffnen");
    private JButton bnValidate = new JButton("Validate?");
    private JButton bnParsing = new JButton("Parsing");

   private JTextArea editorXML = new JTextArea();
   private JTextArea editorParsing = new JTextArea();

   private String filenameXML="";
   private String filenameXSD="";

   // Konstruktor
  public ReadXML() {
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setGUI();
  }

  public void setGUI() {
    this.getContentPane().setLayout( new BorderLayout() );
    this.setSize(new Dimension(600, 400));

   JTabbedPane jTabbedPane1 = new JTabbedPane();
   jTabbedPane1.setFont( new Font("Arial", Font.BOLD,16));

    editorXML = new JTextArea("abc\ndef");
    editorXML.setFont(new Font("Arial", Font.BOLD, 18));

    editorParsing = new JTextArea();
    editorParsing.setFont(new Font("Arial", Font.BOLD, 18));
    
    JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,false,     
    new JScrollPane(editorXML), new JScrollPane(editorParsing) );
    this.getContentPane().add( split, BorderLayout.CENTER );
    split.setDividerLocation(350);
    
    add(split , BorderLayout.CENTER);

    setButtons();
  }  // setGUI


    // setzt die Menüs
  private void setButtons() {
    JPanel panel = new JPanel();

   panel.add(bnOpen);
   panel.add(bnValidate);
   panel.add(bnParsing);
   
   
   bnOpen.setFont(new Font("Arial", Font.BOLD, 18));
   bnValidate.setFont(new Font("Arial", Font.BOLD, 18));
   bnParsing.setFont(new Font("Arial", Font.BOLD, 18));
   add(panel , BorderLayout.NORTH);


   bnOpen.addActionListener(new ActionListener()  {
	      public void actionPerformed(ActionEvent e) {
	        bnOpen_click();
	      }
	    });
   bnValidate.addActionListener(new ActionListener()  {
	      public void actionPerformed(ActionEvent e) {
	    	  bnValidate_click();
	      }
	    });


    bnParsing.addActionListener(new ActionListener()  {
      public void actionPerformed(ActionEvent e) {
        bnParsing_click();
      }
    });
    bnValidate.setEnabled(false);
    bnParsing.setEnabled(false);
  } // setButtons

//     * @see #getFileExt Ändern der Dateierweiterung  .TXT  -> .DAT
  public String changeFileExt(String sFileName, String sExt) {
      String sErgFile;
      int k;
      sErgFile = "";
      File f = null;
      //File.pa
      try {
          // eventuell kann auch f.pathSeparatorChar verwendet werden
          // suche letzten Punkt
          k = sFileName.lastIndexOf(".");
          if (k == -1){ // kein Punkt vorhanden
            return new String(sFileName+sExt);
          }
          else {
            int n = sFileName.length();
              // beginIndex    EndIndex
            sErgFile = sFileName.substring(0,k);  // zählt von 0
            return new String(   sErgFile+sExt);
          }

      }
      catch (Exception e) {
      }
      return sErgFile;
  }  // ChangeFilenameExt
  
  
  public  boolean FileExists(String sFileName) {
      File f = null;
      try {
          // evnetuell kann auch f.pathSeparatorChar verwendet werden
          f = new File(sFileName);
          if (f.exists())
              return true;
          else
              return false;
      }
      catch (Exception e) {
          return false;
      }
  }  // FileExists


    


  void bnOpen_click() {
   JFileChooser jFileChooser = new JFileChooser();
   jFileChooser.setMultiSelectionEnabled(false);
   //jFileChooser1.setCurrentDirectory( new File("C:\\DATEN") );
   jFileChooser.setCurrentDirectory(new File( (String) System.getProperties().get("user.dir")) );
   if (JFileChooser.APPROVE_OPTION == jFileChooser.showOpenDialog(this)) {
          readFile(jFileChooser.getSelectedFile().getPath());
    	  bnParsing.setEnabled(true);
          if (FileExists(filenameXSD) ) {
              bnValidate.setEnabled(true);
          }
    }
  }  // MnOpen
  
   private void readFile(String filename) {
         FileInputStream fin;
         InputStreamReader iin;
         LineNumberReader din;
         String sLine;
         StringBuilder sb = new StringBuilder();
         filenameXML = filename;
         filenameXSD = changeFileExt(filename,".xsd");
         try {
           fin = new FileInputStream(filename);
           iin = new InputStreamReader(fin);
           din = new LineNumberReader(iin);  // oder BufferedReader br = new BufferedReader(ir);
           while  ( din.ready() ) {
             sLine = din.readLine();
             sb.append(sLine);
             sb.append("\n");
             editorXML.setText(sb.toString());
           }
         }
          catch (FileNotFoundException e1) {
              System.err.println("Datei war nicht vorhanden!");
         }
         catch (IOException ee) {
                System.err.println("IOException: " + ee);
         }
   }
   
   
   private boolean bnValidate_click() {
	   try {		   
           SchemaFactory factory =  SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
           Schema schema = factory.newSchema(new File(filenameXSD));
           Validator validator = schema.newValidator();
           validator.validate(new StreamSource(new File(filenameXML)));
       } catch (IOException | SAXException e) {
           System.out.println("Exception: "+e.getMessage());
    	   editorParsing.setText( "XML-Datei ist NICHT valide\n\n" ) ;
    	   editorParsing.append("Fehler:\n"+e.getMessage());           
           return false;
       }
	   editorParsing.setText( "XML-Datei ist valide" ) ;       
       return true;
   }

  public String getInput( String sTitle, String sMessage) {
    return JOptionPane.showInputDialog(null, sMessage, sTitle , 1);
  } // getInput


   private void bnParsing_click() {
      try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse( new File(filenameXML) );

        String node1 = getInput("Traversieren","SearchTag");
        if (node1.length()==0) return;

        String node2 = getInput("Traversieren","ElementTag");
        if (node2.length()==0) return;
        NodeList nodelist = document.getElementsByTagName(node1);
        
        editorParsing.setText( "" ) ;
        for (int i=0;  i

Java (Swing)
Autovermietung.xml