Suresh Naidu
Suresh Naidu

Reputation: 21

VTD XMLRemoving the spaces after Removing the Element

We are using VTD XML for our application.Can you help us in solving the issue in our application.

The issue is: input xml:

    <root>
           <name>suresh</name>
           <address>Address</address>
    </root>


    output xml:

    <root>

          <address>Address</address>
    </root>

    But i want the output to be:

    <root>
          <address>Address</address>
    </root>

When i remove the element with xmlmodifier it is creating an empty space(new line) for the removed element.

Is there any possibility to remove that vtdxml?

Upvotes: 1

Views: 420

Answers (1)

Suresh Naidu
Suresh Naidu

Reputation: 21

Yes, there is, in version 2.12, there is a method called expandWhiteSpace in VTDNav, it works like the following

public final long expandWhiteSpaces(long l)

New in v2.12 This method will take a segment descriptor and return a new descriptor that includes all the leading and trailing white spaces around the input segment It has no effect on CDATA A typical usage would be 'expandWhiteSpaces(getElementFragment())' Parameters: l - upper 32 bits length of the segment, lower 32 bits offset of the segment, unit is byte offset Returns: a long 64 bit segment descriptor Throws: NavException

very thankful to Jimmy Zhang

I have provided the sample program about the usage.

import com.ximpleware.*;
import java.io.*;
public class testExpandSpace {
    public static void main(String[] args) throws VTDException,IOException{
        // TODO Auto-generated method stub
            VTDGen vg = new VTDGen();
            AutoPilot ap = new AutoPilot();
            XMLModifier xm = new XMLModifier();
            if (!vg.parseFile("d://xml//testSuresh.xml",false))
                return;
            VTDNav vn=vg.getNav();
            ap.bind(vn);
            xm.bind(vn);
            ap.selectXPath("//name");
            int index=-1;
            while((index=ap.evalXPath())!=-1)
            {
                System.out.println(" ===> "+vn.toString(index) +"===>");
                long elementFragment=vn.getElementFragment();
                xm.remove(vn.expandWhiteSpaces(elementFragment));
            }
            xm.output("d://xml//test1111.xml");
    }
}

Upvotes: 1

Related Questions