当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Unmarshal a modified xml file

问题描述:

In my java project I need to unmarshal an xml file in order to get a list of objects.

The unmarshalling works very fine but the problem is encontourned when the content of the file is modified. In fact in this project we can add new contents to this file by clicking on "addButon" but when i want to unmarshal another time the same file (after modification) i get only the old list of objects without the last elements (I have just added to this file).

I already verified the physical xml file on the disk and I found all the last content I just added. Also I noticed that The only way to get the last list of objects is to close the appli and to reopen it again, which is unsuitable.

Here is a piece of the code:

//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;

...


Thank you so much Igor for your response

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?

网友答案:

Yep, really, looks like you don't close/flush the outputStream object. Try that, it should help.

分享给朋友:
您可能感兴趣的文章:
随机阅读: