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 Converting an object into an XML 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 "Converting an object into an XML" Watch "Converting an object into an XML" New topic
Author

Converting an object into an XML

Ramachandran Narayanan
Greenhorn

Joined: Feb 26, 2010
Posts: 2
I have an object which is mapped to an XML.The object has a String variable whose value is "Lines requested is above the maximum of %3d "(12 white spaces)

In the above message I expect a 12 character white space after the error message. I have tried adding the white spacing in the code. But when the response XML is build using the XML Schema that is provided, the white spaces are trimmed.

Is there some way to solve this?

Thanks
Ramachandran
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1836
    
    7

Which API are you using to map the object to an XML representation? What does the object mapping look like exactly (for the String member at the very least)?

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Ramachandran Narayanan
Greenhorn

Joined: Feb 26, 2010
Posts: 2
I am using the Apache API.

The object Mapping looks like this:

<element name = "ErrorList">
<complexType>
<sequence>
<element ref = "q:ErrorCode"/>
<element ref = "qErrorMessage"/>(where i need to display the message with the whitespaces)
</sequence>
</complexType>
</element>

and <element name = "ErrorCode" type = "string"/>
<element name = "ErrorMessage" type = "string"/>
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1836
    
    7

The schema definition indicates that the element only content model applies, in which case it's possible that the parser implementation, if in validating mode, will trim ignorable whitespaces from the element's content. Not sure how likekly that is though. You said that you're using "the Apache API". By that do you mean XMLBeans? Could you post the code that invokes the API that write the Object graph to the XML file? Are you absolutely sure that, before writing the String value to the XML file it does, in fact, contain the 12 trailing white spaces?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18993
    
    8

From the XML Recommendation, the section about attribute value normalization:

If the attribute type is not CDATA, then the XML processor MUST further process the normalized attribute value by discarding any leading and trailing space (#x20) characters, and by replacing sequences of space (#x20) characters by a single space (#x20) character.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1836
    
    7

Yes, but that's attribute normalization, which doesn't apply to element values, if I'm not mistaken.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Converting an object into an XML