German English
Data Processing

XML Generation from sensor values

This examples shows how to transform sensor values to a XML (eXtensible Markup Language) description. The implementation demonstrates the performance of the radio module Preon32. The library "qdparser" is used to generate the XML description. Originally, this library was developed for mobile phones with Java ME but can also be used for the Preon32 without adjustment.
/*
 * Copyright (c) 2011., Virtenio GmbH
 * All rights reserved.
 */

package com.virtenio.webdemo;

import com.twicom.qdparser.TaggedElement;

public class XmlGenerate {

    public static void main(String[] args) {
        // various sensor readings
        int humidityRHr = 16196;    // relative humidity from SHT21
        int humidityTr = 30332;     // temperature from SHT21
        int pressurePr = 341;       // pressure from MPL115A2
        int pressureTr = 453;       // temperature from MPL115A2
        int lightRaw = 4041;        // light from BH1715FVC
        int tempTr = 4400;          // temperature from ADT7410

        String strXml = generateXmlDescription(humidityRHr, humidityTr,
                                               pressureTr, pressurePr,
                                               lightRaw,
                                               tempTr);
        System.out.println(strXml);
    }

    private static String generateXmlDescription(
            int humidityRHr, int humidityTr,
            int pressureTr, int pressurePr,
            int lightRaw,
            int tempTr) {

        TaggedElement message = new TaggedElement("varisenA");
        String[][] tags = new String[][]{
                new String[]{"humidity", "RHr", "" + humidityRHr, "Tr", "" + humidityTr}
                , new String[]{"pressure", "Tr", "" + pressureTr, "Pr", "" + pressurePr}
                , new String[]{"light", "raw", "" + lightRaw}
                , new String[]{"temp", "Tr", "" + tempTr}
        };
        for (String[] tag : tags) {
            addXmlTag(message, tag);
        }
        return message.toString();
    }

    private static void addXmlTag(TaggedElement message, String[] tag_addr) {
        TaggedElement tag = new TaggedElement(tag_addr[0]);
        for (int i = 1; i < tag_addr.length; i = i + 2) {
            tag.setAttribute(tag_addr[i], tag_addr[i + 1]);
        }
        message.add(tag);
    }
}