• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Illegal XML character �

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That snippet of code you posted doesn't (as far as I can see) write anything to disk?
 
Vladimir Bezugliy
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry. I forget CDATA section.

Instead of this
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>>Base64 encode the serialized bytes before injecting that into the XML
So, I will use this way.
 
You guys wanna see my fabulous new place? Or do you wanna look at this tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic