[jdom-interest] Element attributes removing white space and returns
Jason Hunter
jhunter at apache.org
Mon Jan 21 11:16:00 PST 2002
If you read the XML specification, you'll see that attribute values are
required to be normalized by the parser. If you want whitespace
preserved, you need to use elements.
-jh-
Matt Petteys wrote:
>
> 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");
>
> }
>
> }
>
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com
More information about the jdom-interest
mailing list