简体   繁体   English

无法使用Java和JAXB解组奇怪的XML格式

[英]Unable to unmarshal strange XML format using Java and JAXB

I need to retrieve financial data using the Open Financial Exchange (OFX) protocol. 我需要使用开放财务交换(OFX)协议检索财务数据。 In order to do this, I am using JAXB to marshal an object tree into an XML string that specifies data request parameters, and then I am sending this XML string to a bank's server. 为此,我使用JAXB将对象树编组为指定数据请求参数的XML字符串,然后将该XML字符串发送到银行的服务器。 The bank then responds with an XML string containing the requested data, which I unmarshal into an object tree using JAXB. 然后,银行用包含所请求数据的XML字符串进行响应,我使用JAXB将其解组到对象树中。 For the first couple of banks I tried, I received the data back in well-formed XML that conformed to the published OFX schema, and I was able to unmarshal it easily using JAXB. 在我尝试使用的前两对银行中,我以符合已发布的OFX架构的格式正确的XML接收了数据,并且可以使用JAXB轻松将其解组。

However, when I requested data from Citigroup, they sent me back the following: 但是,当我向花旗集团索要数据时,他们向我发送了以下信息:

OFXHEADER:100
DATA:OFXSGML
VERSION:102
SECURITY:NONE
ENCODING:USASCII
CHARSET:1252
COMPRESSION:NONE
OLDFILEUID:NONE
NEWFILEUID:NONE

<OFX>
<SIGNONMSGSRSV1>
<SONRS>
<STATUS>
<CODE>0
<SEVERITY>INFO
</STATUS>
<DTSERVER>20150513180826.000
<LANGUAGE>ENG
<FI>
<ORG>Citigroup
<FID>24909
</FI>
</SONRS>
</SIGNONMSGSRSV1>
</OFX>

Note that this is an abbreviated form of the actual output, but it is enough to illustrate the problem. 注意,这是实际输出的缩写形式,但是足以说明问题。 The problem is that I cannot figure out how to use JAXB to unmarshal this content. 问题是我无法弄清楚如何使用JAXB解组此内容。 It is not well-formed XML because (1) it doesn't have an XML header, (2) the custom processing instructions (the first nine lines above) are not enclosed in <?...?> tags, and (3) most importantly, the simpleTypes have only opening tags but no closing tags. 它不是格式正确的XML,因为(1)它没有XML标头;(2)自定义处理指令(上面的前九行)未包含在<?...?>标记中;以及(3 )最重要的是,simpleTypes仅具有开始标记,而没有结束标记。

I have searched all over for an answer to this and found a similar XML-ish format in a couple of places, and one of those places indicated that this may even be a valid format for sending XML over the web. 我一直在寻找答案,并在几个地方找到了类似XML的格式,其中一个地方表明这甚至可能是通过Web发送XML的有效格式。 But I haven't found any information that can help me unmarshal it or parse it. 但是我没有找到任何可以帮助我解组或解析它的信息。

Does anyone have any suggestions? 有没有人有什么建议? I am usually pretty resourceful when it comes to these types of problems (hence why this is my first question on here), but this one has me stumped. 当涉及到这类问题时,我通常非常机智(因此,为什么这是我在此提出的第一个问题),但这使我感到困惑。 Thanks in advance for any help you can provide. 在此先感谢您提供的任何帮助。

Your basic problem is that the input you show here is not XML, it's SGML (see DATA:OFXSGML ). 您的基本问题是,此处显示的输入不是XML,而是SGML(请参阅DATA:OFXSGML )。 You will have to preprocess it to make it acceptable to an XML parser. 您必须对其进行预处理,以使其可以被XML解析器接受。 The kind of preprocessing you have to do will be application specific, as there's no general mechanism to deal well with that. 您必须执行的预处理类型取决于特定的应用程序,因为没有通用的机制可以很好地处理这种情况。 If you have the SGML DTD, you might be able to get a product such as omnimark to "mostly" fix it up. 如果您拥有SGML DTD,则也许能够获得诸如omn​​imark之类的产品来“大部分”对其进行修复。

Well , maybe you need to handle this bank services in some other manner, for example when you receive data from this bank maybe read the Stream and maybe try to undetify the beggining of tag and then the end of (read line by line link )the rest of the stream ..free will . 好吧,也许您需要以其他方式处理该银行服务,例如,当您从该银行接收数据时,可能会读取Stream并尝试取消识别标签的开头,然后(逐行读取) 链接的结尾其余的..自由意志。 After that the string that remains is the XML that you need , so pass it through your already implemented JAXB code. 之后,剩下的字符串就是您需要的XML,因此将其传递给您已经实现的JAXB代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM