【问题标题】:javax.xml.validation.Schema not reading server file in JBoss EAP 5.1 correctlyjavax.xml.validation.Schema 没有正确读取 JBoss EAP 5.1 中的服务器文件
【发布时间】:2013-11-20 02:07:56
【问题描述】:

我在 RESTEasy Web 服务上运行了以下 Java 代码,以获取用于验证 xml 的架构文件(注意:项目文件夹名为“MyWebService”):

String classDir = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();    
String myWebServiceDir = classDir.substring(0, classDir.lastIndexOf("MyWebService"));       

String instanceXSDPath = myWebServiceDir + SCHEMAS_FOLDER + xsdFile;
System.out.println("Streamsource file location: " + instanceXSDPath);
File file = new File(instanceXSDPath);

//StreamSource streamSource = new StreamSource(file);
// Note: Using StreamSource or File seems to make no difference to the issue
Schema schema = factory.newSchema(file);

当我在本地机器上运行上述代码时,一切正常。但是,当我在我的开发服务器上运行上述代码时,我收到以下错误:

2013-11-20 12:47:48,275 信息 [标准输出] (ajp-127.0.0.1-8009-4) 流源文件位置: 文件:/Y:/jboss/jboss-as/server/default/deploy/DEVELOPER_DEPLOY/Schemas/Extensions/1/instance.xsd 2013-11-20 12:47:48,275 错误 [com.mywebservice.dao.validate] (ajp-127.0.0.1-8009-4) 无法解析给定对象的模式 验证:org.xml.sax.SAXParseException:schema_reference.4:失败 阅读架构文档 'file:/Y:/jboss/jboss-as/bin/file:/Y:/jboss/jboss-as/server/default/deploy/DEVELOPER_DEPLOY/Schemas/Extensions/1/instance.xsd', 因为 1) 找不到文件; 2) 文件不能 读; 3) 文档的根元素不是 .在 org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(未知 来源)

我不明白为什么在服务器 EAP 日志中,上面的代码试图从如下所示的路径中读取 instance.xsd:

'file:/Y:/jboss/jboss-as/bin/file:/Y:/jboss/jboss-as/server/default/deploy/DEVELOPER_DEPLOY/Schemas/Extensions/1/instance.xsd'

...因为在我的 localhost EAP 日志中,它正在从如下所示的位置读取 instance.xsd:

'位置: /C:/Users/chen/workspace/.metadata/.plugins/org.jboss.ide.eclipse.as.core/JBoss_5.1_Runtime_Server1382499548190/deploy/Schemas/Extensions/1/instance.xsd'

有人知道为什么吗?为什么打印消息在服务器上显示“文件”,但在我的 locahost 上分别显示在每个服务器日志中的“位置”?也许这与问题有关。

【问题讨论】:

    标签: java xml eclipse jboss xsd


    【解决方案1】:

    答案是使用 StreamSource 并将 StreamSource 的 System Id 设置为其原始路径,如下所示:

    StreamSource streamSource = new StreamSource(file);
    System.out.println("Stream source system id: " + streamSource.getSystemId());
    streamSource.setSystemId(xsltPath);
    Schema schema = factory.newSchema(streamSource);  // change for each artefact type
    

    这很有效,可以防止两个“文件:”位置的混搭,因为它将系统 ID 硬连线为一个。我不知道为什么服务器将两个“文件:”位置混合到系统 ID 中,但谁在乎,问题解决了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-15
      • 1970-01-01
      • 1970-01-01
      • 2019-12-26
      • 2017-09-21
      • 1970-01-01
      • 2022-12-30
      相关资源
      最近更新 更多