在我的搜索过程中,我偶然发现了为实现此要求而指定的scalaxb包。但是提取是我现在卡住的东西。
由于我对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
}
}
}发布于 2018-10-19 12:27:03
我找到了scalaxb的方法。将XSD转换为scala中的类的最佳库。它从XSD创建了3个文件:一个包含XSD中的特征和类的scala代码,scalaxb.scala(我仍在尝试找出原因)&一个相应的XML示例文件。就这样。一旦我获得了类格式的分离字段,就可以很容易地创建模式。
https://stackoverflow.com/questions/52882859
复制相似问题