我正在使用带有自定义处理程序的SAX解析器来解析一些XML文件。到目前为止,此方法运行良好,但我想检查的不只是给定文件的格式正确,还要通过XSD方案使用验证,该方案还包含可选属性的默认值。在线上有很多关于此操作的教程,但是我找不到满足我所有约束的方法,如下所示:
-我事先不知道该方案,我有一堆XML和XSD文件,每个XML都包含有关应遵循的XSD的信息
-validatior应该更改处理程序获取的流,并在必要时从XSD插入可选属性的默认值
-应该使用当前的自定义处理程序
我对这个话题还很陌生,因此不能排除我在不了解它的情况下就迷失了解决方案,但是目前我对如何做到这一点完全感到困惑。
这是最低的SSCCE,其中应显示问题和相关部分:
package parserTest;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.validation.TypeInfoProvider;
import javax.xml.validation.ValidatorHandler;
import org.w3c.dom.ls.LSResourceResolver;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.ErrorHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class ParserTest
{
public final static void main(String[] args)
{
//Initialize SAX parser
final SAXParserFactory saxFactory = SAXParserFactory.newInstance();
SAXParser saxParser = null;
try
{
saxParser = saxFactory.newSAXParser();
}
catch(ParserConfigurationException confEx){confEx.printStackTrace();}
catch (SAXException saxEx){saxEx.printStackTrace();}
//Initialize Handler
DefaultHandler saxHandler = new CustomHandler();
ValidatorHandler vh = new ValidatorHandler()
{
@Override
public void startPrefixMapping(String prefix, String uri) throws SAXException{}
@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException{}
@Override
public void startDocument() throws SAXException{}
@Override
public void skippedEntity(String name) throws SAXException{}
@Override
public void setDocumentLocator(Locator locator){}
@Override
public void processingInstruction(String target, String data) throws SAXException{}
@Override
public void ignorableWhitespace(char[] ch, int start, int length)throws SAXException{}
@Override
public void endPrefixMapping(String prefix) throws SAXException{}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException{}
@Override
public void endDocument() throws SAXException{}
@Override
public void characters(char[] ch, int start, int length) throws SAXException{}
@Override
public void setResourceResolver(LSResourceResolver resourceResolver){}
@Override
public void setErrorHandler(ErrorHandler errorHandler){}
@Override
public void setContentHandler(ContentHandler receiver){}
@Override
public TypeInfoProvider getTypeInfoProvider(){return null;}
@Override
public LSResourceResolver getResourceResolver(){return null;}
@Override
public ErrorHandler getErrorHandler(){return null;}
@Override
public ContentHandler getContentHandler(){return null;}
};
vh.setContentHandler(saxHandler);
//Do the parsing
File input = new File("");
try
{
saxParser.parse(input, saxHandler);
//saxParser.parse(input, vh); //<-- First attempt, gives me error message
//saxParser.setContentHandler(vh); //<-- Second attempt, but my parser does not seem to know this method
}
catch (IOException ioEx){ioEx.printStackTrace();}
catch (SAXException saxEx){saxEx.printStackTrace();}
}
/*
* This class is the handler to be used only by this class.
*/
static private final class CustomHandler extends DefaultHandler
{
//Handle start of element
public final void startElement(String namespaceURI, String localName, String qName, Attributes atts){}
//Handle end of Element
public final void endElement(String namespaceURI, String localName, String qName){}
//Handle start of characters
public final void characters(char[] ch, int start, int length){}
}
}
最佳答案
基本原理是在SAX解析器和ContentHandler之间插入ValidatorHandler。
https://xerces.apache.org/xerces2-j/javadocs/api/javax/xml/validation/ValidatorHandler.html
ValidatorHandler vh = new ValidatorHandler();
vh.setContentHandler(originalContentHandler);
parser.setContentHandler(vh);
棘手的一点是,为了创建ValidatorHandler,您需要知道正在使用哪种模式。如何识别?如果它使用xsi:schemaLocation属性,则可以(可能)获取ValidatorHandler来自动将其拾取。如果它使用某种自定义机制,则可能必须进行“预传递”读取(部分读取)源文件以发现模式,然后在适当的位置使用ValidatorHandler再次读取它。
将为您的ContentHandler通知可选属性的默认值。