Attualmente sto usando il mio XSD per convalidare il mio xml. Questa parte funziona bene il mio problema è che voglio ottenere l'elemento del tag/valore che non è valido.Come ottenere l'elemento di un file xml non valido con convalida xsd non riuscita
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
XMLStreamReader reader = null;
SchemaFactory factory=SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(xsdschemalocation);
Validator validator = schema.newValidator();
try
{
reader = XMLInputFactory.newInstance().createXMLStreamReader(new StreamSource(new StringReader(xml)));
} catch (XMLStreamException ex)
{
LogController.getLogger().logSEVERE("Unable to create the streamreader from the xml source", ex.getLocalizedMessage());
return false;
}
try
{
validator.validate(new StAXSource(reader));
}
catch (IOException ex)
{
LogController.getLogger().logSEVERE("IOException in the validatation has been caused as the reader has become null", ex.getLocalizedMessage());
return false;
}
catch(SAXException saxe)
{
LogController.getLogger().logWARNING("Their is a validation error with the xml", saxe.getLocalizedMessage());
//*****HERE I WANT THE TAG THAT HAS THE ERROR
ClientCommunication.ErrorMessageForClient(VALIDATION_ERROR, socket);
CloseClientConnection();
return;
}
L'idea che avevo, che non è pratico è quello di cercare nel messaggio per la parola "tipo" o "end-tag" e ottenere il valore dopo che, però so che questo non sta per essere buono pratica! Trovo questo frustrante come posso vedere il tag che non è valido ma non riesco a farcela!
Ecco alcuni esempi dell'elemento voglio
1. Message: Element type "first" must be followed by either attribute specifications, ">" or "/>".
2. javax.xml.stream.XMLStreamException: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 353; cvc-pattern-valid: Value '079e989989' is not facet-valid with respect to pattern '([0-9])+' for type 'phoneNumber'.
3. Message: The element type "firstLine" must be terminated by the matching end-tag "</firstLine>".
È comunque possibile ottenere il valore non valido anche sotto forma di stringa. –
Esattamente quello che volevo – pup784