Mulu
Mulu

Reputation: 1

Getting elements from failed XML

I have a big xml file to be validated against a big XSD. The client asked me to populate a table with different values of data when there is a validation error. For eg if Student ID is not valid, I will show school district, region and student ID. In another section of the XML, if state is not valid I will show school name, state and region. The data to show varies based on the invalid data. But its two or three or four elements which are parents of the invalid child element should be extracted.

How I can extract data using XMLSTREAMREADER and Validator?

I tried this one and I can get only the invalid element not other data...

public class StaxReaderWithElementIdentification {

private static final StreamSource XSD = new StreamSource("files\\InterchangeEducationOrganizationExension.xsd");
private static final StreamSource XML = new StreamSource("files\\InterchangeEducationOrganizationExension.xml");

public static void main(String[] args) throws Exception {
    SchemaFactory factory=SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = factory.newSchema(XSD);

    XMLStreamReader reader = XMLInputFactory.newFactory().createXMLStreamReader(XML);
    Validator validator = schema.newValidator();
    validator.setErrorHandler(new MyErrorHandler(reader));
    validator.validate(new StAXSource(reader));

}
}

and Handler is:

public class MyErrorHandler implements ErrorHandler {

private XMLStreamReader reader;

public MyErrorHandler(XMLStreamReader reader) {
    this.reader = reader;
}

@Override
public void error(SAXParseException e) throws SAXException {
    warning(e);
}

@Override
public void fatalError(SAXParseException e) throws SAXException {
    warning(e);
}

@Override
public void warning(SAXParseException e) throws SAXException {
    //System.out.println(reader.getProperty(name));
    System.out.println(reader.getLocalName());
    System.out.println(reader.getNamespaceURI());
    e.printStackTrace(System.out);
    }

}

Can anyone help me how I can extract the other data when the validation error occurred?

Upvotes: 0

Views: 75

Answers (1)

linski
linski

Reputation: 5094

I'm not sure it is the best solution, but you might try using HTML EditorKit and implement a custom ParserCallback.

In that manner you could parse the document and react only to tags you are interested in. It will chew any XML/HTML no matter how invalid it is.

Upvotes: 1

Related Questions