在我的 java 项目中,我需要解组一个 xml 文件以获取对象列表。
解组工作非常好,但是当文件的内容被修改时,问题就会出现。事实上,在这个项目中,我们可以通过单击“addButon”向该文件添加新内容,但是当我想再次解组同一文件时(修改后)我只得到没有最后一个元素的旧对象列表(我刚刚添加到此文件中)。
我已经验证了磁盘上的物理 xml 文件,并找到了我刚刚添加的所有内容。我还注意到获取最后一个对象列表的唯一方法是关闭应用程序并再次重新打开它,这是不合适的。
这是一段代码:
//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?