Reputation: 141
I processing an xml message in Java and I need to remove the parents from a child node based on the child's attributes.
<xml>
<A>
<B>
<C>
<E>11</E>
<F>12</F>
</C>
</B>
<B>
<C>
<E>13</E>
<F>14</F>
</C>
</B>
</A>
For example, how to remove Entire B Node if E=13. This would be trivial task with some in-memory approach such as DOM but because of performance issues I need to be using StAX which parses the xml message top bottom. How can I accomplish this using StAX? Thank you so much in advance.
Upvotes: 1
Views: 1548
Reputation: 5558
I see two options:
You have enough memory to store one entire <B>
;
Basically just store the fragment in memory until you have the information about the <E>
and write it into the output (or don`t)
You dont have enough memory but can stream the xml twice. First pass: remember which <B>
to keep an which dont (by occurence in the xml, e.g. keep first, skip second, keep third aso. A bitset would be a good datastructure). Second pass: keep/skip according the remembered values from the bitset.
Upvotes: 0
Reputation: 99993
StaX, as you have observed, processes events strictly in order. If you want to delete a subtree, you need to write your own code to buffer enough events for that purpose, and you need to have enough memory to hold that buffer. Nothing in the StaX API will help (or hinder) you in this task.
Upvotes: 0
Reputation: 3377
Below is the code of removing B node when C/E is 13. It is done in vtd-xml and xpath. Performance wise VTD-XML is way better than DOM. This code will process your huge xml files with ease. Read this academic paper if you want to know more.
http://sdiwc.net/digital-library/request.php?article=0d947fb50e2f0160a75ac9f6bbf0818a
import com.ximpleware.*;
public class removeParent {
public static void main(String[] s) throws VTDException,java.io.IOException{
VTDGen vg = new VTDGen();
if (vg.parseFile("d:\\xml\\remove.xml",false)){
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
XMLModifier xm = new XMLModifier(vn);
ap.selectXPath("/xml/A/B[C/E='13']");
int i=0;
while((i=ap.evalXPath())!=-1){
xm.remove();
//System.out.println("ok");
}
xm.output("d:\\xml\\updated.xml");
}
}
}
Upvotes: 1