在我的java项目中,我需要解组一个xml文件才能获取对象列表。解组修改后的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?
请勿使用原始类型。使用泛型:'列表','ListIterator '和'TreeMap '(假设'pl.getCode()'类型为'int')。 –
gparyani
2013-05-13 17:27:50
非常感谢您的帮助,但该项目仅使用java 1.4,因此我们无法使用泛型。当我使用它时,我得到这个错误:“语法错误,参数化类型仅在源级别为1.5时才可用” – Samounas 2013-05-14 08:40:49