Вопрос

My problem is with parsing an XSD Schema that has elements with maxOccurs larger than 5000 (but not unbounded).

This is actually a know issue in either Xerces (which I'm using, version 2.9.1) or JAXP, as described here: http://bugs.sun.com/view_bug.do;jsessionid=85335466c2c1fc52f0245d20b2e?bug_id=4990915

I already know that if I changed the maxOccurs numbers in my XSD from numbers larger than 5000 to unbounded all works well. Sadly, this is not an option in my case (I cannot meddle with the XSD file).

My question is:

  • Does someone know some other workaround in Xerces for this issue? Or
  • Can someone recommend another XML parser that does not have this limitation?

Thanks!

Это было полезно?

Решение 2

I have found a solution that doesn't require changing the parser.

There is a FEATURE_SECURE_PROCESSING feature which puts that 5000 limitation on maxOccurs (along with several others).

And here is the document describing the limitations: http://docs.oracle.com/javase/7/docs/technotes/guides/xml/jaxp/JAXP-Compatibility_160.html#JAXP_security

Другие советы

I had the same problem. I used this:

System.setProperty("jdk.xml.maxOccurLimit", "XXXXX");

I came across this thread when looking for solutions for this problem when using xjc command in console.

For anyone who is using xjc command to parse xsd, this works for me:

$ xjc -nv foo.xsd

Be aware though:

By default, the XJC binding compiler performs strict validation of the source schema before processing it. Use this option to disable strict schema validation. This does not mean that the binding compiler will not perform any validation, but means that it will perform a less-strict validation.

So if you think your xsd is from a good source, using less strict validation should not be a problem.

If you use Eclipse IDE with the Dali plugin for JAXB you may obtain the aforementioned error in the console.

Such error can be avoided if you uncheck 'Use strict validation' on panel 'Classes Generator Options' when establishing the options for JAXB generation from a XSD file. Such panel is the third one after 'Java Project' and 'Generate classes from Schema'.

Classes Generator Options

Adding the additional argument -nv suggested by @minjun-yu also works. Instead of applying my first suggestion, you can set such argument in the fourth panel labeled 'Classes Generator Extension Configurations'

Classes Generator Extension Configurations

On parsing data to load the JAXB generated classes, if you are validating against a schema, you still may obtain the SAXException. As pointed by @mzywiol and @marioosh, the exception is avoided setting a special feature on creating the SchemaFactory

    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    // Avoid SAXParseException on maxOccurs > 5000 
    sf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, false); 
    
    URL xsdURL = TestParse.class.getResource(xsdLocation);
    Schema schema = sf.newSchema(xsdURL);

    JAXBContext ctx = JAXBContext.newInstance(MyJAXBClass.class.getPackage().getName());

    Unmarshaller unmarshaller = ctx.createUnmarshaller(); 
    unmarshaller.setSchema(schema);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top