Hallo Gemeinde, mal ein einfaches Java Programm, das prüft, ob eine XML Datei valide ist. Allerdings ohne Berücksichtigung eines Schemas.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | import java.io.File; import javax.swing.JFileChooser; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; public class Main { public static void main(String[] args) { try { JFileChooser chooser = new JFileChooser(); int rc = chooser.showOpenDialog(null); if (rc == JFileChooser.APPROVE_OPTION) { File file = chooser.getSelectedFile(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); org.w3c.dom.Document doc = db.parse(file.getAbsolutePath()); System.out.println("XML Version: " + doc.getXmlVersion()); } else { System.out.println("Main:main:abgebrochen"); } } catch (Exception e) { System.out.println("Main:main:Exception:"); System.out.println(e.toString()); } } } |
Moderiert von schwarzheit:
Verschoben, da kein Problem besteht das gelöst werden soll.