Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize Document type to a Java Class

Tags:

java

Is it possible to deserialize a "org.w3c.dom.Document" type into a Java class? We have this class (Employee), and we have an existing method in our application that calls a web service and returns a "Document" type of the Employee class. Can we serialize the Document to Employee class?

Can you please show some example? Most of the sample I found from the internet reads an XML file in a directory then deserialize it to the class. But in our scenario, the object is a Document type.

What I'm trying to achieve is something like as below (pseudocode):

String employeeID = "12344444";
org.w3c.dom.Document xmlEmployee = helperClass.callWebServiceEmployee(employeeID);

EmployeeClass employee = new EmployeeClass();

employee = (EmployeeClass)xmlEmployee. 
like image 749
yonan2236 Avatar asked Feb 02 '26 13:02

yonan2236


1 Answers

Part of the reason people aren't leaping to help you is that you've got the terminology wrong. Going from DOM objects to domain-specific Java objects is NOT serialization. It is translation one object form to another object form. Serialization is when you go from an object form (e.g. DOM) to a serial form; e.g. XML.

(And it isn't deserialisation either :-) )


Anyway ...

Given where you currently are, I think this approach should work:

  1. Serialise the DOM objects to XML.

  2. Deserialise the XML to POJOs using XStream or JAXB or some other "XML binding" technology.

It is not the most efficient approach (in terms of runtime costs), but I suspect that the efficient approach would entail implementing a mapping / binding technology that doesn't currently exist and (frankly) wouldn't be much use to other people.

But then I don't know why you are starting from DOM objects in the first place. It doen't make a lot of sense to me:

  • If you got the DOM objects by parsing XML, then it would better to start with the XML and go directly to step 2.

  • If you constructed the DOM some other way ..... why?

like image 112
Stephen C Avatar answered Feb 04 '26 06:02

Stephen C