How can you save the constant update and the finally save the output as a file with XMLModifier

∥☆過路亽.° 提交于 2020-01-14 03:36:09

问题


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?

I have also tried using

xm.outputAndReparse();

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.

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");
}

回答1:


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

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).

The most important concepts of XMLModier are

  1. One operation per offset value: two repeating operations at the same offset will cause exception.
  2. No content change is honored instantly: it merely marks the change and keeps a record of it internally within XMLModifier. The output XML is generated only by calling output() method.
  3. The original input XML document, as contained within VTDNav object, remains untouched.

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.

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.

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.

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().



来源:https://stackoverflow.com/questions/34326164/how-can-you-save-the-constant-update-and-the-finally-save-the-output-as-a-file-w

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!