SAXParseException - 不允许属性;但只有在JBoss中
我有一个非常简单的XSD文件SAXParseException - 不允许属性;但只有在JBoss中
<xs:element name="SummaryStatus">
<xs:complexType>
<xs:attribute name="cnt" type="xs:int" use="required"></xs:attribute>
<xs:attribute name="err" type="xs:int" use="optional" default="0"></xs:attribute>
</xs:complexType>
</xs:element>
,我用它来验证此XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<SummaryStatus cnt="1" />
anywawy,验证自身当我在单元测试中运行它时,一切都按预期工作,验证显然使用xml解析器从com.sun.org.apache.xerces * TC。从\的Java \ jdk1.7.0_51 \ JRE \ LIB \ rt.jar的
当我部署了整个事情到JBoss,它使用组织。 apache.xerces。*从jboss-4.2.3.GA \ LIB \认可\ xercesImpl.jar和我得到这个例外
Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 63; cvc-complex-type.3.2.2: Attribute 'cnt' is not allowed to appear in element 'SummaryStatus'.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.reportSchemaError(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.processAttributes(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
at org.apache.xerces.jaxp.validation.ValidatorHandlerImpl.startElement(Unknown Source)
at com.sun.xml.bind.v2.runtime.unmarshaller.ValidatingUnmarshaller.startElement(ValidatingUnmarshaller.java:89)
at com.sun.xml.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(InterningXmlVisitor.java:71)
at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:137)
at org.xml.sax.helpers.XMLFilterImpl.startElement(XMLFilterImpl.java:551)
at at.apa.commons.webservice.http.xml.JaxbXmlResponseStrategy$NamespaceFilter.startElement(JaxbXmlResponseStrategy.java:188)
at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source)
at org.apache.xerces.parsers.AbstractXMLDocumentParser.emptyElement(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.emptyElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentScannerImpl$ContentDispatcher.scanRootElementHook(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at org.xml.sax.helpers.XMLFilterImpl.parse(XMLFilterImpl.java:357)
我的验证/解组代码归结为
XMLFilter filter = new NamespaceFilter(getXmlns());
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
SAXParser saxParser = saxParserFactory.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
filter.setParent(xmlReader);
unmarshaller = jaxbContext.createUnmarshaller();
Schema schema = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema").newSchema(getSchemaLocation());
unmarshaller.setSchema(schema);
UnmarshallerHandler unmarshallerHandler = unmarshaller.getUnmarshallerHandler();
filter.setContentHandler(unmarshallerHandler);
InputSource xml = getInputSource();
filter.parse(xml);
return (ResponseType)unmarshallerHandler.getResult();
其中
getXmlns() = " http://apa.at/powersearch/inputservice/SummaryStatus " and getSchemaLocation() = SummaryStatus.class.getClassLoader().getResource("schema/SummaryStatus.xsd")
我使用getResource的模式文件的URL是有效的,并且可以正确解析(我尝试在url上打开输入流并在jboss中运行时读取模式,并且工作 - 所以架构正在加载正确)
和过滤器看起来像
protected static class NamespaceFilter extends XMLFilterImpl {
private final String xmlns;
public NamespaceFilter(String xmlns) {
this.xmlns = xmlns;
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
super.endElement(xmlns, localName, qName);
}
@Override
public void startElement(String uri, String localName,
String qName, Attributes atts) throws SAXException {
super.startElement(xmlns, localName, qName, atts);
}
}
我使用此过滤器,因为XML我从Web服务中获得有没有xmlns声明,所以我“注入”是手动,这样我就可以验证它反对该模式 - 再次这工作完全正常(使用com.sun的东西,但与jboss类失败)
我试图更新xercesImpl.jar与我的jboss版本2.0+从maven回购,但这只是在启动服务器时导致一堆异常,我真的不想开始切换出jar文件,因为它可能会破坏其他的东西
我最后做的是改变了模式文件
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="SummaryStatus">
<xs:complexType>
<xs:attribute name="cnt" type="xs:int" use="required"></xs:attribute>
<xs:attribute name="err" type="xs:int" use="optional" default="0"></xs:attribute>
</xs:complexType>
</xs:element>
</xs:schema>
之前的架构元素包含了xmlns和targetNamespace的声明,现在它不再做,所期望的XML元素我验证了而不是无论如何都在命名空间中,我可以解组xml用简单的unmarshaller +架构
unmarshaller = getJaxbContext().createUnmarshaller();
if(getRequest().isResponseStrict()) {
unmarshaller.setSchema(getSchema());
}
return (ResponseType)unmarshaller.unmarshal(getXmlInputStream());