[jdom-interest] Element attributes removing white space and returns

Matt Petteys matt at liquidcool.com
Mon Jan 21 05:58:26 PST 2002


Is there any equivalent function of setTextNormalize for attributes that
will prevent white space or returns from being removed from attribute
values..   Take a look at the following code which fails on the equals
comparison of the attVal value because the \n character has been removed.

import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.XMLOutputter;

public class testjdom {

	public static void main(String[] args) {

		try {

			System.out.println("starting");

			XMLOutputter builder = null;
			Writer writer = null;

			builder = new XMLOutputter();
			builder.setTextNormalize(false);
			builder.setOmitDeclaration(false);
			builder.setOmitEncoding(false);

			System.out.println("creating");

			String value = "line\nline2>";

			Element e = new Element("test");

			//  set the base element to the constant value
			e.setText(value);

			// create a test attribute using the constant value
			e.setAttribute("t", value);

			System.out.println("writing");

			Document d = new Document(e);
			Writer w = new FileWriter("c:\\temp\\test.xml", false);
			builder.output(d, w);
			w.close();
			builder = null;

			System.out.println("reading");

			DOMBuilder input = new DOMBuilder();
			Document d2 = input.build(new FileInputStream("c:\\temp\\test.xml"));
			input = null;

			//  read the base elment value to compare to the constant
			String elVal = d2.getRootElement().getText();

			//  read the test attribute value to compare against the constant
			String attVal = d2.getRootElement().getAttribute("t").getValue();

			if (!value.equals(attVal))
				System.out.println("wrong attribute val:[" + attVal + "]");
			else
				System.out.println("attribute value correct");

			if (!value.equals(elVal))
				System.out.println("wrong element val:[" + elVal + "]");
			else
				System.out.println("element value correct");

		} catch (Exception e) {
			System.out.println("exception");
			e.printStackTrace();
		}

		System.out.println("done");

	}

}




More information about the jdom-interest mailing list