0
檢查xml中的所有節點是否格式良好。 我想拋出所有錯誤&錯誤的格式在XML中,但它會拋出單個節點丟失的異常。應該一次拋出所有解析錯誤:解析un well格式的XML
我需要丟棄所有丟失的節點&不正確形成的節點xml一次。 在此先感謝。
/**
* To Validate the XML with its Schema
* @param xmlPath
* @param schemaXSDPath
* @return
*/
public static boolean validateXMLagainstSchema(String xmlPath,String schemaXSDPath)
{
try
{
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
factory.setNamespaceAware(true);
SAXParser parser = null;
try
{
parser = factory.newSAXParser();
}
catch (ParserConfigurationException ex)
{
ex.printStackTrace();
return false;
}
catch (SAXException ex)
{
ex.printStackTrace();
return false;
}
parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema");
parser.setProperty(
"http://java.sun.com/xml/jaxp/properties/schemaSource",
schemaXSDPath);
SAXReader reader = new SAXReader(parser.getXMLReader());
InputSource ip=new InputSource(xmlPath);
reader.read(ip);
}
catch (SAXNotRecognizedException ex)
{
ex.printStackTrace();
return false;
}
catch (SAXNotSupportedException ex)
{
ex.printStackTrace();
return false;
}
catch (SAXParseException ex)
{
ex.printStackTrace();
return false;
}
catch (SAXException ex)
{
ex.printStackTrace();
return false;
}
catch (DocumentException ex)
{
ex.printStackTrace();
return false;
}
return true;
}