2

I am trying to remove some nodes using XMLModifier using the following code. I am ending with white spaces in between. How can I get rid of this?

import java.nio.file.Files;
import java.nio.file.Paths;

import com.ximpleware.AutoPilot;
import com.ximpleware.VTDGen;
import com.ximpleware.VTDNav;
import com.ximpleware.XMLModifier;

public class VTDWhiteSpaceIssue {
public static void main(String[] args) throws Exception {

    byte[] encoded = Files.readAllBytes(Paths.get("Sample.xml"));
    String query = new String(encoded, "UTF-8");

    VTDGen vtdGenDoc = new VTDGen();
    vtdGenDoc.setDoc(query.getBytes());
    vtdGenDoc.parse(false);
    VTDNav vtdNav = vtdGenDoc.getNav();
    AutoPilot autoPilot = new AutoPilot(vtdNav);
    XMLModifier xmlModifier = new XMLModifier(vtdNav);

    autoPilot.selectXPath("//product/catalog_item");
    if (autoPilot.evalXPath() != -1 && vtdNav.toElement(VTDNav.FIRST_CHILD)) {
        do {
            String nodeName = vtdNav.toRawString(vtdNav.getCurrentIndex());
            if (!"price".equals(nodeName) && !"item_number".equals(nodeName)) {
                System.out.println("Removing node " + nodeName);
                xmlModifier.remove();
            }
        } while (vtdNav.toElement(VTDNav.NEXT_SIBLING));
    }
    System.out.println();
    System.out.println("==============================================================");
    // normalizedQueryNav = normalizedQueryModifier.outputAndReparse();
    xmlModifier.output(System.out);
    System.out.println("==============================================================");
}

}

Sample.xml

<catalog>
<product description="Cardigan Sweater" product_image="cardigan.jpg">
    <catalog_item gender="Men's">
        <title>Cardigan Sweater</title>
        <item_number>QWZ5671</item_number>
        <size description="Medium">
            <color_swatch image="red_cardigan.jpg">Red</color_swatch>
            <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
        </size>
        <size description="Large">
            <color_swatch image="red_cardigan.jpg">Red</color_swatch>
            <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
        </size>
        <price>39.95</price>
    </catalog_item>
</product>

I tried using the following code instead of xmlModifier.remove()

   long elementFragment = vtdNav.getElementFragment();
   xmlModifier.remove(vtdNav.expandWhiteSpaces(elementFragment));

It fails with the following exception:-

com.ximpleware.ModifyException: Invalid insertion/deletion condition    detected between offset 189 and offset 373
at com.ximpleware.XMLModifier.check2(XMLModifier.java:888)
at com.ximpleware.XMLModifier.output(XMLModifier.java:1977)
at vtd.VTDWhiteSpaceIssue.main(VTDWhiteSpaceIssue.java:40)

Note:- Sample code executed with vtd-xml_2_13.jar

Muhammed K
  • 39
  • 4
  • I will look into it and get back to you on this... one quick comment: you extract the nodeName out into a string, which is not optimal... you can use matchElement(String s) to directly compare the node name, no intermediate string is created, saving a single round trip of object/string creation and collection. – vtd-xml-author Oct 29 '16 at 01:52

1 Answers1

0

The exception is caused by fragments overlapping... obviously when you call expandWhiteSpaces on node named "size." The trailing white spaces of first Size will overlap with the leading white spaces of the second "size" element. The fix is to call

public final long expandWhiteSpaces(long l,
                                    short actionType)

For action Type, use WS_LEADING. That should do it for you.

vtd-xml-author
  • 3,319
  • 4
  • 22
  • 30