如何保存常量更新,最后将输出保存为XMLModifier文件 [英] How can you save the constant update and the finally save the output as a file with XMLModifier

查看:132
本文介绍了如何保存常量更新,最后将输出保存为XMLModifier文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有不同的方法尝试根据路径更新/删除元素/令牌。

I have different methods trying to update/remove elements/tokens based on the path.

现在,每个方法在完成编辑后立即保存为文件,

Right now, each method, after it done edited, it straight away save as a file,

VTDGen vg = new VTDGen();
vg.parseFile("input.xml", false);
vn.getNav();
AutoPilot ap = new AutoPilot(vn);  
XMLModifier xm; 

public void updateToken(String path)
{
    xm = new XMLModifier(vn);
    ap.selectXPath(path);
    ......
    xm.updateToken(i,"hello");
    ......
    xm.output("output.xml");
}

如果我继续再次调用此方法,它会保存,但它使用新的更改覆盖文件,我不希望这样,所以我该怎么做?

And if I continue to call this method again, it does save, but it overwrites the file with the new changes, and I do not want that, so how can I do it?

我也尝试过使用

xm.outputAndReparse();

我将更新保存回 NTDNav vn ,并重用其余方法,但它会破坏方法移动到节点的其余部分(使用AutoPilot)。

where I save the update back to NTDNav vn, and reuse for the remaining methods but it corrupts the rest of the method's movement to the node (with AutoPilot).


我想要什么:调用方法根据路径更新令牌,保存更新,并使用其他路径再次调用该方法,最后在完成更新后,保存到文件中。

What I want: Call the method to update the token based on the path, save the update, and call the method again with another path, and finally after finishing the update, save into the file.

如果我有任何不妥,请告诉我,因为我还是VTD-XML的新手。谢谢。

Please do let me know if I am in any wrong, as I am still new to VTD-XML. Thanks.

搞定了,这就是我所做的。

Got it working and this is what I have done.

VTDGen vg = new VTDGen();
vg.parseFile("input.xml", false);
vn.getNav();
AutoPilot ap = new AutoPilot(vn);
XMLModifier xm = new XMLModifier(vn);

public void output()
{
   ....       
   xm.output("output.xml");
   ....
}

public void updateToken(String path) 
{
    ap.selectPath(path);
    .........
    xm.updateToken(i,"hello");
}


推荐答案

XMLModifier是方法的集合实现VTD-XML独特的增量更新功能。

XMLModifier is a collection of methods that implements VTD-XML's unique incremental update capability.

要更新XML的内容,首先必须将VTDNav的实例附加到XMLModifier对象。您可以对XML文档应用三种类型的操作:删除,插入和更新(同时删除和插入)。

To update the content of XML, you first have to attach an instance of VTDNav to an XMLModifier object. There are three types of operations you can apply to the XML document: delete, insert and update (which is a simultaneous deletion and insertion).

XMLModier最重要的概念是

The most important concepts of XMLModier are


  1. 每个偏移值一次操作:在同一偏移处的两次重复操作将导致异常。

  2. 不会立即更改内容更改:它仅标记更改并在XMLModifier内部保留其记录。输出XML仅通过调用output()方法生成。

  3. 原始输入XML文档(包含在VTDNav对象中)保持不变。

基本上,您可以根据需要标记任意数量的更改,但是您需要明确要求将这些更改推送到输出。输入XML没有变化。

So basically, you can mark as many changes as you want, but you need to explicitly ask those changes to be pushed through to output. There is no change to input XML.

所以我认为你需要将output()方法移出updateToken方法,而是在所有updateToken调用之后在更高级别的过程中调用它。换句话说,你的updateToken()应该只标记更改,而不是直接输出到输出。

So I think you need to move the output() method out of the updateToken method, and instead, invoke it in an higher level procedure after all updateToken invocations. In other words, your updateToken() should only mark the change, and not push thru to the output.

另一个相关点:你可以通过调用XMLModifier来删除所有标记reset()方法。这将帮助您重用XMLModifier将一组新操作应用于输入文档。

Another related point: you can remove all the marks by invoking XMLModifier's reset() method. That will help you reuse XMLModifier to apply a fresh set of operations to the input document.

至于outputAndParse()的目的,它基本上是为了减少编码工作量输出一个新文档,然后立即解析它。但是这种方法有一个缺点,因为它是一种相对缓慢/沉重的调用方法。所以考虑小心翼翼地使用它。如果您尝试对文档内容应用多批更改,请在调用outputAndParse()之前立即执行此操作。

As to the purpose of outputAndParse(), it is basically to reduce the coding effort of outputting a new document and then immediately parsing it. But there is a down side to this method as it is relatively a slow/heavy method to call. So consider using it gingerly. If you are trying to apply multiple batches of changes to the document content, do so all at once before calling outputAndParse().

这篇关于如何保存常量更新,最后将输出保存为XMLModifier文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆