Creating an XML document from Java objects, where the structure is very different

Situation

I have a complex object graphic in Java that needs to be translated back and forth into an XML document. The structure of the graphical object of the XML document schema is very different from the tree of model objects. The two are interchangeable, but translation requires a lot of logic based on contexts that use the relationship between parents and children.

Problem

I work with model objects that are well installed on the old system, and the XML document schema is fairly new. Since our code depends on the structure of model objects, we do not want to restructure them. Here is a simplified example of the type of structural differences I'm dealing with:

Example data model tree

Paragraph

  • Description
  • cost
  • ...

Person

  • Name
  • Surname
  • The address
  • ...

  • ...

SaleTransaction (* )

  • ()
  • ()
  • [] ()
  • [] ()
  • ()

XML-

    • party_contact_ref
      • contact_id
    • total_amount_exchanged
      • owning_party_contact_ref_id
      • exchange_use_type
      • ID

: [ | | ]

: [PERSON | ADDRESS]

: [CASH EXCHANGE | BARTER EXCHANGE]

SaleTransaction Exchange , 1-1. "" , XML. , "owning_party_contact_ref_id" "Item" SaleTransaction.

, , - , XML-, go-to - XmlAdapter. , , XML- JAXB .

  • XML- , . , XmlAdapter / .
  • , XmlAdapters. MOXy , , .
  • , XmlAdapters , /.

, , ? ?

, , :

# 1 XML. , - . JAXB XML, , . XML- , . , , .

# 2 XmlAdapter , (, XML). JAXB, , , .

# 3 # 1, JDOM JAXB. JAXB, DOM.

, # 1.

+5
4

1 - . , . , , , , , Java . .

Dozer , . , , .

, xjc, JAXB, , fluent-api value-constructor.

+2

, , , XML-, go-to - XmlAdapter. XML- JAXB . , XmlAdapters , /unmarshalled.

XmlAdapter , XmlAdapter Marshaller/Unmarshaller, . , .

YourAdapter yourAdapter = new YourAdapter();
yourAdapter.setState(someState);
marshaller.setAdapter(yourAdapter);
unmarshaller.setAdapter(yourAdapter);

, Marshaller/Unmarshaller, , :

@XmlJavaTypeAdpater(YourAdapter.class)

+1

, , , "".

Altova XML Spy . . , , , , , ", , ".

MapForce ( , ), , , .

, , ( ).

0

If you have an xsd schema, it is very useful to generate pojos using a tool like castor . When you have a package with pojos created, you can use dozer to map your "old" object model to pojos, which represent an XML document. Then you simply transform the object created by the castor using Marshaller for Stream, String, etc.

0
source

All Articles