Java中的VTD-XML - 在XMLModifier.insertAfterElement之后查找索引

时间:2023-02-07 16:38:06

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:

我已经开始在Java中使用VTD(我猜VTD-XML),而XPath读取它非常好。我现在遇到的问题是插入数据。让我们说我正在做以下事情:

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

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.

我所假设的是这是一次实时更新,将在VTDNav中反映出来。看起来我的理解是不正确的,因为简单地插入元素内容对nav没有任何作用(如果我输出VTDNav,它仍然包含我的原始xml)。我似乎能够处理新xml的唯一方法是从XMLModifier输出它。

modifier.outputAndReparse(); // Gives me a new VTDNav with the new content

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.

这里有什么我想念的吗?有更简单的方法吗?我希望能够插入新内容,然后立即获取新索引。我现有的代码(使用标准DOM类)有大量的插入和更新,我还需要知道文档中最后插入的元素的位置。每次都要输出AndReparse(),然后找到插入的元素(我甚至可能无法保证)似乎不是一个合理的解决方案。

2 个解决方案

#1


2  

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.

我认为答案是仔细规划您的修改和随后访问新内容。如果您插入新内容,并尝试立即访问新内容,insertAndParse()是要走的路。但正如你所看到的,由于重新分析,它相当缓慢。我的建议是你计划一次插入所有,然后只调用一次解析,这样会更有效率。

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不是试图成为DOM,它有自己的优点和缺点......这是其中一个弱点,但你可以解决它......当你尝试合并多个xml文件时,vtd-xml肯定会闪耀....

Also if you tag this question with vtd-xml, i will be able to find it much easier.

此外,如果你用vtd-xml标记这个问题,我将能够更容易地找到它。

#2


0  

modifier.remove():

in the text file stored no.of xpaths, val so for each xpath generate one new output xxx.xml file. when u use xml modifier previous data along with current data it will write in a xxx.xml file so for elimination of previous data and only for current xpath data changes write into the new xxx.xml file so for that use the modifier.remove();

在文本文件中存储no.of xpaths,因此每个xpath生成一个新的输出xxx.xml文件。当您使用xml修饰符以前的数据以及当前数据时,它将写入xxx.xml文件,以便消除以前的数据,并且仅针对当前的xpath数据更改写入新的xxx.xml文件,以便使用modifier.remove( );

xm2.output( new FileOutputStream("/home/cupola-hadoop-project/TotalEnvironment/document/link/"+j+"101new.xml"));
xm2.remove();

and rotate the loop desired times.

并旋转所需的循环次数。

#1


2  

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.

我认为答案是仔细规划您的修改和随后访问新内容。如果您插入新内容,并尝试立即访问新内容,insertAndParse()是要走的路。但正如你所看到的,由于重新分析,它相当缓慢。我的建议是你计划一次插入所有,然后只调用一次解析,这样会更有效率。

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不是试图成为DOM,它有自己的优点和缺点......这是其中一个弱点,但你可以解决它......当你尝试合并多个xml文件时,vtd-xml肯定会闪耀....

Also if you tag this question with vtd-xml, i will be able to find it much easier.

此外,如果你用vtd-xml标记这个问题,我将能够更容易地找到它。

#2


0  

modifier.remove():

in the text file stored no.of xpaths, val so for each xpath generate one new output xxx.xml file. when u use xml modifier previous data along with current data it will write in a xxx.xml file so for elimination of previous data and only for current xpath data changes write into the new xxx.xml file so for that use the modifier.remove();

在文本文件中存储no.of xpaths,因此每个xpath生成一个新的输出xxx.xml文件。当您使用xml修饰符以前的数据以及当前数据时,它将写入xxx.xml文件,以便消除以前的数据,并且仅针对当前的xpath数据更改写入新的xxx.xml文件,以便使用modifier.remove( );

xm2.output( new FileOutputStream("/home/cupola-hadoop-project/TotalEnvironment/document/link/"+j+"101new.xml"));
xm2.remove();

and rotate the loop desired times.

并旋转所需的循环次数。