File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Illegal XML character � Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Illegal XML character � " Watch "Illegal XML character � " New topic
Author

Illegal XML character �

Vladimir Bezugliy
Ranch Hand

Joined: Sep 29, 2004
Posts: 33
I should save tree to xml file and read it from this file.

I save tree nodes as element <node>.
Value of node I save as text in element <value>.


But when I read this file I get SAXException "Illegal XML character �".

I save my value this way

What should I do in order not to write � text in xml file?
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
That snippet of code you posted doesn't (as far as I can see) write anything to disk?


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Vladimir Bezugliy
Ranch Hand

Joined: Sep 29, 2004
Posts: 33
Here is some more code


And as result I should get something like this
<value><![CDATA[serialized string "root"]]></value>

but I get this string as result
<value><![CDATA[┬м╨╜]]>�<![CDATA[]]><![CDATA[t]]>�<![CDATA[]]><![CDATA[root]]></value>

And this string is not right look at the "& # 0 ;". It is not correct string.

Where is the bug?
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
I believe the bug is here:

Having the JVM serialize a String object is not the same as writing the String object to a stream.

If you skip the whole ByteArrayOutputStream/ObjectOutputStream stuff and set valueString's value directly to "root", does it work then?
Vladimir Bezugliy
Ranch Hand

Joined: Sep 29, 2004
Posts: 33
It should be possible to use any Object not only Strings.

treeNode.setValue(<<SOME OBJECT>>);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(baos);

//Serialize Object
os.writeObject(treeNode.getValue());

os.flush();

String valueString = new String(baos.toByteArray());

os.close();
baos.close();

//create child node "value"
//this node will contain CDATA section.
Element valueXMLNode = document.createElement("value");
currentXMLNode.appendChild(valueXMLNode);

//Store serialized Object as CDATA section
CDATASection dataXMLNode = document.createCDATASection(valueString);

valueXMLNode.appendChild(dataXMLNode);


And instead of something like this


I get next code


Why there are many CDATA section instead of one section?
Vladimir Bezugliy
Ranch Hand

Joined: Sep 29, 2004
Posts: 33
I am sorry. I forget CDATA section.

Instead of this
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Ok. Object serialization in Java produces a binary output. That binary output may or may not be valid content for an XML message even if it's inside a CDATA block. Now, to solve the need to be able to encode any kind of Java objects into the XML message, you could
1) Base64 encode the serialized bytes before injecting that into the XML and then Base64 decode the stuff you read from the XML before running them through an ObjectInputStream, or
2) Use an XML-to-Java serialization library such as XStream, Castor or Digester.
Vladimir Bezugliy
Ranch Hand

Joined: Sep 29, 2004
Posts: 33
>>Base64 encode the serialized bytes before injecting that into the XML
So, I will use this way.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Illegal XML character &#x0;