Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Single Empty Tag in XML using JAXB  RSS feed

 
Ranch Hand
Posts: 31
1
Java Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I would want to generate a XML with a single closing tag for the elements which has null value.
For Eg: Instaed of representing XML in the below format
<Account></Account>

Want to represent like this
</Account>

Can anyone please help me understanding on how to do this using JAXB Implementation.?
 
Akhilesh Murthy
Ranch Hand
Posts: 31
1
Java Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Akhilesh Murthy wrote:Hi I would want to generate a XML with a single closing tag for the elements which has null value.
For Eg: Instaed of representing XML in the below format
<Account></Account>

Want to represent like this
<Account/>

Can anyone please help me understanding on how to do this using JAXB Implementation.?

 
Sheriff
Posts: 23640
48
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both formats are equivalent in XML and mean exactly the same thing. There should normally be no reason to care which of the two formats are used in any XML document, and all compliant XML parsers should treat them in exactly the same way. What's your reason for wanting one rather than the other?
 
Akhilesh Murthy
Ranch Hand
Posts: 31
1
Java Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:Both formats are equivalent in XML and mean exactly the same thing. There should normally be no reason to care which of the two formats are used in any XML document, and all compliant XML parsers should treat them in exactly the same way. What's your reason for wanting one rather than the other?

.

True, but i have a strict format structure from the regulator to whom i am sending this XML, they are rejecting the XML without this format.

Thanks,
Akhilesh
 
Paul Clapham
Sheriff
Posts: 23640
48
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pity about that. It's unfortunate that they don't understand XML but still attempt to specify it. Anyway, your best bet is to post-process the output to do that.
 
Ranch Hand
Posts: 44
1
Java Oracle Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Akhilesh Murthy wrote:

Akhilesh Murthy wrote:Hi I would want to generate a XML with a single closing tag for the elements which has null value.
For Eg: Instaed of representing XML in the below format
<Account></Account>

Want to represent like this
<Account/>

Can anyone please help me understanding on how to do this using JAXB Implementation.?



Hi there

I can see why they want to do this.  If you are transferring large amounts of XML data then having <tag></tag> style tags can just add to the size of the document. Industry regulated messaging systems love to be tight on these kind of definitions.

Anyhow, if you must have a <tag/> styled null tag you need to use a different JAXBContextFactory when you initialize the JAXBContext.  The example at https://stackoverflow.com/questions/17366054/represent-null-value-as-empty-element-in-xml-jaxb should do the job for you. 

Don't forget to create a jaxb.properties file mentioned in http://blog.bdoughan.com/2012/04/binding-to-json-xml-handling-null.html.

If your project is maven based then to get the library mentioned for the JAXBContextfactory, just add  the following dependancy (alternatively you could just download the file from mavenrepository):
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.7.0</version>
        </dependency>

The example marshall's to the console so if you want to marshall your result to a to string, it can be done via a StringWriter:
e.g. ( instead of marshaller.marshal(address, System.out); have the following code)

           StringWriter sw = new StringWriter();
           jaxbMarshaller.marshal(address, sw);
           String myString = sw.toString();
           System.out.println(myString);
          // from here you can unmarshall back to a class, or do what ever else you wish to do.

I hope that helps.

Cheers
Q


 
Akhilesh Murthy
Ranch Hand
Posts: 31
1
Java Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Q, this helps.

What i concluded after many iterations is JAXB doesnt support this format which i needed. I Used MOXY implementation from eclipse to pull this through. So MOXY provides annotations which can be specified on each element type  and any element within it .

Pasting the similar article what Q had posted but this is based on annotations.

https://stackoverflow.com/questions/11743306/how-to-represent-null-value-as-empty-element-with-jaxb

 
Quintin Stephenson
Ranch Hand
Posts: 44
1
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Akhilesh Murthy wrote:Thanks Q, this helps.

What i concluded after many iterations is JAXB doesnt support this format which i needed. I Used MOXY implementation from eclipse to pull this through. So MOXY provides annotations which can be specified on each element type  and any element within it .

Pasting the similar article what Q had posted but this is based on annotations.

https://stackoverflow.com/questions/11743306/how-to-represent-null-value-as-empty-element-with-jaxb



My pleasure, I saw that article as well.  If you can use the annotation version, it would be best, less work for you.

Cheers
Q
 
Paul Clapham
Sheriff
Posts: 23640
48
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great answers from both of you!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!