我使用带有自定义处理程序的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){}
}
}
答案 0 :(得分:1)
基本原则是在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以自动获取它。如果它使用一些自定义机制,你可能必须做一个&#34; prepass&#34;读取(部分)源文件以发现模式,然后使用ValidatorHandler再次读取它。
您的ContentHandler将收到有关可选属性的默认值的通知。