Difference between revisions of "EclipseLink/UserGuide/MOXy/Runtime/Binding to an Existing Document"

From Eclipsepedia

Jump to: navigation, search
m
m (Replacing page with ''''Warning For the current release, see [http://www.eclipse.org/eclipselink/documentation/2.4/moxy Developing JAXB Applications Using EclipseLink ...')
 
(13 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{EclipseLink_UserGuide
+
'''[[Image:Elug_draft_icon.png|Warning]] For the current release, see [http://www.eclipse.org/eclipselink/documentation/2.4/moxy Developing JAXB Applications Using EclipseLink MOXy, EclipseLink 2.4]
|info=y
+
'''
}}
+
  
=Binding to an Existing XML Document=
+
http://www.eclipse.org/eclipselink/documentation/2.4/moxy/runtime009.htm
The JAXB Binder interface (introduced in JAXB 2.0) allows you to preserve an entire XML document, even if only some of the items are mapped.  
+
 
+
Normally, when using a Unmarshaller to load the XML document into objects and a Marshaller to save the objects back to XML, the unmapped content is lost.
+
 
+
The Binder is created from the '''JAXBContext''' and interacts with XML in the form of a DOM. The Binder maintains an association between the Java objects and their corresponding DOM nodes.
+
 
+
<source lang="java">
+
import java.io.File;
+
import javax.xml.bind.*;
+
import javax.xml.parsers.*;
+
import javax.xml.transform.*;
+
import javax.xml.transform.dom.DOMSource;
+
import javax.xml.transform.stream.StreamResult;
+
import org.w3c.dom.*;
+
+
public class BinderDemo {
+
+
    public static void main(String[] args) throws Exception {
+
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+
        DocumentBuilder db = dbf.newDocumentBuilder();
+
        File xml = new File("input.xml");
+
        Document document = db.parse(xml);
+
+
        JAXBContext jc = JAXBContext.newInstance(Customer.class);
+
+
        Binder<Node> binder = jc.createBinder();
+
        Customer customer = (Customer) binder.unmarshal(document);
+
        customer.getAddress().setStreet("2 NEW STREET");
+
        PhoneNumber workPhone = new PhoneNumber();
+
        workPhone.setType("work");
+
        workPhone.setValue("555-WORK");
+
        customer.getPhoneNumbers().add(workPhone);
+
        binder.updateXML(customer);
+
+
        TransformerFactory tf = TransformerFactory.newInstance();
+
        Transformer t = tf.newTransformer();
+
        t.transform(new DOMSource(document), new StreamResult(System.out));
+
    }
+
+
}
+
</source>
+
 
+
The Binder applies the changes to the original DOM instead of creating a new XML document, thereby preserving the entire XML infoset.
+
 
+
<source lang="xml">
+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+
<customer>
+
    <UNMAPPED_ELEMENT_1/>
+
    <name>Jane Doe</name>
+
    <!-- COMMENT #1 -->
+
    <address>
+
        <UNMAPPED_ELEMENT_2/>
+
        <street>2 NEW STREET</street>
+
        <!-- COMMENT #2 -->
+
        <UNMAPPED_ELEMENT_3/>
+
        <city>Any Town</city>
+
    </address>
+
    <!-- COMMENT #3 -->
+
    <UNMAPPED_ELEMENT_4/>
+
    <phone-number type="home">555-HOME</phone-number>
+
    <!-- COMMENT #4 -->
+
    <phone-number type="cell">555-CELL</phone-number>
+
    <phone-number type="work">555-WORK</phone-number>
+
    <UNMAPPED_ELEMENT_5/>
+
    <!-- COMMENT #5 -->
+
</customer>
+
</source>
+
 
+
{{EclipseLink_MOXy
+
|previous=[[EclipseLink/UserGuide/MOXy/Relationships/Shared Reference/Keys and Foreign Keys|Keys and Foreign Keys]]
+
|up      =[[EclipseLink/UserGuide/MOXy/Relationships/Shared Reference/Keys and Foreign Keys|Keys and Foreign Keys]]
+
|next    =[[EclipseLink/UserGuide/MOXy/Relationships/Shared Reference/Keys and Foreign Keys/Composite Key|Composite Key]]
+
|version=2.2.0
+
}}
+

Latest revision as of 16:31, 6 November 2012

Warning For the current release, see Developing JAXB Applications Using EclipseLink MOXy, EclipseLink 2.4

http://www.eclipse.org/eclipselink/documentation/2.4/moxy/runtime009.htm