Headline

In-memory XML processing in Language:Java with Technology:DOM

Characteristics

Companies are represented in Language:XML and the object model of Technology:DOM is used to represent and process XML documents in memory. In particular, operations on companies are implemented in Java on top of DOM objects. Such objects are easily queried - as needed for Feature:Total. As DOM objects also mutable, Feature:Cut is implemented as an impure function.

Illustration

The data model is implemented as xml files (sampleCompany.xml) that conform to a schema (Company.xsd).

Feature:Open serialization is implemented by loading and storing a Technology:DOM Document object:

	/**
	 * Parse a file and return the Document object
	 */
	public static Document loadDocument(String filename) {
		try {
			// Create a builder factory
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();

			// Create the builder
			DocumentBuilder builder = factory.newDocumentBuilder();

			// Deserialization by parsing
			Document doc = builder.parse(new File(filename));

			// Done
			return doc;

		} catch (SAXException e) {
		} catch (ParserConfigurationException e) {
		} catch (IOException e) {
		}
		// Return null for any sort of problem
		return null;
	}
	/**
	 * Save a document to a file.
	 */
	public static boolean saveDocument(Document doc, String filename) {

		try {
			// Prepare the DOM document for writing
			Source source = new DOMSource(doc);

			// Prepare the output file
			File file = new File(filename);
			Result result = new StreamResult(file);

			// Creater a transformer factory
			TransformerFactory xfactory = TransformerFactory.newInstance();

			// Create a transformer
			Transformer transformer = xfactory.newTransformer();

			// Force pretty printing
			transformer.setOutputProperty(OutputKeys.METHOD, "xml");
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			transformer.setOutputProperty(
					"{http://xml.apache.org/xslt}indent-amount", "2");
			transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

			// Serialization by transformation
			transformer.transform(source, result);

			// Done
			return true;

		} catch (TransformerConfigurationException e) {
		} catch (TransformerException e) {
		}
		// Return false for any sort of problem
		return false;
	}

A method to validate the deserialized xml with the Company XSD schema is implemented by a DOM Validator:

	/**
	 * return true if document is a valid company
	 * 
	 */
	public static boolean isValidXml(String xmlFile, String xsdFile) {
		Schema schema = null;
		// Create a builder factory
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

		factory.setNamespaceAware(true);

		// Create a factory for validation
		String language = XMLConstants.W3C_XML_SCHEMA_NS_URI;
		SchemaFactory schemaFactory = SchemaFactory.newInstance(language);

		try {
			// Create schema from xsd file
			schema = schemaFactory.newSchema(new File(xsdFile));
			// Create validator
			Validator validator = schema.newValidator();
			// Create DocumentBuilder
			DocumentBuilder builder = factory.newDocumentBuilder();
			// Create document from xml file
			Document doc = builder.parse(new File(xmlFile));
			// validate xml file with xsd schema
			validator.validate(new DOMSource(doc));
			return true;
		} catch (SAXException e) {
			System.err.println(e);
		} catch (IOException e) {
			System.err.println(e);
		} catch (ParserConfigurationException e) {
			System.err.println(e);
		}
		return false;
	}

Feature:Total and Feature:Cut are implemented using methods of Technology:DOM's Document class:

    public static double total(Document doc) {
        // The aggregation variable 
        double total = 0;

        // Get the matching elements
        NodeList nodelist = doc.getElementsByTagName("salary");

        // Process the elements in the nodelist
        for (int i=0; i<nodelist.getLength(); i++) {
            // Get element
            Element elem = (Element)nodelist.item(i);
            total += Double.parseDouble(elem.getTextContent());
        }
        return total;
    }
    public static void cut(Document doc) {
        // Get the matching elements
        NodeList nodelist = doc.getElementsByTagName("salary");

        // Process the elements in the nodelist
        for (int i=0; i<nodelist.getLength(); i++) {

            // Get element
            Element elem = (Element)nodelist.item(i);

            // Transform content of element
            double value = parseDouble(elem.getTextContent());
            elem.setTextContent(Double.toString(value / 2));
        }
    }

Test cases are implemented for all Namespace:Features.

Relationships

For DOM-like implementations of in-memory XML processing see Contribution:dom, Contribution:jdom and Contribution:xom.

For a query-based implementation of in-memory XML processing see Contribution:xpathAPI.

For push-based XML processing see Contribution:sax.

For Object/XML mapping see Contribution:jaxbChoice (XSD with choice for different subunits), Contribution:jaxbComposition (XSD with object composition), Contribution:jaxbExtension (XSD with type extension) and Contribution:jaxbSubstitution (XSD with substitution groups).

Architecture

The contribution follows a standardized structure:

  • inputs contains input files for tests
  • src/main/java contains the following packages:
  • src/test/java contains the following packages:

Usage

This contribution uses Technology:Gradle for building. Technology:Eclipse is supported.

See https://github.com/101companies/101simplejava/blob/master/README.md


There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.