• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem using JAXB to marshall string data containing XML tags into XML file

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I'm encountering a small problem with my app that I'm hoping someone can clear up for me. I am doing a two-step marshalling procedure to create an XML file. What happens is all < and > characters from the first marshall step are converted to & lt; and & gt; during the second marshall step. (I've added a space after the escape character)

Here's a little preamble and some examples of the steps involved: I'm using JAXB to unmarshall an XML file into a collection of objects - each object is an input txn. I iterate the collection and process the txns.

Here is an example of one of the input txns:
<inputTxn>
<field1>xxx</field1>
<field2>xxx</field2>
...
</inputTxn>

For each unmarshalled txn (object), we marshall it to string data. This is done to "preserve" the originating txn in the format as received (XML format). This part works fine, the resulting string data looks like the received XML txn above.

We then go on to build a txResult object, and one of the attributes for this object is the originalTxn (the marshalled-txn as string data). We populate the remaining attributes of the txResult object (transaction Ids, error information etc), and then marshall the txResult object.

So, to clarify, txResult contains an atribute called originalTxn which is a String and has a value of "<inputTxn><field1>xxx</field1>...</inputTxn>"

This is what I'm expecting the marshalled txResult object to look like:
<txResult>
<transactionId>12345</transactionId>
<errors>none</errors>
<originalTxn>
<inputTxn>...</inputTxn>
</originalTxn>
</txResult>

But what I get is:
<txResult>
<transactionId>12345</transactionId>
<errors>none</errors>
<originalTxn>
& lt;inputTxn& gt;...& lt;inputTxn& gt;
</originalTxn>
</txResult>

Why are the < and > characters in the originalTxn attribute converted to & gt; and & lt; during the 2nd marshall phase, and what can I do to prevent that from occurring?

Very sorry for possibly asking an extremely stupid question, but I'm not sure what the cause of this is and it's pretty darn frustrating. Thanks for your time and assistance, always much appreciated....

Steve

[ July 19, 2005: Message edited by: Steve Kritzler ]
[ July 19, 2005: Message edited by: Steve Kritzler ]
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been there and yes, it is very frustrating. If you are constructing an Element that contains text that has < and > - that will get serialized to the & form by the output transform.
Try putting the string into the output document as the content of a CDATA node, not a TEXT node.
Bill
 
reply
    Bookmark Topic Watch Topic
  • New Topic