Home > Net >  XMLAdapter - Is it possible to call de default marshal method inside the adapter?
XMLAdapter - Is it possible to call de default marshal method inside the adapter?

Time:09-20

I have a system when I need to convert one XML to another. Todo so, I have a model that I'm unmarshalling into, doing some necessary changes and then marshalling to the final XML.

One class has one attribute (description) that's inside only at the final XML. But this attribute is dependent to another Attribute (ID) from the original XML.

For example:

Original XML:

<root>
  <node1>
    <node2 attr1="xxx" attr2="xxx" attrN="xxx" id="1234"/>
  </node1>
</root>

The final XML would be:

<root>
  <node1>
    <node2 attr1="xxx" attr2="xxx" attrN="xxx" id="1234" description="description"/>
  </node1>
</root>

The XMLs have many (dozens or more) instances of node1, node2, etc. To populate it, I'm considering using the XMLAdapter. But, to avoid creating the object manually and populate attribute by attribute, I'd like to have someway to call a 'super' marshal and unmarshal method, so it'd generated easily, but I'm not sure if it's possible and how.

I have 2 ideas.

Option 1, creating a XMLAdapter at type level and call the marshaller marshal/unmarshal methods inside the Adapter.

Node2 Model:

@XmlJavaTypeAdapter(MyAdapter.class)
public class Node2 { 
    ... 
}

My Adapter:

public class MyAdapter extends XmlAdapter <String, Node2> {

  @Override
  public Node2 unmarshal(String s) throws Exception {
    Node2 node2 = marshaller.unmarshal(s); //Can I do something like this, access the original marshaller?
    node2.setDescription(myNewMethodToGetDescription(node2.getId()));
    return node2;
  }

  @Override
  public String marshal(Node2 node2) throws Exception {
    return marshaller.marshal(node2); //Can I do something like this, access the original marshaller?
  }

}

Option 2, creating a XMLAdapter at field level. For this, I'd need to access another attribute from the node:

Node2 Model:

public class Node2 { 
    ... 

  @XmlAttribute(name = "id")
  protected String id;
  @XmlAttribute(name = "description")
  @XmlJavaTypeAdapter(MyAdapter.class)
  protected String description;
}

My Adapter:

public class MyAdapter extends XmlAdapter <String, String> {

  @Override
  public String unmarshal(String s) throws Exception {
    Node2 node2 = getNode2SomeHow(); //Can I do something similar?
    return myNewMethodToGetDescription(node2.getId());
  }

  @Override
  public String marshal(String s) throws Exception {
    return s; 
  }

}

How can I do it?

CodePudding user response:

I found a solution. Actually it's easier that I thought.

At the type level, I just need to use the XMLAdapter passing the Node2 as object in both parameters, the XMLAdapter will use the unmarshalled object already.

So it'd be like this:

public class MyAdapter extends XmlAdapter <Node2, Node2> {

  @Override
  public Node2 unmarshal(Node2 node2) throws Exception {
    node2.setDescription(myNewMethodToGetDescription(node2.getId()));
    return node2;
  }

  @Override
  public Node2 marshal(Node2 node2) throws Exception {
    return node2
  }

}
  • Related