Here you can find the source of isXML(final byte[] data)
Parameter | Description |
---|---|
data | Datos a evaluar. |
public static boolean isXML(final byte[] data)
//package com.java2s; /* Copyright (C) 2011 [Gobierno de Espana] * This file is part of "Cliente @Firma". * "Cliente @Firma" is free software; you can redistribute it and/or modify it under the terms of: * - the GNU General Public License as published by the Free Software Foundation; * either version 2 of the License, or (at your option) any later version. * - or The European Software License; either version 1.1 or (at your option) any later version. * Date: 11/01/11// www. jav a 2 s .co m * You may contact the copyright holder at: soporte.afirma5@mpt.es */ import java.io.ByteArrayInputStream; import java.util.logging.Logger; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.ErrorHandler; import org.xml.sax.InputSource; import org.xml.sax.SAXParseException; import org.xml.sax.XMLReader; public class Main { /** * Comprueba si los datos proporcionados son un XML válido. * @param data Datos a evaluar. * @return {@code true} cuando los datos son un XML bien formado. {@code false} * en caso contrario. */ public static boolean isXML(final byte[] data) { final SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(false); factory.setNamespaceAware(true); try { final SAXParser parser = factory.newSAXParser(); final XMLReader reader = parser.getXMLReader(); reader.setErrorHandler(new ErrorHandler() { @Override public void warning(final SAXParseException e) { log(e); } @Override public void fatalError(final SAXParseException e) { log(e); } @Override public void error(final SAXParseException e) { log(e); } private void log(final Exception e) { Logger.getLogger("es.gob.afirma").fine("El documento no es un XML: " + e); //$NON-NLS-1$ //$NON-NLS-2$ } }); reader.parse(new InputSource(new ByteArrayInputStream(data))); } catch (final Exception e) { return false; } return true; } }