At 14:06 -0400 20/04/04, Koes, Derrick wrote:
I want to insert a line feed into an XML document, serialize it to a file,
and then parse it to a DOM document for use in an XSL transform (applying a
sytlesheet).  Parsing always throws the following exception.

org.xml.sax.SAXParseException: An invalid XML character (Unicode: 0x10) was
found in the element content of the document.

I have a feeling I'm inserting the wrong character for what I want.  Does
anyone have a suggestion?

Hex 0x10 isn't a line feed (\n); *decimal* 10 (0xA) is.

Using "\n", rather than "\u0010", doesn't cause
the exception, but it doesn't get me the desired line feed when I transform
the XML to HTML using an XSL stylesheet.

All whitespace (including \n) gets normalized to spaces. To get such a character into your XML text you need to use a character entity: 



Test code:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
Element test = doc.createElement("Test");
test.appendChild(doc.createTextNode("before"));


test.appendChild(doc.createTextNode("\u0010"));

Replace "\u0010" with "
".

test.appendChild(doc.createTextNode("after"));
doc.appendChild(test);
FileOutputStream fos = new FileOutputStream("C:/myoutput.xml");
Transformer t = transFact.newTransformer(); // identity transform
// not necessary
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
Node node = doc.getDocumentElement();
DOMSource source = new DOMSource(node);
StreamResult result = new StreamResult(fos);
t.transform(source, result);
fos.close();
db.parse(new File("C:/myoutput.xml"));


-- Richard Grossman, AIS Ltd.


-----Original Message-----
From: Koes, Derrick
Sent: Friday, April 16, 2004 10:49 AM
To: '[EMAIL PROTECTED]'
Subject: RE: insert carriage return

My apologies...

OK, I tried createTextNode(Character.toString('\u0010')) in my java code.
Serialization may happen correctly, however when I need to parse back into a
document I get an exception.

org.xml.sax.SAXParseException: An invalid XML character (Unicode: 0x10) was
found in the element content of the document.

What's the issue?

Thanks,
Derrick


-----Original Message----- From: Joseph Kesselman [mailto:[EMAIL PROTECTED] Sent: Friday, April 16, 2004 9:28 AM To: '[EMAIL PROTECTED]' Subject: Re: insert carriage return





Whether working with DOM or SAX, the answer is the same: just put the
characters you want  into a Text node's string value, as it would appear if
you had obtained the DOM by parsing a document. It's the serializer's
responsibility to render that appropriately when you output XML.

If you want a line break, XML's representation of line break is the newline
character (ASCII/Unicode 10). The serializer may convert that to CRLF if
that's the appropriate representation of line break on your system. (Note
that XML parsers must convert CRLF, CR, or LF to newline.)

If you really want a carriage return character, use it (ASCII/Unicode 13).
The serializer will recognize that the only way you can have this character
in XML content is if it was escaped, and will render it appropriately.

______________________________________
Joe Kesselman, IBM Next-Generation Web Technologies: XML, XSL and more.
"The world changed profoundly and unpredictably the day Tim Berners Lee
got bitten by a radioactive spider." -- Rafe Culpin, in r.m.filk
This electronic transmission is strictly confidential to Smith & Nephew and
intended solely for the addressee.  It may contain information which is
covered by legal, professional or other privilege.  If you are not the
intended addressee, or someone authorized by the intended addressee to
receive transmissions on behalf of the addressee, you must not retain,
disclose in any form, copy or take any action in reliance on this
transmission.  If you have received this transmission in error, please
notify the sender as soon as possible and destroy this message.
This electronic transmission is strictly confidential to Smith & Nephew and
intended solely for the addressee.  It may contain information which is
covered by legal, professional or other privilege.  If you are not the
intended addressee, or someone authorized by the intended addressee to
receive transmissions on behalf of the addressee, you must not retain,
disclose in any form, copy or take any action in reliance on this
transmission.  If you have received this transmission in error, please
notify the sender as soon as possible and destroy this message.



Reply via email to