我有一个xml ans我想让它成为对象,我正在使用xsteam,我在我的类路径中添加了xstream jar ..
下面的是我的xml ...
<Eurexflows xmlns:eur="http://www.eurexchange.com/EurexIRSFullInventoryReport" xmlns:fpml="http://www.fpml.org/FpML-5/confirmation">
<EurexMessageObject>
<CCPTradeId>109599</CCPTradeId>
<novDateTime>2012-02-15 10:59:00.0</novDateTime>
</EurexMessageObject>
<EurexMessageObject>
<CCPTradeId>122270</CCPTradeId>
<novDateTime>2012-06-29 18:59:00.0</novDateTime>
</EurexMessageObject>
</Eurexflows>
下面是我的pojo ...
public class EurexMessageObject {
private Long CCPTradeId;
private String migratedDate;
public Long getCCPTradeId() {
return CCPTradeId;
}
public void setCCPTradeId(Long cCPTradeId) {
CCPTradeId = cCPTradeId;
}
public String getMigratedDate() {
return migratedDate;
}
public void setMigratedDate(String migratedDate) {
this.migratedDate = migratedDate;
}
}
在我的主要课程中,我用这种方式编码..
String xmlInputtra="C:\\Rahul\\InputXml\\Xmloutput.xml";
try
{
// get XStream instance and set required aliases
XStream xstream = new XStream();
xstream.alias("EurexMessageObject", com.rbos.gdspc.eurex.EurexMessageObject.class);
// prepare cash flow message from xslt output
EurexMessageObject eurexflowMsg = (EurexMessageObject) xstream.fromXML(xmlInputtra);
System.out.println(eurexflowMsg.toString());
}catch(Exception e)
{
e.printStackTrace();
}
现在在调试时我得到了以下异常。请告诉我如何克服这个
com.thoughtworks.xstream.io.StreamException: : only whitespace content allowed before start tag and not C (position: START_DOCUMENT seen C... @1:1)
答案 0 :(得分:2)
嗯,这里忽略的是你如何在XML文件中阅读。你正在使用 fromXML 这个期望实际 XML的方法输入而不是文件名。所以当它解析你的xml(它是“Xmloutput.xml”而不是实际的xml)
时我建议你使用 FileReader / BufferedReader 来获取XML的内容。这样的事情应该有效:
XStream instream = new XStream();
BufferedReader br = new BufferedReader(new FileReader("Xmloutput.xml"));
StringBuffer buff = new StringBuffer();
String line;
while((line = br.readLine()) != null){
buff.append(line);
}
EurexMessageObject eurexflowMsg = (EurexMessageObject)instream.fromXML(buff.toString());
我希望它会对你有所帮助,最好的问候。
答案 1 :(得分:0)
这里是XML文件的路径:
String xmlInputtra="C:\\Rahul\\InputXml\\Xmloutput.xml";
被视为XML内容, 所以你需要传递为String,你可以读取文件并传递给构造函数。