【问题标题】:Extracting schema from XSD File in scala: scalaxb从 scala 中的 XSD 文件中提取模式:scalaxb
【发布时间】:2018-10-18 21:28:49
【问题描述】:

我有一个 XSD 文件和 XML,这是我不知道的架构。您可以说 XSD 是当前 XML 的架构,但我不太了解如何继续。

在我的搜索过程中,我偶然发现了为满足此要求而规定的 scalaxb 包。有人可以帮助我实现这一目标吗?我以前没有做过这种 xsd 处理,对这个很陌生。虽然我得到了一个小代码来从 XSD 验证 XML,但这似乎没问题。但是提取是我现在遇到的问题。

由于我对 XSD 的了解有限,我无法区分哪些元素被视为架构字段,哪些不应该被视为架构字段。感谢任何帮助。

XSD 文件片段:

   <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.

验证代码:

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
    }
  }
}

【问题讨论】:

    标签: xml scala xsd scalaxb


    【解决方案1】:

    我找到了scalaxb。将 XSD 转换为 scala 中的类的最佳库。它从 XSD 创建 3 个文件:具有 XSD 特征和类的 scala 代码、scalaxb.scala(我仍在试图找出原因)和相应的 XML 示例文件。而已。一旦我得到了类格式的隔离字段,以后就很容易创建模式了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-09
      • 1970-01-01
      • 2021-06-16
      • 2018-12-10
      • 2013-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多