我目前使用我的XSD來驗證我的xml。這部分工作正常我的porblem是我想獲得無效的標記/值的元素。如何獲取失敗的xsd的元素和無效的xml文件驗證
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;
}
我的想法是不實際的是看單詞「類型」或「結束標記」的消息,並獲得價值後,但我知道這不會是好實踐!我發現這令人沮喪,因爲我可以看到無效但無法控制的標籤!
以下是該元素的一些例子我想
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>".
是否有無論如何,我可以以字符串的形式獲取無效值。 – 2016-02-02 21:49:18
正是我想要的 – pup784 2016-06-01 18:19:14