问题描述
我已经开始在Java中使用VTD(我猜VTD-XML),而对于XPath读取它是非常好的。我现在正在遇到的一个问题是插入数据。让我说我在做以下事情:
I've started using VTD (I guess VTD-XML) in Java, and for XPath reads it's excellent. Where i'm hitting an issue now is with inserting data. Lets say I am doing the following:
VTDNav nav = preExistingGen.getNav();
AutoPilot pilot = new AutoPilot(nav);
pilot.selectXPath("/Something/SomethingElse");
if (pilot.evalXPath() != -1) {
XMLModifier modifier = new XMLModifier(nav);
modifier.insertAfterElement("<some>content</some>");
}
我以为这是一个实时更新,反映在VTDNav。看起来我的理解是不正确的,因为简单地插入元素内容对nav无效(如果我输出VTDNav,它仍然包含我的原始xml)。我可以通过从XMLModifier输出来处理新的xml的唯一方法。
What I had assumed was this was a real-time update, which would be reflected in the VTDNav. It looks like my understanding is incorrect, since simply inserting the element content does nothing to the nav (if I output the VTDNav, it still contains my original xml). The only way I can seem to get a handle on the new xml, is by outputting it from the XMLModifier.
modifier.outputAndReparse(); // Gives me a new VTDNav with the new content
有没有什么我在这里?有更简单的方法吗?我想要插入新的内容,然后立即得到新的索引。我现有的代码(使用标准DOM类)有大量的插入和更新,我还需要知道文档中最后插入元素的位置。必须每次输出AndReparse(),然后找到插入的元素(我可能甚至不能保证)似乎不是一个合理的解决方案。
Is there something i'm missing here? Is there an easier way of doing this? I wanted to be able to insert the new content, and then immediately get the new index. My existing code (using the standard DOM classes) has a ton of inserts and updates, and I also need to know where the last inserted element existed in the document. Having to outputAndReparse() everytime and then find the inserted element (which I may not even be able to guarantee) doesn't seem like a plausible solution.
推荐答案
我认为答案是计划您的修改和后续访问新的内容仔细。如果插入新的内容,并尝试立即访问新的内容,那么insertAndParse()就是这样。但是,正如你所看到的那样,因为重新组织而相当缓慢。我的建议是,你计划尽可能多地插入所有的一次,然后再打电话一次,这样会更有效率。
I think the answer is to plan your modification and subsequent access to new content carefully. If you insert the new content, and try to access the new content immediately afterwards, insertAndParse() is the way to go. But as you can see, it is rather slow because of the reparsing. My suggestion is that you plan as much as insert all at once, then call reparsing just once, it will be a lot more efficient this way.
精神是VTD -XML不是想成为DOM,它有自己的优点和缺点...这是一个弱点,但你可以解决它...
当你尝试合并多个xml文件时, vtd-xml一定会发光....
The spirit is that VTD-XML is not trying to be DOM, it has its own strengths and weaknesses... and this is one of the weakness, but you can work around it ...And when you try to merge multiple xml files, vtd-xml will certainly shine....
此外,如果您使用vtd-xml标记此问题,我将能够轻松找到它。
Also if you tag this question with vtd-xml, i will be able to find it much easier.
这篇关于Java中的VTD-XML - 在XMLModifier.insertAfterElement之后查找索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!