fitsena
fitsena

Reputation: 65

How to use XML for data communication

I have a task of transforming an existing ASCII format of data communication of a simulation tool to an XML format. The XML or previous ASCII data contain database information and other parameters which the server needs for calculation of a task.

Using JAXB , I managed to populate the required datas. And when I marshal it, I can see an XML structure as defined by the schema.

item item1 = new item();

Parameter par1 = new Par1();
par1.setName("DatabaseNr);
par1.setContent("1500");

------Marshalling---------

Marshaller mar = context.createMarshaller();
mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

----Printing----

<Item >
    <Parameter Name="databaseNr">1500</Parameter>
    <Parameter Name="pressure"></Parameter>
    <Parameter Name="time"></Parameter>
</Item>

My question is how could I locate the most important information like database number out of the very large XML for furthur steps? Is this where I need the parsing? Thanks for your answers

Upvotes: 2

Views: 246

Answers (1)

bdoughan
bdoughan

Reputation: 148977

You appear to be using your JAXB (JSR-222) implementation with a very generic object structure. It would be easier to navigate if you had a more domain specific model such as the following:

Item

package forum11100410;

import java.util.Date;
import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlType(propOrder={"databaseNr", "time", "pressure"})
public class Item {

    private int databaseNr;
    private int pressure;
    private Date time;

    public int getDatabaseNr() {
        return databaseNr;
    }

    public void setDatabaseNr(int databaseNr) {
        this.databaseNr = databaseNr;
    }

    public int getPressure() {
        return pressure;
    }

    public void setPressure(int pressure) {
        this.pressure = pressure;
    }

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

}

Demo

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<item>
    <databaseNr>1500</databaseNr>
    <pressure>1</pressure>
    <time>2012-06-19T09:16:35.666-04:00</time>
</item>

Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<item>
    <databaseNr>1500</databaseNr>
    <pressure>1</pressure>
    <time>2012-06-19T09:16:35.666-04:00</time>
</item>

Upvotes: 1

Related Questions