Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #35309

Re: XML

From Arne Vajhøj <arne@vajhoej.dk>
Newsgroups comp.lang.java.programmer
Subject Re: XML
Date 2016-08-13 15:38 -0400
Organization Aioe.org NNTP Server
Message-ID <nonsuv$425$1@gioia.aioe.org> (permalink)
References (8 earlier) <487a50aa-8e4b-460a-9824-bd23c26d4c02@googlegroups.com> <nodg3g$elh$1@dont-email.me> <de36e9b4-44de-44ba-835d-80e9dc80fa90@googlegroups.com> <nofttr$u80$1@dont-email.me> <00fae8a8-dc01-435f-9f33-e5a5055d93df@googlegroups.com>

Show all headers | View raw


On 8/10/2016 4:29 PM, Eric Douglas wrote:
> Now, I'm not sure the best API to manage records.  As the only method
> I found for storing multiple records with JAXB is a List of the
> DataRecord instances, how do I pass it a key value, get one or more
> of these record objects, update a field (or fields), and write it
> back out?  This RMFID.xsd maps to an RMFID Java class which gives me
> instances specific to this file name where I can call it's
> setFILENAME(), getFILENAME() methods, but I don't see an obvious way
> to get specific instances of the RMFID from the list aside from
> customizing these classes further to map out what keys I need and
> mapping them through the DataFile class. I'm wondering if it makes
> sense to keep going down this path or try something else, like JiBX
> which claims to do all this marshall/unmarshall stuff plus add code
> to the Java classes to make everything more efficient and support
> XPath queries to find records?

Here is a complete example showing some stuff.

First we have the data model:

package jaxbdemo;

public abstract class Data {
	private int iv;
	public Data() {
		this(0);
	}
	public Data(int iv) {
		super();
		this.iv = iv;
	}
	public int getIv() {
		return iv;
	}
	public void setIv(int iv) {
		this.iv = iv;
	}
}

package jaxbdemo;

public class DataA extends Data {
	private String sv;
	public DataA() {
		this(0, "");
	}
	public DataA(int iv, String sv) {
		super(iv);
		this.sv = sv;
	}
	public String getSv() {
		return sv;
	}
	public void setSv(String sv) {
		this.sv = sv;
	}
	@Override
	public String toString() {
		return String.format("{ %d, %s }", getIv(), getSv());
	}
}

package jaxbdemo;

public class DataB extends Data {
	private double xv;
	public DataB() {
		this(0, 0.0);
	}
	public DataB(int iv, double xv) {
		super(iv);
		this.xv = xv;
	}
	public double getXv() {
		return xv;
	}
	public void setXv(double xv) {
		this.xv = xv;
	}
	@Override
	public String toString() {
		return String.format("{ %d, %f }", getIv(), getXv());
	}
}

Completely standard.

Now we create a wrapper class handling all of the JAXB:

package jaxbdemo;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="list")
public class DataContainer {
     private List<Data> list;
     public DataContainer() {
     	list = new ArrayList<>();
     }
     @XmlElements( { @XmlElement(name="a", type=DataA.class), 
@XmlElement(name="b", type=DataB.class) } )
     public List<Data> getList(){
     	return list;
     }
}

Now for usage.

First setup:

		// JAXB
		JAXBContext jxbctx = JAXBContext.newInstance(DataContainer.class);
		Marshaller m = jxbctx.createMarshaller();
		Unmarshaller um = jxbctx.createUnmarshaller();

Let us try write a file:

		DataContainer data = new DataContainer();
		data.getList().add(new DataA(123, "ABC"));
		data.getList().add(new DataB(456, 123.456));
		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
		try(OutputStream os = new FileOutputStream("/work/demo.xml")) {
			m.marshal(data, os);
		}

demo.xml looks like:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<list>
     <a>
         <iv>123</iv>
         <sv>ABC</sv>
     </a>
     <b>
         <iv>456</iv>
         <xv>123.456</xv>
     </b>
</list>

which I consider to be "nice" XML.

Reading the file again is simple:

		// load
		DataContainer data2 = null;
		try(InputStream is = new FileInputStream("/work/demo.xml")) {
			data2 = (DataContainer)um.unmarshal(is);
		}

Searching for various data is easy using the new Java 8 streams:

		// search
		Data o1 = data2.getList().stream().filter(item -> item.getIv() == 
456).findFirst().get();
		Data o2 = data2.getList().stream().filter(item -> item instanceof 
DataA && ((DataA)item).getSv().equals("ABC")).findFirst().get();

Objects can be update:

		// update
		o1.setIv(789);
		((DataA)o2).setSv("XXX");

And when written to another file:

		// save again
		try(OutputStream os = new FileOutputStream("/work/demo2.xml")) {
			m.marshal(data2, os);
		}

then demo2.xml has the expected content:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<list>
     <a>
         <iv>123</iv>
         <sv>XXX</sv>
     </a>
     <b>
         <iv>789</iv>
         <xv>123.456</xv>
     </b>
</list>

Arne

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

XML Eric Douglas <e.d.programmer@gmail.com> - 2016-07-29 13:25 -0700
  Re: XML Arne Vajhøj <arne@vajhoej.dk> - 2016-07-29 19:46 -0400
  Re: XML Roedy Green <inquiry@mindprod.com> - 2016-07-30 17:09 -0700
    Re: XML Arne Vajhøj <arne@vajhoej.dk> - 2016-07-30 21:49 -0400
      Re: XML Arne Vajhøj <arne@vajhoej.dk> - 2016-07-30 22:35 -0400
      Re: XML Roedy Green <inquiry@mindprod.com> - 2016-07-31 21:39 -0700
        Re: XML Eric Douglas <e.d.programmer@gmail.com> - 2016-08-01 05:19 -0700
          Re: XML Arne Vajhøj <arne@vajhoej.dk> - 2016-08-01 08:51 -0400
            Re: XML Eric Douglas <e.d.programmer@gmail.com> - 2016-08-05 06:07 -0700
              Re: XML Eric Douglas <e.d.programmer@gmail.com> - 2016-08-05 10:17 -0700
              Re: XML Stanimir Stamenkov <s7an10@netscape.net> - 2016-08-07 19:04 +0300
                Re: XML Eric Douglas <e.d.programmer@gmail.com> - 2016-08-08 05:38 -0700
                Re: XML Eric Douglas <e.d.programmer@gmail.com> - 2016-08-08 07:19 -0700
                Re: XML Stanimir Stamenkov <s7an10@netscape.net> - 2016-08-09 23:57 +0300
                Re: XML Eric Douglas <e.d.programmer@gmail.com> - 2016-08-10 05:37 -0700
                Re: XML Eric Douglas <e.d.programmer@gmail.com> - 2016-08-10 10:39 -0700
                Re: XML Stanimir Stamenkov <s7an10@netscape.net> - 2016-08-10 22:05 +0300
                Re: XML Eric Douglas <e.d.programmer@gmail.com> - 2016-08-10 13:29 -0700
                Re: XML Stanimir Stamenkov <s7an10@netscape.net> - 2016-08-11 02:18 +0300
                Re: XML Eric Douglas <e.d.programmer@gmail.com> - 2016-08-11 05:15 -0700
                Re: XML Eric Douglas <e.d.programmer@gmail.com> - 2016-08-11 05:50 -0700
                Re: XML Eric Douglas <e.d.programmer@gmail.com> - 2016-08-11 12:24 -0700
                Re: XML Arne Vajhøj <arne@vajhoej.dk> - 2016-08-13 15:38 -0400
                Re: XML Eric Douglas <e.d.programmer@gmail.com> - 2016-08-16 11:49 -0700
                Re: XML Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-08-16 15:57 -0400
                Re: XML Eric Douglas <e.d.programmer@gmail.com> - 2016-08-16 13:20 -0700
                Re: XML Arne Vajhøj <arne@vajhoej.dk> - 2016-08-16 17:04 -0400
                Re: XML Arne Vajhøj <arne@vajhoej.dk> - 2016-08-16 21:37 -0400
                Re: XML Eric Douglas <e.d.programmer@gmail.com> - 2016-08-17 05:22 -0700
                Re: XML Eric Douglas <e.d.programmer@gmail.com> - 2016-08-17 05:21 -0700
                Re: XML Arne Vajhøj <arne@vajhoej.dk> - 2016-08-16 16:13 -0400
                Re: XML Eric Douglas <e.d.programmer@gmail.com> - 2016-08-16 13:22 -0700
                Re: XML Arne Vajhøj <arne@vajhoej.dk> - 2016-08-16 16:53 -0400
                Re: XML Eric Douglas <e.d.programmer@gmail.com> - 2016-08-16 13:32 -0700
                Re: XML Arne Vajhøj <arne@vajhoej.dk> - 2016-08-16 16:47 -0400
                Re: XML Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-08-16 16:59 -0400
        Re: XML Arne Vajhøj <arne@vajhoej.dk> - 2016-08-01 08:46 -0400

csiph-web