我有一个 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
}
}
}