2014-03-19 27 views
1

我有一個非常簡單的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文件,因爲它可能會破壞其他的東西

回答

1

我最後做的是改變了模式文件

<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());