无法找到元素'OFX'的声明

时间:2015-03-06 08:27:03

标签: java xsd xml-validation

我已经检查了类似的问题,但没有一个能解决我的问题。 我正在使用OFX架构验证我的xml字符串。 这是架构 OFX.xsd

我的xml字符串是

<?xml version="1.0"?>
<OFX>
<SIGNONMSGSRSV1>
    <SONRS>
        <STATUS>
            <CODE>0</CODE>
            <SEVERITY>INFO</SEVERITY>
        </STATUS>
        <DTSERVER>20120716233626.570[-7:PDT]</DTSERVER>
        <LANGUAGE>ENG</LANGUAGE>
        <FI>
            <ORG>Symitar</ORG>
            <FID>01182</FID>
        </FI>
        <INTU.BID>01182</INTU.BID>
        <INTU.USERID>66983</INTU.USERID>
    </SONRS>
</SIGNONMSGSRSV1>
<BANKMSGSRSV1>
    <STMTTRNRS>
        <TRNUID>0</TRNUID>
        <STATUS>
            <CODE>0</CODE>
            <SEVERITY>INFO</SEVERITY>
        </STATUS>
        <STMTRS>
            <CURDEF>USD</CURDEF>
            <BANKACCTFROM>
                <BANKID>
                </BANKID>
                <ACCTID>66983-S80</ACCTID>
                <ACCTTYPE>CHECKING</ACCTTYPE>
            </BANKACCTFROM>
            <BANKTRANLIST>
                <DTSTART>20120501</DTSTART>
                <DTEND>20120716</DTEND>
                <STMTTRN>
                    <TRNTYPE>FEE</TRNTYPE>
                    <DTPOSTED>20120713135400</DTPOSTED>
                    <TRNAMT>-25.00</TRNAMT>
                    <FITID>30403620120713WF</FITID>
                    <NAME>Account Transaction</NAME>
                    <MEMO>Withdrawal Fee</MEMO>
                </STMTTRN>
              </BANKTRANLIST>
            <LEDGERBAL>
                <BALAMT>-254.64</BALAMT>
                <DTASOF>20120716233626</DTASOF>
            </LEDGERBAL>
            <AVAILBAL>
                <BALAMT>-254.64</BALAMT>
                <DTASOF>20120716233626</DTASOF>
            </AVAILBAL>
        </STMTRS>
    </STMTTRNRS>
</BANKMSGSRSV1>
</OFX>

我用来验证xml的Java代码是

InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("OFX.xsd");
        Source ofxsource = new StreamSource(new StringReader(output));
        SchemaFactory schemaFactory = SchemaFactory
            .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = schemaFactory.newSchema(new StreamSource(in));
        Validator validator = schema.newValidator();
        try {
          validator.validate(ofxsource);
          System.out.println("Source is valid");
        } catch (SAXException e) {
          System.out.println("Source is NOT valid");
          System.out.println("Reason: " + e.getLocalizedMessage());
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("IOException "+ e.getLocalizedMessage());
        }

我没有收到任何错误,但它没有验证xml,我得到的消息是&#34;找不到元素的声明&#39; OFX&#39;&#34;。 如果有人解决了有关OFX验证的问题,那么请分享解决方案。

1 个答案:

答案 0 :(得分:0)

您尚未在OLX.xsd中定义根元素

<xs:complexType name="OLX" >修改为<xs:complexType>并使用元素标记

进行包装
<xs:element name="OFX">
    <xs:complexType>
        <xs:choice>
            <xs:element name="SIGNONMSGSRQV1" type="SIGNONMSGSRQV1" minOccurs="0" maxOccurs="1"/>
            <xs:element name="SIGNONMSGSRSV1" type="SIGNONMSGSRSV1" minOccurs="0" maxOccurs="1"/>
            <xs:element name="SIGNUPMSGSRQV1" type="SIGNUPMSGSRQV1" minOccurs="0" maxOccurs="1"/>
            <xs:element name="SIGNUPMSGSRSV1" type="SIGNUPMSGSRSV1" minOccurs="0" maxOccurs="1"/>
            <xs:element name="BANKMSGSRQV1" type="BANKMSGSRQV1" minOccurs="0" maxOccurs="1"/>
            <xs:element name="BANKMSGSRSV1" type="BANKMSGSRSV1" minOccurs="0" maxOccurs="1"/>
            <xs:element name="CREDITCARDMSGSRQV1" type="CREDITCARDMSGSRQV1" minOccurs="0" maxOccurs="1"/>
            <xs:element name="CREDITCARDMSGSRSV1" type="CREDITCARDMSGSRSV1" minOccurs="0" maxOccurs="1"/>
            <xs:element name="LOANMSGSRQV1" type="LOANMSGSRQV1" minOccurs="0" maxOccurs="1"/>
            <xs:element name="LOANMSGSRSV1" type="LOANMSGSRSV1" minOccurs="0" maxOccurs="1"/>
            <xs:element name="INVSTMTMSGSRQV1" type="INVSTMTMSGSRQV1" minOccurs="0" maxOccurs="1"/>
            <xs:element name="INVSTMTMSGSRSV1" type="INVSTMTMSGSRSV1" minOccurs="0" maxOccurs="1"/>
            <xs:element name="SECLISTMSGSRSV1" type="SECLISTMSGSRSV1" minOccurs="0" maxOccurs="1"/>
        </xs:choice>
    </xs:complexType>
</xs:element>