Java XML SAX Parser isXML(final byte[] data)

Here you can find the source of isXML(final byte[] data)

Description

Comprueba si los datos proporcionados son un XML válido.

License

Open Source License

Parameter

Parameter Description
data Datos a evaluar.

Return

true cuando los datos son un XML bien formado. false en caso contrario.

Declaration

public static boolean isXML(final byte[] data) 

Method Source Code

//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;
    }
}

Related

  1. getSchema(URL xml_schema)
  2. getSchemaFromResource(URL schemaURL)
  3. getXIncludeXMLReader()
  4. getXmlReader()
  5. isWellFormedXml(final String string)
  6. load(InputStream is, DefaultHandler handler)
  7. newInstance()
  8. newXMLReader()
  9. read(InputStream is, DefaultHandler h)