<?xml version="1.0" encoding="UTF-8"?>
<!--

DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.

Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.

The contents of this file are subject to the terms of either the GNU
General Public License Version 2 only ("GPL") or the Common Development
and Distribution License("CDDL") (collectively, the "License").  You
may not use this file except in compliance with the License.  You can
obtain a copy of the License at
http://glassfish.java.net/public/CDDL+GPL_1_1.html
or packager/legal/LICENSE.txt.  See the License for the specific
language governing permissions and limitations under the License.

When distributing the software, include this License Header Notice in each
file and include the License file at packager/legal/LICENSE.txt.

GPL Classpath Exception:
Oracle designates this particular file as subject to the "Classpath"
exception as provided by Oracle in the GPL Version 2 section of the License
file that accompanied this code.

Modifications:
If applicable, add the following below the License Header, with the fields
enclosed by brackets [] replaced by your own identifying information:
"Portions Copyright [year] [name of copyright owner]"

Contributor(s):
If you wish your version of this file to be governed by only the CDDL or
only the GPL Version 2, indicate your decision by adding "[Contributor]
elects to include this software in this distribution under the [CDDL or GPL
Version 2] license."  If you don't indicate a single choice of license, a
recipient has the option to distribute your version of this file under
either the CDDL, the GPL Version 2 or to extend the choice of license to
its licensees as provided above.  However, if you add GPL Version 2 code
and therefore, elected the GPL Version 2 license, then the option applies
only if the new code is made subject to such option by the copyright
holder.

        -->
<!DOCTYPE book [
<!ENTITY % ents SYSTEM "docbook.ent">
%ents;
]>
<section version="5.0" xml:id="unmarshalling-dealing-with-large-documents"
         xml:lang="en" xmlns="http://docbook.org/ns/docbook"
         xmlns:xlink="http://www.w3.org/1999/xlink"
         xmlns:ns5="http://www.w3.org/1999/xhtml"
         xmlns:ns3="http://www.w3.org/2000/svg"
         xmlns:ns="http://docbook.org/ns/docbook"
         xmlns:m="http://www.w3.org/1998/Math/MathML">
    <title>Dealing with large documents</title>

    <para>JAXB API is designed to make it easy to read the whole XML document
    into a single tree of JAXB objects. This is the typical use case, but in
    some situations this is not desirable. Perhaps:</para>

    <orderedlist>
        <listitem>
            <para>A document is huge and therefore the whole may not fit the
            memory.</para>
        </listitem>

        <listitem>
            <para>A document is a live stream of XML (such as <link
            xlink:href="http://www.xmpp.org/">XMPP</link>) and therefore you
            can't wait for the EOF.</para>
        </listitem>

        <listitem>
            <para>You only need to databind the portion of a document and
            would like to process the rest in other XML APIs.</para>
        </listitem>
    </orderedlist>

    <para>This section discusses several advanced techniques to deal with
    these situations.</para>

    <section xml:id="Processing_a_document_by_chunk">
        <title>Processing a document by chunk</title>

        <para>When a document is large, it's usually because there's
        repetitive parts in it. Perhaps it's a purchase order with a large
        list of line items, or perhaps it's an XML log file with large number
        of log entries.</para>

        <para>This kind of XML is suitable for chunk-processing; the main idea
        is to use the StAX API, run a loop, and unmarshal individual chunks
        separately. Your program acts on a single chunk, and then throws it
        away. In this way, you'll be only keeping at most one chunk in memory,
        which allows you to process large documents.</para>

        <para>See the streaming-unmarshalling example and the
        partial-unmarshalling example in the JAXB RI distribution for more
        about how to do this. The streaming-unmarshalling example has an
        advantage that it can handle chunks at arbitrary nest level, yet it
        requires you to deal with the push model --- JAXB unmarshaller will
        "<literal>push</literal>" new chunk to you and you'll need to process them right
        there.</para>

        <para>In contrast, the partial-unmarshalling example works in a pull
        model (which usually makes the processing easier), but this approach
        has some limitations in databinding portions other than the repeated
        part.</para>
    </section>

    <section xml:id="Processing_a_live_stream_of_XML">
        <title>Processing a live stream of XML</title>

        <para>The techniques discussed above can be used to handle this case
        as well, since they let you unmarshal chunks one by one. See the
        xml-channel example in the JAXB RI distribution for more about how to
        do this.</para>
    </section>

    <section xml:id="Creating_virtual_infosets">
        <title>Creating virtual infosets</title>

        <para>For further advanced cases, one could always run a streaming
        infoset conversion outside JAXB API and basically curve just the
        portion of the infoset you want to data-bind, and feed it as a
        complete infoset into JAXB API. JAXB API accepts XML infoset in many
        different forms (DOM, SAX, StAX), so there's a fair amount of
        flexibility in choosing the right trade off between the development
        effort in doing this and the runtime performance.</para>

        <para>For more about this, refer to the respective XML infoset
        API.</para>
    </section>
</section>
