4

I am trying to validate (valid xml file) an xml when creating a XMLStreamReader using a XMLInputFactory.

I know that there is property that can be set in the factory javax.xml.stream.isValidating to allow a specific-implementation validation (in my case of xml). But i am working with java 6 build 43 (Jboss eap 6.0) and cannot update so this is not working for me (kwnon issue).

So if anybody know about any API (other than StAX) that could do the same thing that would be great ! Please let me know if something is not clearly explained.

Divya
  • 1,469
  • 1
  • 13
  • 25
maher.belkh
  • 473
  • 2
  • 7
  • 21
  • It is very unclear what you're asking. For starters, validation is always against a schema of some sort (XSD, RELAX, Schematron, etc). For the difference between ***valid XML*** and ***well-formed XML***, I suggest that you read [**this**](http://stackoverflow.com/a/25830482/290085). Thanks. – kjhughes Nov 04 '14 at 12:50
  • You're right Ashes .What i meant is Well-formed xml file. for example when i open a none well formed xml file with firefox it shows an error (example XML Parsing Error). – maher.belkh Nov 04 '14 at 13:37

2 Answers2

1

Found it, i used the api SAX to create a parser (independently of a xsd schema) using the provided SAXParserFactory and to parse the file. (more details can be found on pages (23/24) at http://apiacoa.org/publications/teaching/xml/api-xml.pdf)

Also, don't forget to mark/reset your inputstream before/after you parse it with the parser (that will throw an exception if the xml file is not well-formed) so you can keep as it is if the parse does not fail.

maher.belkh
  • 473
  • 2
  • 7
  • 21
-2

you can try for java xml validation API by IBM validation API or you can use javax.xml.validation javax.xml.validation

ex :

import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.*;
...

URL schemaFile = new URL("http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd");
Source xmlFile = new StreamSource(new File("web.xml"));
SchemaFactory schemaFactory = SchemaFactory
    .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(schemaFile);
Validator validator = schema.newValidator();
try {
  validator.validate(xmlFile);
  System.out.println(xmlFile.getSystemId() + " is valid");
} catch (SAXException e) {
  System.out.println(xmlFile.getSystemId() + " is NOT valid");
  System.out.println("Reason: " + e.getLocalizedMessage());
}

EDITED :

SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

        File schemaFile = new File("schema.xsd");

        Schema xsdScheme = factory.newSchema(schemaFile);

        Validator validator = xsdScheme.newValidator();

        Source source = new StreamSource(xmlfile);

        validator.validate(source);

one link which i found on SO xsd validation

Community
  • 1
  • 1
Divya
  • 1,469
  • 1
  • 13
  • 25
  • 2
    He asked *"without having a specific XSD"*, yet you offer an answer with a specific XSD (`http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd`)? – kjhughes Nov 04 '14 at 12:54