gene b.
gene b.

Reputation: 11984

Remove Leading/Trailing Whitespace from all XML Values

What is the quickest way to trim all leading/trailing whitespace from all node values in XML?

We have a Java application which uses JDOM to parse the XML document.

Upvotes: 1

Views: 795

Answers (2)

vtd-xml-author
vtd-xml-author

Reputation: 3377

Here is the code snippet to do it in VTD-XML

import com.ximpleware.*;
import java.io.*;
public class trim {
    public static void main(String s[]) throws VTDException,IOException{
        VTDGen vg = new VTDGen();
        if (!vg.parseFile("d:\\xml\\input.txt", false))
            return;
        VTDNav vn = vg.getNav();
        AutoPilot ap = new AutoPilot(vn);
        XMLModifier xm  = new XMLModifier(vn);
        ap.selectXPath("//text()");
        int i=0;
        long l=0;
        while((i=ap.evalXPath())!=-1){
            l=vn.getTokenOffset(i)| ((long)vn.getTokenLength(i))<<32;
            l = vn.trimWhiteSpaces(l);
            xm.updateToken(i,vn,(int)l,(int)(l>>32));
        }
        xm.output("d:\\xml\\output.txt");
    }
}

Upvotes: 0

gene b.
gene b.

Reputation: 11984

This is my manual solution with JDOM. It seems to work, but let me know of any issues if you see any.

public static void trimWhitespace(Element element)
{
    List<Element> children = (List<Element>)element.getChildren();
    if (children != null)
    {
        for (int i = 0; i < children.size(); i++)
        {
            Element child = children.get(i);

            if (child.getChildren() != null && child.getChildren().size() > 0)
                trimWhitespace(child);
            else
            {
                // Leaf Element
                if (child.getText() != null)
                {
                    child.setText(child.getText().trim());
                }
            }
        }
    }
}

This recursive function is initially called with the root element,

trimWhitespace(rootElement);

Upvotes: 0

Related Questions