简体   繁体   English

XMLAdapter - 是否可以在适配器内调用默认编组方法?

[英]XMLAdapter - Is it possible to call de default marshal method inside the adapter?

I have a system when I need to convert one XML to another.当我需要将一个 XML 转换为另一个时,我有一个系统。 Todo so, I have a model that I'm unmarshalling into, doing some necessary changes and then marshalling to the final XML.为此,我有一个 model 正在解组,进行一些必要的更改,然后编组到最终的 XML。

One class has one attribute (description) that's inside only at the final XML.一个 class 有一个属性(描述),它只在最后的 XML 内部。 But this attribute is dependent to another Attribute (ID) from the original XML.但此属性依赖于原始 XML 中的另一个属性 (ID)。

For example:例如:

Original XML:原装 XML:

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

The final XML would be:最终的 XML 将是:

<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. XML 有许多(几十个或更多)node1、node2 等实例。为了填充它,我正在考虑使用 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.但是,为了避免手动创建 object 并按属性填充属性,我想有办法调用“超级”编组和解组方法,所以它很容易生成,但我不确定它是否可能以及如何.

I have 2 ideas.我有2个想法。

Option 1, creating a XMLAdapter at type level and call the marshaller marshal/unmarshal methods inside the Adapter.选项 1,在类型级别创建 XMLAdapter 并调用适配器内部的编组器编组/解组方法。

Node2 Model:节点 2 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.选项 2,在字段级别创建 XMLAdapter。 For this, I'd need to access another attribute from the node:为此,我需要从节点访问另一个属性:

Node2 Model:节点 2 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?我该怎么做?

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.在类型级别,我只需要在两个参数中使用将 Node2 传递为 object 的 XMLAdapter,XMLAdapter 将使用未编组的 object。

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
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM