Skip to content
Andreas Rudolph edited this page Aug 16, 2021 · 19 revisions

How to use ImmoXML format

Reading XML in ImmoXML format

The class org.openestate.io.immoxml.ImmoXmlUtils provides a static createDocument() function to read XML data in ImmoXML format from a java.io.File, java.io.InputStream, java.lang.String or org.w3c.dom.Document into a org.openestate.io.immoxml.ImmoXmlDocument.

import java.io.File;
import org.openestate.io.immoxml.ImmoXmlDocument;
import org.openestate.io.immoxml.ImmoXmlUtils;
import org.openestate.io.immoxml.xml.Anbieter;
import org.openestate.io.immoxml.xml.Immobilie;
import org.openestate.io.immoxml.xml.Immoxml;

public class ImmoXmlReadingExample {
    public static void main(String[] args) throws Exception {
        if (args.length < 1) {
            System.err.println("No file was specified!");
            System.exit(1);
        }

        // read file into a ImmoXmlDocument
        ImmoXmlDocument doc = ImmoXmlUtils.createDocument(new File(args[0]));

        // convert ImmoXmlDocument into a Java object
        Immoxml immoxml = doc.toObject();

        // now we can access the XML content through ordinary Java objects
        for (Anbieter anbieter : immoxml.getAnbieter()) {
            System.out.println("found agency '" + anbieter.getAnbieternr() + "'");

            // process real estates of the agency
            for (Immobilie immobilie : anbieter.getImmobilie()) {
                // get object nr
                String objectNr = (immobilie.getVerwaltungTechn() != null) ?
                        immobilie.getVerwaltungTechn().getObjektnrExtern() :
                        null;

                // get object title
                String objectTitle = (immobilie.getFreitexte() != null) ?
                        immobilie.getFreitexte().getObjekttitel() :
                        null;

                // print object information to console
                System.out.println("> found object " +
                        "'" + objectNr + "': " + objectTitle);
            }
        }
    }
}

See a full example at ImmoXmlReadingExample.java.

Accessing XML data in ImmoXML format

The class org.openestate.io.immoxml.xml.Immoxml is equivalent to a <immoxml> root element in a ImmoXML document. For example the following code creates a ImmoXML document programmatically:

import com.thedeanda.lorem.Lorem;
import com.thedeanda.lorem.LoremIpsum;
import java.math.BigDecimal;
import java.util.Locale;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.openestate.io.immoxml.ImmoXmlUtils;
import org.openestate.io.immoxml.xml.Aktion;
import org.openestate.io.immoxml.xml.Anbieter;
import org.openestate.io.immoxml.xml.Anhang;
import org.openestate.io.immoxml.xml.Haus;
import org.openestate.io.immoxml.xml.Immobilie;
import org.openestate.io.immoxml.xml.Immoxml;
import org.openestate.io.immoxml.xml.ObjectFactory;
import org.openestate.io.immoxml.xml.Uebertragung;

public class ImmoXmlWritingExample {
    private final static ObjectFactory FACTORY = ImmoXmlUtils.getFactory();
    private final static Lorem RANDOMIZER = new LoremIpsum();

    public static void main(String[] args) {
        // create an Immoxml object with some example data
        // this object corresponds to the <immoxml> root element in XML
        Immoxml immoxml = FACTORY.createImmoxml();
        immoxml.setUebertragung(createUebertragung());

        int anbieterCount = RandomUtils.nextInt(1, 5);
        for (int i = 0; i < anbieterCount; i++) {
            immoxml.getAnbieter().add(createAnbieter());
        }

        // now make something useful with the object
    }

    /**
     * Create an {@link Anbieter} with some example data.
     *
     * @return created example object
     */
    private static Anbieter createAnbieter() {
        // create an example agency
        Anbieter anbieter = FACTORY.createAnbieter();
        anbieter.setAnbieternr(RandomStringUtils.randomAlphanumeric(5));
        anbieter.setFirma(RANDOMIZER.getName());
        anbieter.setImmoxmlAnid(RandomStringUtils.randomAlphanumeric(5));

        // add some real estates to the agency
        int immobilieCount = RandomUtils.nextInt(1, 6);
        for (int i = 0; i < immobilieCount; i++) {
            anbieter.getImmobilie().add(createImmobilie());
        }

        return anbieter;
    }

    /**
     * Create an {@link Immobilie} with some example data.
     *
     * @return created example object
     */
    private static Immobilie createImmobilie() {
        // create an example real estate
        Immobilie immobilie = FACTORY.createImmobilie();

        // add some administrative information
        immobilie.setVerwaltungTechn(FACTORY.createVerwaltungTechn());
        immobilie.getVerwaltungTechn().setAktion(FACTORY.createAktion());
        immobilie.getVerwaltungTechn().getAktion().setAktionart(Aktion.AktionArt.CHANGE);
        immobilie.getVerwaltungTechn().setObjektnrIntern(RandomStringUtils.randomNumeric(10));

        // set categorization
        immobilie.setObjektkategorie(FACTORY.createObjektkategorie());
        immobilie.getObjektkategorie().setNutzungsart(FACTORY.createNutzungsart());
        immobilie.getObjektkategorie().getNutzungsart().setANLAGE(RandomUtils.nextBoolean());
        immobilie.getObjektkategorie().getNutzungsart().setGEWERBE(RandomUtils.nextBoolean());
        immobilie.getObjektkategorie().getNutzungsart().setWAZ(RandomUtils.nextBoolean());
        immobilie.getObjektkategorie().getNutzungsart().setWOHNEN(RandomUtils.nextBoolean());
        immobilie.getObjektkategorie().setVermarktungsart(FACTORY.createVermarktungsart());
        immobilie.getObjektkategorie().getVermarktungsart().setERBPACHT(RandomUtils.nextBoolean());
        immobilie.getObjektkategorie().getVermarktungsart().setKAUF(RandomUtils.nextBoolean());
        immobilie.getObjektkategorie().getVermarktungsart().setLEASING(RandomUtils.nextBoolean());
        immobilie.getObjektkategorie().getVermarktungsart().setMIETEPACHT(RandomUtils.nextBoolean());
        immobilie.getObjektkategorie().setObjektart(FACTORY.createObjektart());

        Haus singleFamilyHouse = FACTORY.createHaus();
        singleFamilyHouse.setHaustyp(randomValue(Haus.Haustyp.values()));
        immobilie.getObjektkategorie().getObjektart().getHaus().add(singleFamilyHouse);

        // add some information about the location
        immobilie.setGeo(FACTORY.createGeo());
        immobilie.getGeo().setPlz(RANDOMIZER.getZipCode());
        immobilie.getGeo().setOrt(RANDOMIZER.getCity());
        immobilie.getGeo().setLand(FACTORY.createLand());
        immobilie.getGeo().getLand().setIsoLand(Locale.GERMANY.getISO3Country());

        // add some information about prices
        immobilie.setPreise(FACTORY.createPreise());
        immobilie.getPreise().setHeizkosten(BigDecimal.valueOf(RandomUtils.nextDouble(100, 1000)));
        immobilie.getPreise().setKaufpreis(BigDecimal.valueOf(RandomUtils.nextDouble(10000, 999999)));

        // add some information about features
        immobilie.setAusstattung(FACTORY.createAusstattung());
        immobilie.getAusstattung().setGartennutzung(RandomUtils.nextBoolean());
        immobilie.getAusstattung().setHeizungsart(FACTORY.createHeizungsart());
        immobilie.getAusstattung().getHeizungsart().setZENTRAL(RandomUtils.nextBoolean());
        immobilie.getAusstattung().getHeizungsart().setFUSSBODEN(RandomUtils.nextBoolean());

        // add some descriptions
        immobilie.setFreitexte(FACTORY.createFreitexte());
        immobilie.getFreitexte().setObjekttitel(RANDOMIZER.getWords(1, 10));
        immobilie.getFreitexte().setObjektbeschreibung(RANDOMIZER.getWords(10, 50));

        // set the contact person
        immobilie.setKontaktperson(FACTORY.createKontaktperson());
        immobilie.getKontaktperson().setName(RANDOMIZER.getName());
        immobilie.getKontaktperson().setEmailDirekt(RANDOMIZER.getEmail());
        immobilie.getKontaktperson().setTelDurchw(RANDOMIZER.getPhone());
        immobilie.getKontaktperson().setPlz(RANDOMIZER.getZipCode());
        immobilie.getKontaktperson().setOrt(RANDOMIZER.getCity());
        immobilie.getKontaktperson().setLand(FACTORY.createLand());
        immobilie.getKontaktperson().getLand().setIsoLand(Locale.GERMANY.getISO3Country());

        // add some attachments
        immobilie.setAnhaenge(FACTORY.createAnhaenge());
        int attachmentCount = RandomUtils.nextInt(3, 10);
        for (int i = 0; i < attachmentCount; i++) {
            immobilie.getAnhaenge().getAnhang().add(createAnhang());
        }

        return immobilie;
    }

    /**
     * Create an {@link Anhang} with some example data.
     *
     * @return created example object
     */
    private static Anhang createAnhang() {
        // create an example transfer
        Anhang anhang = FACTORY.createAnhang();
        anhang.setAnhangtitel(RANDOMIZER.getWords(2, 5));
        anhang.setLocation(Anhang.Location.EXTERN);
        anhang.setDaten(FACTORY.createDaten());
        anhang.getDaten().setPfad("image" + RandomStringUtils.randomAlphabetic(3) + ".jpg");
        return anhang;
    }

    /**
     * Create an {@link Uebertragung} with some example data.
     *
     * @return created example object
     */
    private static Uebertragung createUebertragung() {
        // create an example transfer
        Uebertragung uebertragung = FACTORY.createUebertragung();
        uebertragung.setArt(randomValue(Uebertragung.Art.values()));
        uebertragung.setSendersoftware(RANDOMIZER.getName());
        uebertragung.setTechnEmail(RANDOMIZER.getEmail());
        uebertragung.setUmfang(randomValue(Uebertragung.Umfang.values()));
        return uebertragung;
    }

    /**
     * Get a random value from an array.
     *
     * @param values array containing values to select from
     * @param <T>    type of contained values
     * @return randomly selected value
     */
    private static <T> T randomValue(T[] values) {
        return (values != null && values.length > 0) ?
                values[RandomUtils.nextInt(0, values.length)] :
                null;
    }
}

See a full example at ImmoXmlWritingExample.java.

Writing XML in ImmoXML format

After a org.openestate.io.immoxml.xml.Immoxml object was created, it can be converted into a org.openestate.io.immoxml.ImmoXmlDocument with the static newDocument() function.

The class org.openestate.io.immoxml.ImmoXmlDocument provides a toXml() function, that finally writes the contents of the Immoxml object as XML into a java.io.File, java.io.OutputStream or java.io.Writer.

import java.io.File;
import java.io.OutputStream;
import java.io.Writer;
import org.openestate.io.immoxml.ImmoXmlDocument;
import org.openestate.io.immoxml.xml.Immoxml;

public class ImmoXmlWritingExample {
    private final static boolean PRETTY_PRINT = true;

    /**
     * Convert a {@link Immoxml} to XML and write it into a {@link File}.
     *
     * @param immoxml Java object representing the XML root element
     * @param file    the file, where the document is written to
     * @throws Exception if the document can't be written
     */
    private static void write(Immoxml immoxml, File file) throws Exception {
        ImmoXmlDocument
                .newDocument(immoxml)
                .toXml(file, PRETTY_PRINT);
    }

    /**
     * Convert a {@link Immoxml} to XML and write it into an {@link OutputStream}.
     *
     * @param immoxml Java object representing the XML root element
     * @param output  the stream, where the document is written to
     * @throws Exception if the document can't be written
     */
    private static void write(Immoxml immoxml, OutputStream output) throws Exception {
        ImmoXmlDocument
                .newDocument(immoxml)
                .toXml(output, PRETTY_PRINT);
    }

    /**
     * Convert a {@link Immoxml} to XML and write it into a {@link Writer}.
     *
     * @param immoxml Java object representing the XML root element
     * @param output  the writer, where the document is written to
     * @throws Exception if the document can't be written
     */
    private static void write(Immoxml immoxml, Writer output) throws Exception {
        ImmoXmlDocument
                .newDocument(immoxml)
                .toXml(output, PRETTY_PRINT);
    }

    /**
     * Convert a {@link Immoxml} to XML and print the results to the console.
     *
     * @param immoxml Java object representing the XML root element
     * @throws Exception if the document can't be written
     */
    private static void writeToConsole(Immoxml immoxml) throws Exception {
        System.out.println(
            ImmoXmlDocument
                .newDocument(immoxml)
                .toXmlString(PRETTY_PRINT)
        );
    }
}

See a full example at ImmoXmlWritingExample.java.

Clone this wiki locally