|
From: <al...@sa...> - 2014-05-28 22:33:06
|
This code will find and delete the notes/elements you want.
The comments will provide any details about usage:
byte[] tableData = someMethodThatGetsXMLData();
byte[] returnData = null;
int numRowsDeleted = 0;
try {
VTDGen vtdGen = new VTDGen();
vtdGen.setDoc(tableData);
vtdGen.parse(false);
VTDNav nav = vtdGen.getNav();
// this modifier will actually make the deletes, and contain the modified XML data
XMLModifier modifier = new XMLModifier();
modifier.bind(nav);
AutoPilot autoPilot = new AutoPilot();
// use a query that will select all the nodes/elements you want to delete
String xpathQuery = "//note-id[text()='1']";
autoPilot.selectXPath(xpathQuery);
autoPilot.bind(nav);
int cnt;
while((cnt=autoPilot.evalXPath()) != -1) {
modifier.remove();
numRowsDeleted++;
}
if (numRowsDeleted > 0) {
// The XMLModifier will delete the nodes, but they will only be deleted in
// the XML data stored in the XMLModifier.
// extract the modified xml with the deleted nodes from the XMLModifier
returnData = modifier.outputAndReparse().getXML().getBytes();
}
else {
returnData = tableData;
}
catch (Exception ex) {
_logger.logError("Error deleting elements: ", ex);
}
// return the modified xml, if there was any
return returnData;
Fire back if you have any questions or comments.
AlG
---------- Original Message -----------
From: W K <wka...@ho...>
To: "vtd...@li..." <vtd...@li...>, "jz...@xi..." <jz...@xi...>
Sent: Wed, 28 May 2014 01:45:59 -0400
Subject: Re: [Vtd-xml-users] XMLModifier remove all elements of type 'x'
> Corrected inconsistencies in snippets below from previous email...
>
> I have the following xml snippet:
>
> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
> <notes version="1.0">
> <note-count>5</note-count>
> <refs>
> <b id="1">
> <c id="1">
> <v id="2">
> <note-id>1</note-id>
> <note-id>2</note-id>
> <note-id>5</note-id>
> </v>
> </c>
> <c id="4">
> <v id="2">
> <note-id>1</note-id>
> <note-id>2</note-id>
> <note-id>3</note-id>
> </v>
> </c>
> </b>
> <b id="22">
> <c id="1">
> <v id="2">
> <note-id>1</note-id>
> <note-id>4</note-id>
> <note-id>5</note-id>
> </v>
> </c>
> </b>
> </refs>
> <notes>
>
> I need to remove all elements: <note-id>1</note-id>
>
> I have the following code snippet to remove all of the elements:
> XM.bind(VN);
> AutoPilot ap = new AutoPilot( VN );
>
> // find all note-id elements with a value of '1'
> String xpath = "//note-id[text()='1']";
> ap.selectXPath( xpath );
>
> int i = -1;
> while ( ( i = ap.evalXPath() ) != -1 ) {
> log.debug( "element/value: " + VN.toNormalizedString( VN.getCurrentIndex() ) + "/" + VN.toNormalizedString( VN.getText() ));
> XM.remove();
> }// while
>
> If I just print out the xpath results, the element names and values are correct. However, when trying to remove each value, I get the following error:
>
> java.lang.ArrayIndexOutOfBoundsException: -1
> at com.ximpleware.FastLongBuffer.longAt(FastLongBuffer.java:309)
> at com.ximpleware.VTDNav.getTokenDepth(VTDNav.java:1327)
> at com.ximpleware.VTDNav.getElementFragment(VTDNav.java:1079)
> at com.ximpleware.XMLModifier.remove(XMLModifier.java:210)
> at com.notes.android.util.NotesXmlUtil.deleteNoteIds(NotesXmlUtil.java:633)
> at com.notes.android.test.NotesXmlUtilTest.deleteNoteById(NotesXmlUtilTest.java:146)
> at com.notes.android.test.NotesXmlUtilTest.test(NotesXmlUtilTest.java:49)
>
> What is the best practice for removing these elements in bulk?
>
> Thanks for any help you can provide.
------- End of Original Message -------
|