开发者

JAXB Dynamic Xml Tag Generation

In my current JAXB marshaller application, I need to conditionally write an element into xml. Let me see if I can think of an example without making it sound like a college homework assignment.

I have a class called Student with properties called merit and status. Not all students will have Merit. Only the non-Freshman students will have merits ( property called status gives the information on whether student is freshman or not). While I traverse through the list of Students and marshall into xml, I need to conditionally display Merit. How do I handle such dynamic creation of XML elements with JAXB? I can think of a solution where I split my Student class into FreshManStudents and OtherStudents and define merit 开发者_Go百科as @XMlTransient for Otherstudents. Isn't that a bit overkill to refactor my object just for JAXB purpose?

Alternatively, I can have the logic inside getMerit adn have the getMerit return NULL for Freshman students. Assuming JAXB ignores null objects, is that a valid ( read elegant) way to solve the problem?

Thanks for your opinions and suggestions

 public class Student {
        private Merit merit;
        private String status;

        public Merit getMerit() {
           return merit;
        }

        public void setMerit(Merit m) {
           merit= m;        
        } 

        public String getStatus() {
           return status;
        }

        public void setStatus(String s) {
           status= s;        
        }   

    }


Quick Fix

Alternatively, I can have the logic inside getMerit adn have the getMerit return NULL for Freshman students. Assuming JAXB ignores null objects, is that a valid ( read elegant) way to solve the problem?

This would work, and matches your problem description (below):

I have a class called Student with properties called merit and status. Not all students will have Merit. Only the non-Freshman students will have merits ( property called status gives the information on whether student is freshman or not)


Regarding Inheritance

I can think of a solution where I split my Student class into FreshManStudents and OtherStudents and define merit as @XMlTransient for Otherstudents. Isn't that a bit overkill to refactor my object just for JAXB purpose?

Your use case calls for inheritance regardless of JAXB's involvement:

Student

public class Student {

    private String status;

    public String getStatus() {
       return status;
    }

    public void setStatus(String s) {
       status= s;        
    }   

}

NonFreshman

public class NonFreshman extends Student {

    private Merit merit;

    public Merit getMerit() {
       return merit;
    }

    public void setMerit(Merit m) {
       merit= m;        
    } 

}

For More Information

  • http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-xsitype.html
  • http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-substitution.html
  • http://blog.bdoughan.com/2011/06/ignoring-inheritance-with-xmltransient.html
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜