Created
March 17, 2015 13:30
-
-
Save dmikurube/aeaef5e012e8c8afa3b4 to your computer and use it in GitHub Desktop.
JAXB Adapter from DOM nodes / elements
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.w3c.dom.Document; | |
import org.w3c.dom.Element; | |
import javax.xml.bind.annotation.adapters.XmlAdapter; | |
import javax.xml.parsers.DocumentBuilder; | |
import javax.xml.parsers.DocumentBuilderFactory; | |
// XmlAdapter<Object,*> receives XML DOM nodes in org.w3c.dom.Element. | |
public class MyXmlAdapter extends XmlAdapter<Object, TargetClass> { | |
private DocumentBuilder documentBuilder; | |
public MyXmlAdapter() { | |
try { | |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | |
documentBuilder = factory.newDocumentBuilder(); | |
} catch(Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
@Override | |
public TargetClass unmarshal(Object nodes) { | |
Element element = (Element)nodes; | |
TargetClass target = new TargetClass(...); | |
// target.... | |
return target; | |
} | |
@Override | |
public Object marshal(TargetClass v) { | |
Document document = documentBuilder.newDocument(); | |
Element root = document.createElement("element"); | |
// element.... | |
return root; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
XmlAdapter<org.w3c.dom.Element, *> does work well.