在我的java项目中,我需要解组一个xml文件才能获得一个对象列表。
解组工作非常正常,但在修改文件内容时会引发问题。实际上在这个项目中我们可以通过点击“addButon”向这个文件中添加新的内容,但是当我想要另一个时间解组同一个文件(修改后)后,我只得到没有最后一个元素的旧对象列表(我刚刚添加到此文件)。
我已经验证了磁盘上的物理xml文件,我找到了刚刚添加的所有内容。另外我注意到获取最后一个对象列表的唯一方法是关闭appli并再次重新打开它,这是不合适的。
以下是一段代码:
//classe1.java
File iniFile = new File(proprietesPerle.getRepRef() + "referential.xml");
FileInputStream fs = = new FileInputStream(iniFile);
ref = Referentiel.unmarshal(fs);
TreeMap map = ReferentielUtil.getApplication( ref );
...
//classe2.java
TreeMap map = new TreeMap();
List listPL = app.getPl(); //this list is unmarshalled from the xml file
ListIterator itpl = listPL.listIterator();
while (itpl.hasNext())
{
Pl pl = (Pl)itpl.next();
map.put(pl.getCode(), pl);
}
return map;
...
非常感谢伊戈尔的回复
In fact we select an app from a combobox, it's type is "Application" and this variable is stocked in a session. we use it like this:
session.setAttribute("selectedApplication",selectedAppli);
TreeMap mapp = ReferentielUtil.getPl(selectedAppli.getApp());
//Class ReferentielUtil.java
public static TreeMap getPl(Application app)
{
logger.debug("getPl");
logger.debug("passe 10");
TreeMap map = new TreeMap();
List listPL = app.getPl();
ListIterator itpl = listPL.listIterator();
while (itpl.hasNext())
{
Pl pl = (Pl)itpl.next();
map.put(pl.getCode(), pl);
}
return map;
}
inside Referentiel.unmarshal, you will find a code like this:
public void unmarshal(Unmarshaller u)
throws UnmarshalException
{
XMLScanner xs = u.scanner();
xs.takeStart("application");
while (xs.atAttribute()) {
String an = xs.takeAttributeName();
throw new InvalidAttributeException(an);
}
if (xs.atStart("code")) {
xs.takeStart("code");
String s;
if (xs.atChars(XMLScanner.WS_COLLAPSE)) {
s = xs.takeChars(XMLScanner.WS_COLLAPSE);
} else {
s = "";
}
try {
_Code = String.valueOf(s);
} catch (Exception x) {
throw new ConversionException("code", x);
}
xs.takeEnd("code");
}
if (xs.atStart("libelle")) {
xs.takeStart("libelle");
String s;
if (xs.atChars(XMLScanner.WS_COLLAPSE)) {
s = xs.takeChars(XMLScanner.WS_COLLAPSE);
} else {
s = "";
}
try {
_Libelle = String.valueOf(s);
} catch (Exception x) {
throw new ConversionException("libelle", x);
}
xs.takeEnd("libelle");
}
_TypeApplication = ((TypeApplication) u.unmarshal());
if (xs.atStart("entite")) {
xs.takeStart("entite");
String s;
if (xs.atChars(XMLScanner.WS_COLLAPSE)) {
s = xs.takeChars(XMLScanner.WS_COLLAPSE);
} else {
s = "";
}
try {
_Entite = String.valueOf(s);
} catch (Exception x) {
throw new ConversionException("entite", x);
}
xs.takeEnd("entite");
}
{
List l = PredicatedLists.create(this, pred_Pl, new ArrayList());
while (xs.atStart("pl")) {
l.add(((Pl) u.unmarshal())); // the problem is here : unmarshal() dont gives the last element --------------
}
_Pl = PredicatedLists.createInvalidating(this, pred_Pl, l);
}
xs.takeEnd("application");
}
I noticed that in (while loop) we dont get the last element Pl which we want to add to the list "l"
Please do you have any idea?
答案 0 :(得分:0)
是的,看起来好像你没有关闭/刷新outputStream对象。试试看,它应该有所帮助。