首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在scala中从XSD文件中提取模式: scalaxb

在scala中从XSD文件中提取模式: scalaxb
EN

Stack Overflow用户
提问于 2018-10-19 05:28:49
回答 1查看 732关注 0票数 0

在我的搜索过程中,我偶然发现了为实现此要求而指定的scalaxb包。但是提取是我现在卡住的东西。

由于我对XSD的理解有限,我不能区分哪个元素应该被认为是模式字段&哪个不应该。

XSD文件片段:

代码语言:javascript
复制
   <xs:element name="preList">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="nNumber" type="remNum"/>
            <xs:element name="fileName" type="FileName"/>
            <xs:element name="recordCnt" type="RecordCount"/>
            <xs:choice maxOccurs="unbounded">
               <xs:element ref="tfiws"/>
               <xs:element ref="structured"/>
            </xs:choice>
         </xs:sequence>
      </xs:complexType>
   </xs:element>
   <xs:element name="tfiws">
      <xs:complexType>
         <xs:sequence>
            <xs:element ref="header" minOccurs="0"/>
            <xs:element name="tfMst" type="TFMsg"/>
            <xs:element name="turns" minOccurs="0">
               <xs:complexType>
                  <xs:sequence>
                     <xs:element name="tDate" type="SDate" minOccurs="0"/>
                     <xs:element name="direction" type="Direction" minOccurs="0"/>
                     <xs:element name="mstAmount" type="MSTAmount" minOccurs="0"/>
                     <xs:element name="minDate" type="EDate" minOccurs="0"/>
                  </xs:sequence>
                  <xs:attributeGroup ref="RequiredAttrs"/>
               </xs:complexType>
                </xs:element>
...and so on.

验证代码:

代码语言:javascript
复制
class validate {
  def validateXML(xmlFilePath: String, xsdFilePath: String): Boolean = {
    try{
      val factory: SchemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)

      val schema: Schema = factory.newSchema(new File(xsdFilePath))

      val validator: Validator = schema.newValidator()
      validator.validate(new StreamSource(new File(xmlFilePath)))
      true
    } 
    catch {
      case NonFatal(error) => error.printStackTrace()
      false
    }
  }
}
EN

回答 1

Stack Overflow用户

发布于 2018-10-19 12:27:03

我找到了scalaxb的方法。将XSD转换为scala中的类的最佳库。它从XSD创建了3个文件:一个包含XSD中的特征和类的scala代码,scalaxb.scala(我仍在尝试找出原因)&一个相应的XML示例文件。就这样。一旦我获得了类格式的分离字段,就可以很容易地创建模式。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52882859

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档