users@glassfish.java.net

Re: Jersey: XMLElement with the same name, but different structure

From: Paul Sandoz <Paul.Sandoz_at_oracle.com>
Date: Tue, 5 Oct 2010 11:38:38 +0200

Hi,

I think this is a JAXB issue.

It does not has enough information in the XML document to determine
which ArrayList to choose from. So it is just picking the first one
declared ArrayList<bcOneValueParameter> and using that (JAXB will
ignore any elements it does not understand).

To distinguish between the different parameter elements an xml:type
attribute will be required in the XML document.

Paul.


On Sep 21, 2010, at 12:20 PM, glassfish_at_javadesktop.org wrote:

> Hi all,
>
> I have a problem with parsing XML following structure using Jersey:
>
> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
> <entity id="">
> <parameter id="a9feb17a-5400-0001-2b0f-f2be4400000e">ReportDb01</
> parameter>
> <parameter id="a9feb17a-5400-0001-2b0f-f2be4400000f">on</parameter>
> <parameter id="a9feb17a-5400-0001-2b0f-f2be44000010">
> <element>item 1</element>
> <element>item 2</element>
> </parameter>
> </entity>
>
>
> The problem is in structure of incoming xml request – it may
> contents two or more XMLElement with the name <parameter>, but
> different structure: one value and multi valued.
> When Jersey is parsing this request it can’t decide what type of
> <parameter> (one value or multi valued) it should be instantiated.
> Actually Jersey parsing both type of parameters as one value.
>
> Here is my implementation:
>
> @XmlRootElement(name = "entity")
> @XmlAccessorType(XmlAccessType.FIELD)
> static class bcEntity {
> @XmlAttribute(name = "id")
> protected String m_id;
>
>
> @XmlElement(name = "parameter")
> public ArrayList<bcOneValueParameter> m_oneParams = new
> ArrayList<bcOneValueParameter>();
>
> @XmlElement(name = "parameter")
> public ArrayList<bcMultiValuedParameter> m_multiParams = new
> ArrayList<bcMultiValuedParameter>();
>
> public void addParam(bcOneValueParameter param) {
> m_oneParams.add(param);
> }
>
> public void addParam(bcMultiValuedParameter param) {
> m_multiParams.add(param);
> }
>
> public bcEntity() {
> }
>
> public bcEntity(HashMap<fiCustomProperty, Object> values) {
> //m_parameterValues = new bcParameterValues(task);
> m_id = "";
> for (fiCustomProperty prop : values.keySet()) {
> Object value = values.get(prop);
> //addParam(new bcParameter(prop, value)); //ok
>
> if (value != null) {
> // If the property is an array, generate each value within an
> <element> tag.
> if (prop.isMultiValued())
> addParam(new bcMultiValuedParameter(prop, value));
> else
> addParam(new bcOneValueParameter(prop, value));
> }
> }
> }
> }
>
>
> @XmlRootElement(name = "parameter")
> @XmlAccessorType(XmlAccessType.FIELD)
> static class bcMultiValuedParameter {
> @XmlAttribute(name = "id")
> protected String m_id;
>
> @XmlElement(name = "element")
> public ArrayList<bcElement> m_elements = new ArrayList<bcElement>();
>
> public void addElement(bcElement element) {
> m_elements.add(element);
> }
>
> public bcMultiValuedParameter() {
> }
>
> public bcMultiValuedParameter(fiCustomProperty prop, Object value) {
> m_id = prop.getGUID().toString();
> // If the property is an array, generate each value within an
> <element> tag.
> if (prop.isMultiValued()) {
> for (Iterator i = proCollections.iterator(value); i.hasNext();)
> addElement(new
> bcElement(fiDomainConverter.formatInterchangeValue(i.next())));
> }
> }
> }
>
> @XmlRootElement(name = "parameter")
> @XmlAccessorType(XmlAccessType.FIELD)
> static class bcOneValueParameter {
> @XmlAttribute(name = "id")
> protected String m_id;
>
> @XmlValue
> public String m_element;
>
> public bcOneValueParameter() {
> }
>
> public bcOneValueParameter(fiCustomProperty prop, Object value) {
> m_id = prop.getGUID().toString();
> m_element = fiDomainConverter.formatInterchangeValue(value);
> }
> }
>
> @XmlRootElement(name = "element")
> @XmlAccessorType(XmlAccessType.FIELD)
> static class bcElement {
> @XmlValue
> public String m_element;
>
> public bcElement() {
> }
>
> public bcElement(String element) {
> m_element = element;
> }
>
> public String toString() {
> return m_element;
> }
> }
>
> Is there a solution to this problem?[b] I can’t change the structure
> of xml!!![/b]
>
> Paul
> [Message sent by forum member 'pavelb']
>
> http://forums.java.net/jive/thread.jspa?messageID=483240
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>