How to add attributes in XML elements using JAXB in dynamic web application for Java EE

If you are trying to add an XML attribute in an XML entry using JAXB this information is for you. First of all you need to understand what an attribute in a XML entry means: in strict sense an attribute provides additional information about an element, this information could be useful to add metadata, states of an XML entry or any other extra information you want. As you know, all attribute’s values must always be quoted. Either single or double quotes can be used. Now, what’s better: use many attributes o XML elements? well, the answer according to W3C is: avoid attributes and try to adapt them to XML elements; but in my case I needed to use attributes.

java

1) Imagine you need to get this kind of XML output:

<profiles current="profileID1">
	<profile>
		<profile_id>profileID1</profile_id>
		<user_id>alex_arriaga</user_id>
		<name>Big Dealer Supply Co</dealer_name>
		<application>TOOL_ZH</application>
	</profile>
	<profile>
		<profile_id>profileID2</profile_id>
		<user_id>alex_arriaga_m</user_id>
		<application>TOOL_SX</application>
	</profile>
</profiles>

2) And you are using JAXB for dynamic generating for your XML document:

DataUserProfiles is a composed class to hold all profile data.

package com.base22.model;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="profiles")
@XmlAccessorType(XmlAccessType.FIELD)
public class DataUserProfiles{

	@XmlElement(name="profiles")
	private UserProfilesList profilesList;

	public DataUserProfiles(){

	}

	public DataUserProfiles(UserProfilesList profilesList){
		this.profilesList = profilesList;
	}

	public UserProfilesList getProfilesList() {
		return profilesList;
	}

	public void setProfilesList(UserProfilesList profilesList) {
		this.profilesList = profilesList;
	}

}

This class UserProfileList contains the definition of an attribute called current.

package com.base22.model;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * This class was created to hold an attribute "current" on the list of User's profiles
 * It could be removed if another library can be added (like EclipseLink MOXy to support XmlPath annotation)
 * @author Alex Arriaga (base22.com)
 *
 */
@XmlRootElement(name="profiles")
@XmlAccessorType(XmlAccessType.FIELD)
public class UserProfilesList {
	@XmlElement(name="profile")
	private List<UserProfile> profiles;

	@XmlAttribute(name="current")
	private String currentUserProfile;

	public UserProfilesList(){
		this.profiles = new ArrayList<UserProfile>();
	}

	public UserProfilesList(List<UserProfile> listProfiles){
		this.profiles = listProfiles;
	}

	public UserProfilesList(List<UserProfile> listProfiles, String currentUserProfile){
		this.profiles = listProfiles;
		this.currentUserProfile = currentUserProfile;
	}

	public List<UserProfile> getProfiles() {
		return profiles;
	}

	public void setProfiles(List<UserProfile> profiles) {
		this.profiles = profiles;
	}

	public String getCurrentUserProfile() {
		return currentUserProfile;
	}

	public void setCurrentUserProfile(String currentUserProfile) {
		this.currentUserProfile = currentUserProfile;
	}

}

Finally, class UserProfile contains: userId, profileId, name and application elements.

package com.base22.model;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * @author Alex Arriaga
 *
 */
@XmlRootElement(name = "profile")
@XmlAccessorType(XmlAccessType.FIELD)
public class UserProfile{
	@XmlElement(name="profile_id")
	private String profileId;

	@XmlElement(name="user_id")
	private String userId;

	@XmlElement(name="name")
	private String name;

	@XmlElement(name="application")
	private String application;

	public UserProfile(){

	}

	public UserProfile(String userId, String name, String application){
		this.userId = userId;
		this.profileId = "profile" + this.userId;
		this.name = name;
		this.application = application;
	}

	public String getProfileId() {
		return profileId;
	}

	public void setProfileId(String profileId) {
		this.profileId = profileId;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = dealerName;
	}

	public String getApplication() {
		return application;
	}

	public void setApplication(String application) {
		this.application = application;
	}
}

3) As you can see, it’s very easy to add an XML attribute to your Java class, just type the annotation “@XmlAttribute” and  JAXB will create your attribute automatically.

@XmlAttribute(name="current")
private String currentUserProfile;

That’s it!

Be happy with your code!

 

Añade un Comentario

Leave a Reply

Your email address will not be published. Required fields are marked *