XSD SchemaFactory 错误 - Java 6

发布于 2024-12-21 21:21:18 字数 1675 浏览 5 评论 0原文

在下面的代码中,我尝试验证 XML。当我从 eclipse 运行它时发现一切正常。当我在 weblogic 上部署它时,代码失败并出现错误:“处理 src-resolve 时出现 SAX 异常:无法将名称 XX:sometag 解析为(n)‘类型定义’组件。”

SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
System.out.println("schemaFactory -> " + schemaFactory.getClass());
Schema schema = schemaFactory.newSchema(resourceHandler.getXSDStreams());
StreamSource xmlContent = new StreamSource(new StringReader(requestXML));
Validator validator = schema.newValidator();
validator.validate(xmlContent);

这是在 eclipse 上为 Schema Factory 生成的输出: 模式工厂-> class com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory

在 Weblogic 上,具有相同 JAR 文件的相同代码会出现上述错误。 schemaFactory 完全相同。

Config - 2

我尝试设置系统属性来加载 xerces 文件并将 xerces 添加到类路径中,

String schemaFactoryProperty = "javax.xml.validation.SchemaFactory:" + XMLConstants.W3C_XML_SCHEMA_NS_URI;
System.setProperty(schemaFactoryProperty,"org.apache.xerces.jaxp.validation.XMLSchemaFactory");  

模式工厂的输出仍然保持不变:“schemaFactory -> class com.sun.org.apache.xerces.internal.jaxp。 validation.XMLSchemaFactory”,但这次错误更改为:

java.lang.IllegalArgumentException: Source parameter of type 'javax.xml.transform.stream.StreamSource' is not recognized this SchemaFactory.
    at com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory.newSchema(XMLSchemaFactory.java:203)
    at com.bt.asi.task.AffectedServiceTask.execute(AffectedServiceTask.java:47)
    at com.bt.asi.service.ASIJMSService.onMessage(ASIJMSService.java:30)
    at... 

我使用 Java 6 和 WLS 10.3.0 (Java 6) 并使用 Spring 框架。

In the code below, I am trying to validate a XML. Everything works find when i run it from eclipse. When i deploy it on weblogic the code fails with error: "SAX Exception while processingsrc-resolve: Cannot resolve the name XX:sometag to a(n) 'type definition' component."

SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
System.out.println("schemaFactory -> " + schemaFactory.getClass());
Schema schema = schemaFactory.newSchema(resourceHandler.getXSDStreams());
StreamSource xmlContent = new StreamSource(new StringReader(requestXML));
Validator validator = schema.newValidator();
validator.validate(xmlContent);

This is the output generated on eclipse for Schema Factory:
schemaFactory -> class com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory

On Weblogic the same code with the same JAR files gives the error mentioned above. The schemaFactory is exactly the same.

Config - 2

I tried setting the System property to load xerces files and added xerces to the classpath

String schemaFactoryProperty = "javax.xml.validation.SchemaFactory:" + XMLConstants.W3C_XML_SCHEMA_NS_URI;
System.setProperty(schemaFactoryProperty,"org.apache.xerces.jaxp.validation.XMLSchemaFactory");  

Still the output for the schema factory remains the same: "schemaFactory -> class com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory" but this time the error changed to:

java.lang.IllegalArgumentException: Source parameter of type 'javax.xml.transform.stream.StreamSource' is not recognized this SchemaFactory.
    at com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory.newSchema(XMLSchemaFactory.java:203)
    at com.bt.asi.task.AffectedServiceTask.execute(AffectedServiceTask.java:47)
    at com.bt.asi.service.ASIJMSService.onMessage(ASIJMSService.java:30)
    at... 

I am on Java 6 and WLS 10.3.0 (Java 6) and using Spring framework.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傾城如夢未必闌珊 2024-12-28 21:21:18

开始工作了(4 小时后!!!)。

在本地,它工作的原因:架构是从文件夹加载的,因此 XSD 中的导入就像以前一样工作。

在服务器上,我放入了一个 jar 文件,因此 sourceLocation="SomeFile.xsd" 从未用于生成文件,因为实际路径是 URI。

是什么解决了它:
1)我更改了加载模式的顺序(从无导入 - >所有导入)以确保当前xsd编译时它的所有依赖项已经在schemaFactory中(这是可能的,因为我没有下一步导入,所以我有一个明确的顺序,即首先加载哪个文件等等)

2)我也可以做的是 - 将所有 XSD 合并到一个文件中。

3) 或者我可以将 XSD 保存在 web-inf/ 中,然后查找它们而不是 JAR 文件。然后 import sourceLocation="" 就会找到该文件。

希望这对某人有帮助!

干杯。
维韦克

Got it working (after 4 hrs !!!).

On local why it worked: The Schemas were loaded from a folder so the imports within the XSD like used to work.

On Server i had put in a jar file so the sourceLocation="SomeFile.xsd" never used to result in a file as the actual path was a URI.

What fixed it:
1) I changed the sequence in which i load the schemas (from no imports --> all imports) to make sure when the current xsd compiles all dependencies for it are already in the schemaFactory (It was possible as i did not have nexted imports so i had a clear order of what file to load first and so on)

2) What i could have done as well was - combine all XSDs to one file.

3) OR I could have kept the XSD in web-inf/ and then looked them up instead of a JAR file. Then the import sourceLocation="" would have found that file.

Hope this helps someone !!!

Cheers.
Vivek

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文