Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 解组修改后的xml文件_Java_Xml Parsing_Unmarshalling - Fatal编程技术网

Java 解组修改后的xml文件

Java 解组修改后的xml文件,java,xml-parsing,unmarshalling,Java,Xml Parsing,Unmarshalling,在我的java项目中,我需要解组一个xml文件以获得一个对象列表 解组工作得很好,但在修改文件内容时会出现问题。事实上,在这个项目中,我们可以通过单击“addButon”向这个文件添加新内容,但是当我想再次对同一个文件进行解组时(修改后),我只得到旧的对象列表,没有最后的元素(我刚刚添加到这个文件中) 我已经验证了磁盘上的物理xml文件,并且找到了刚才添加的所有最后内容。我还注意到,获取最后一个对象列表的唯一方法是关闭appli并再次打开它,这是不合适的 下面是一段代码: //classe1.j

在我的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?

是的,真的,看起来您没有关闭/刷新outputStream对象。试试看,应该会有帮助。

不要使用原始类型。使用泛型:
List
listierator
TreeMap
(假设
pl.getCode()
int
类型)。非常感谢您的帮助,但该项目只使用java 1.4,因此我们不能使用泛型。当我使用它时,我得到了这样一个错误:“语法错误,参数化类型只有在源代码级别为1.5时才可用”谢谢Igor的帮助。我已经在finally子句中使用了fs.flush()和fs.close(),但它仍然不起作用。请问您还有其他想法吗?恐怕您提供的代码看起来没问题。。。我还需要看一点:什么是“app”变量,Referentiel.unmarshal方法内部发生了什么?谢谢你,Igor我发布了你问题的答案在第一个示例中,你将FileInputStream传递给Referentiel.unmarshal方法,但在第二个示例中,它将Unmarshaller作为参数。。。你是如何创造它的?对不起,我不知道答案,我只是想问一些问题,这些问题可能会引导我们找到解决方案。是的,你说得对,这是一个连续的方法调用。您可以想象Method1(Object1O1)->调用Method2(Object2O2)->调用最后一个方法unmarshal(我在这里发布)