From b6a08f8c4e8286a6a813dca51209d7befb29f543 Mon Sep 17 00:00:00 2001 From: "Dr. Martin Goik" <goik@hdm-stuttgart.de> Date: Wed, 11 Apr 2018 09:27:14 +0200 Subject: [PATCH] STAX/JAXP --- Doc/Sda1/dom.xml | 152 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 125 insertions(+), 27 deletions(-) diff --git a/Doc/Sda1/dom.xml b/Doc/Sda1/dom.xml index fda23bd4f..a0ad80781 100644 --- a/Doc/Sda1/dom.xml +++ b/Doc/Sda1/dom.xml @@ -30,16 +30,74 @@ </itemizedlist> </figure> + <figure xml:id="sda1_dom_fig_motivationApiOverview"> + <title>Important APIs</title> + + <itemizedlist> + <listitem> + <para>SAX</para> + </listitem> + + <listitem> + <para>Jaxb</para> + </listitem> + + <listitem> + <para>DOM / JDOM</para> + </listitem> + </itemizedlist> + + <informaltable border="0"> + <tr> + <th>Streaming / Event-based</th> + + <th>In-memory tree</th> + </tr> + + <tr> + <td valign="top"><itemizedlist> + <listitem> + <para><link + xlink:href="http://www.java2s.com/Tutorials/Java/Java_XML/0030__Java_SAX_Intro.htm">SAX</link> + (push)</para> + </listitem> + + <listitem> + <para><link + xlink:href="https://docs.oracle.com/javase/tutorial/jaxp/stax/why.html">STAX</link> + (pull)</para> + </listitem> + </itemizedlist></td> + + <td valign="top"><itemizedlist> + <listitem> + <para><link + xlink:href="http://tutorials.jenkov.com/java-xml/dom.html">DOM</link></para> + </listitem> + + <listitem> + <para><link xlink:href="http://www.jdom.org">JDOM</link></para> + </listitem> + + <listitem> + <para><link + xlink:href="https://docs.oracle.com/javase/tutorial/jaxb/intro">JAXB</link></para> + </listitem> + </itemizedlist></td> + </tr> + </informaltable> + </figure> + <section xml:id="domBase"> <title>Language independent specification</title> <titleabbrev>Language independence</titleabbrev> - <para>XML documents allow for automated content processing. We already - discussed the <acronym - xlink:href="http://www.saxproject.org">SAX</acronym> API to access XML - documents by <xref linkend="glo_Java"/> applications. There are however - situations where <acronym + <para>XML documents allow for automated content processing. The <acronym + xlink:href="http://www.saxproject.org">SAX</acronym> API allows for + accessing <xref linkend="glo_XML"/> documents by <xref + linkend="glo_Java"/> applications in an event based fashion. There are + however situations where <acronym xlink:href="http://www.saxproject.org">SAX</acronym> is not appropriate:</para> @@ -74,9 +132,9 @@ </itemizedlist> <para>The greatest deficiency of the <acronym - xlink:href="http://www.saxproject.org">SAX</acronym> is the fact that an - XML instance is not represented as a tree like structure but as a - succession of events. The <acronym + xlink:href="http://www.saxproject.org">SAX</acronym> is its lack of + providing just an event sequence rather than an in-memory tree like + representation. The <acronym xlink:href="https://www.w3.org/DOM">DOM</acronym> allows us to represent XML document instances as tree like structures and thus enables navigational operations between nodes.</para> @@ -509,23 +567,20 @@ printer.output(titel, System.out); // Serialize to console</programlist xlink:href="https://www.w3.org/DOM">DOM</acronym> based <xref linkend="glo_XML"/> parsing application:.</para> - <figure xml:id="domTreeTraversal"> + <figure xml:id="sda1_dom_fig_TreeTraversal"> <title>Accessing a XML Tree purely by <acronym xlink:href="https://www.w3.org/DOM">DOM</acronym> methods.</title> <programlisting language="java">public class ReadCatalog { - // Though we are playing DOM here, a SAX parser still assembles our DOM tree. - private SAXBuilder builder = new SAXBuilder(); + private SAXBuilder builder = new SAXBuilder(); // The workhorse - /** - * Initialize for parsing. - */ public ReadCatalog() { // Though an ErrorHandler is not strictly required it allows // for easier localization of XML document errors builder.setErrorHandler(new MySaxErrorHandler(System.out)); <co - linkends="domSetSaxErrorHandler-co" xml:id="domSetSaxErrorHandler"/> + linkends="sda1_dom_fig_TreeTraversal-1" + xml:id="sda1_dom_fig_TreeTraversal-1-co"/> } /** Descending a catalog till its &lt;item&gt; elements. For each product @@ -534,26 +589,68 @@ printer.output(titel, System.out); // Serialize to console</programlist * @param filename The catalog's filename to be parsed * */ - public void process(final String filename) throws JDOMException, IOException { + public void process(final String filename) <co + linkends="sda1_dom_fig_TreeTraversal-2" + xml:id="sda1_dom_fig_TreeTraversal-2-co"/> throws JDOMException <co + linkends="sda1_dom_fig_TreeTraversal-3" + xml:id="sda1_dom_fig_TreeTraversal-3-co"/>, IOException { // Parsing our XML file being located below "resources" final Document docInput = builder.build( - getClass().getClassLoader().getResource(filename) + getClass().getClassLoader().getResource(filename) <co + linkends="sda1_dom_fig_TreeTraversal-4" + xml:id="sda1_dom_fig_TreeTraversal-4-co"/> ); // Accessing the document's root element <catalog> - final Element docRoot = docInput.getRootElement(); + final Element docRoot = docInput.getRootElement(); <co + linkends="sda1_dom_fig_TreeTraversal-5" + xml:id="sda1_dom_fig_TreeTraversal-5-co"/> // Accessing the <item> children of parent element <catalog> - docRoot.getChildren().forEach(item -> + docRoot.getChildren().forEach(item -> <co + linkends="sda1_dom_fig_TreeTraversal-6" + xml:id="sda1_dom_fig_TreeTraversal-6-co"/> System.out.println( "Article: " + item.getText() + ", order number: " + item.getAttributeValue("orderNo"))); } }</programlisting> - <para>Note <coref linkend="domSetSaxErrorHandler" - xml:id="domSetSaxErrorHandler-co"/> for error handling.</para> + <calloutlist> + <callout arearefs="sda1_dom_fig_TreeTraversal-1-co" + xml:id="sda1_dom_fig_TreeTraversal-1"> + <para>Though an <classname + xlink:href="https://docs.oracle.com/javase/9/docs/api/org/xml/sax/ErrorHandler.html">ErrorHandler</classname> + is not strictly being required it allows for easier localization of + XML document errors</para> + </callout> + + <callout arearefs="sda1_dom_fig_TreeTraversal-2-co" + xml:id="sda1_dom_fig_TreeTraversal-2"> + <para/> + </callout> + + <callout arearefs="sda1_dom_fig_TreeTraversal-3-co" + xml:id="sda1_dom_fig_TreeTraversal-3"> + <para/> + </callout> + + <callout arearefs="sda1_dom_fig_TreeTraversal-4-co" + xml:id="sda1_dom_fig_TreeTraversal-4"> + <para/> + </callout> + + <callout arearefs="sda1_dom_fig_TreeTraversal-5-co" + xml:id="sda1_dom_fig_TreeTraversal-5"> + <para/> + </callout> + + <callout arearefs="sda1_dom_fig_TreeTraversal-6-co" + xml:id="sda1_dom_fig_TreeTraversal-6"> + <para/> + </callout> + </calloutlist> </figure> <para>Executing this method needs a driver instance providing an <xref @@ -839,8 +936,8 @@ Document contains 15 elements and 3 attributes.</screen> <question> <para>Instead of transforming our <link linkend="simpleCatalog">simple catalog</link> into textual - output in <xref linkend="domTreeTraversal"/> we may also create - <xref linkend="glo_XHTML"/> pages like:</para> + output in <xref linkend="sda1_dom_fig_TreeTraversal"/> we may + also create <xref linkend="glo_XHTML"/> pages like:</para> <programlisting language="xml"><!DOCTYPE html> <!-- Static content section--> <co @@ -874,8 +971,9 @@ Document contains 15 elements and 3 attributes.</screen> <para>Rather then just coding<code>...println(<html>\n\t<head>...)</code> statements you are expected to implement a more sophisticated - solution: We may combine <xref linkend="domTreeTraversal"/> and - <xref linkend="createDocModify"/>. The idea is parsing the <link + solution: We may combine <xref + linkend="sda1_dom_fig_TreeTraversal"/> and <xref + linkend="createDocModify"/>. The idea is parsing the <link linkend="simpleCatalog">XML catalog instance</link> to a <xref linkend="glo_Java"/> <acronym xlink:href="https://www.w3.org/DOM">DOM</acronym> object as @@ -1359,8 +1457,8 @@ public class Article2Html { <itemizedlist> <listitem> - <para><xref linkend="domTreeTraversal"/> effectively limited to - simple hierarchies.</para> + <para><xref linkend="sda1_dom_fig_TreeTraversal"/> effectively + limited to simple hierarchies.</para> </listitem> <listitem> -- GitLab