Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VTD-XML in Java - Find index after XMLModifier.insertAfterElement

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

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

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.

like image 440
jpcamara Avatar asked Nov 04 '22 17:11

jpcamara


1 Answers

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.

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

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

like image 192
vtd-xml-author Avatar answered Nov 10 '22 15:11

vtd-xml-author