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


Groups > comp.lang.java.programmer > #35149 > unrolled thread

XML

Started byEric Douglas <e.d.programmer@gmail.com>
First post2016-07-29 13:25 -0700
Last post2016-08-01 08:46 -0400
Articles 17 on this page of 37 — 5 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  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

Page 2 of 2 — ← Prev page 1 [2]


#35256

FromEric Douglas <e.d.programmer@gmail.com>
Date2016-08-11 05:50 -0700
Message-ID<f2323348-1d30-4a73-88f3-6417e2ed19a8@googlegroups.com>
In reply to#35255
On Thursday, August 11, 2016 at 8:15:28 AM UTC-4, Eric Douglas wrote:
> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
> <xs:schema elementFormDefault="unqualified" version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
> 
>   <xs:element name="rmfid" type="rmfid"/>
> 
>   <xs:complexType name="rmfid">
>     <xs:complexContent>
>       <xs:extension base="dataRecord">
>         <xs:sequence>
>           <xs:element name="FILENAME" type="xs:string" minOccurs="0"/>
>           <xs:element name="SEQNUM" type="xs:string" minOccurs="0"/>
>         </xs:sequence>
>       </xs:extension>
>     </xs:complexContent>
>   </xs:complexType>
> 
>   <xs:complexType name="dataRecord" abstract="true">
>     <xs:sequence/>
>   </xs:complexType>
> </xs:schema>
> 

I forgot to also pass in DataFile.class which is the actual root to the file.  That's a bit different story on this generated code.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema elementFormDefault="unqualified" version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="DataFile" type="dataFile"/>

  <xs:element name="rmfid" type="rmfid"/>

  <xs:complexType name="rmfid">
    <xs:sequence>
      <xs:element name="FILENAME" type="xs:string" minOccurs="0"/>
      <xs:element name="SEQNUM" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="dataFile">
    <xs:sequence>
      <xs:element name="DataRecord" type="xs:anyType" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

Obvious problem besides previously mentioned is the generated code writes <xs:sequence> which requires the fields to be in alphabetic order in the XSD and the data file which may not make sense.  If everything can be generated with that tool, there must be tags to add what I'm missing, but if the data file stores fields in logical order it would have FILENAME then SEQNUM because that's the key, then the third field could be DESCRIPTION.
Otherwise, it is an interesting exercise in the different ways to structure the elements and complex types.

[toc] | [prev] | [next] | [standalone]


#35257

FromEric Douglas <e.d.programmer@gmail.com>
Date2016-08-11 12:24 -0700
Message-ID<6688492a-1bae-479d-9556-68ed4ef82405@googlegroups.com>
In reply to#35251
On Wednesday, August 10, 2016 at 7:18:12 PM UTC-4, Stanimir Stamenkov wrote:
> http://blog.bdoughan.com/2010/07/xmladapter-jaxbs-secret-weapon.html
> 
> I guess you could have something like then:
> 
>      @XmlJavaTypeAdapter(MyIdMapAdapter.class)
>      Map<Integer, DataRecord> records;

So I wrote the XmlAdapter class to store my DataRecords which were just in a List as a TreeMap instead.  So, when I unmarshall it I get a DataFile instance which contains a HashMap with String keys and RMFID values.  RMFID is a custom class which extends DataRecord.  So does the XML make sense?  It seems like it's making nodes a level deeper than it needs.

Sample xml output:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<DataFile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="">
    <DataRecord>
        <entry key="APCDD   001">
            <value xsi:type="rmfid">
                <FILENAME>APCDD</FILENAME>
                <SEQNUM>001</SEQNUM>
            </value>
        </entry>
        <entry key="APCDD   002">
            <value xsi:type="rmfid">
                <FILENAME>APCDD</FILENAME>
                <SEQNUM>002</SEQNUM>
            </value>
        </entry>
        <entry key="APCDD   003">
            <value xsi:type="rmfid">
                <FILENAME>APCDD</FILENAME>
                <SEQNUM>003</SEQNUM>
            </value>
        </entry>
    </DataRecord>
</DataFile>

[toc] | [prev] | [next] | [standalone]


#35309

FromArne Vajhøj <arne@vajhoej.dk>
Date2016-08-13 15:38 -0400
Message-ID<nonsuv$425$1@gioia.aioe.org>
In reply to#35250
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

[toc] | [prev] | [next] | [standalone]


#35357

FromEric Douglas <e.d.programmer@gmail.com>
Date2016-08-16 11:49 -0700
Message-ID<9c174795-1c98-46f0-bbac-179581cd39a2@googlegroups.com>
In reply to#35309
On Saturday, August 13, 2016 at 3:38:21 PM UTC-4, Arne Vajhøj wrote:
> 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();
> 

I haven't used this stream feature yet.  Is that doing the same thing this guy is doing?
http://blog.bdoughan.com/2012/08/handle-middle-of-xml-document-with-jaxb.html

[toc] | [prev] | [next] | [standalone]


#35358

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2016-08-16 15:57 -0400
Message-ID<novr71$8j0$1@dont-email.me>
In reply to#35357
On 8/16/2016 2:49 PM, Eric Douglas wrote:
> On Saturday, August 13, 2016 at 3:38:21 PM UTC-4, Arne Vajhøj wrote:
>> 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();
>>
>
> I haven't used this stream feature yet.  Is that doing the same thing this guy is doing?
> http://blog.bdoughan.com/2012/08/handle-middle-of-xml-document-with-jaxb.html

     No: The "streams" Arne mentions are not the "XMLStreams" and
"StreamSources" found at your link.  See

http://docs.oracle.com/javase/tutorial/collections/streams/index.html

     (Note also that Arne's example is slightly abbreviated, or perhaps
slightly optimistic: The findFirst() operation produces an Optional
that will be empty if nothing satisfies the filter() predicate, and
performing get() on an empty Optional throws NoSuchElementException.
"Serious" code would have to be cognizant of the possibility.)

-- 
esosman@comcast-dot-net.invalid
The Tooth Fairy's gone high-tech, pays in Bitecoin.

[toc] | [prev] | [next] | [standalone]


#35360

FromEric Douglas <e.d.programmer@gmail.com>
Date2016-08-16 13:20 -0700
Message-ID<6eae6f69-b51d-4e1a-9308-fb68fc735db8@googlegroups.com>
In reply to#35358
On Tuesday, August 16, 2016 at 3:57:32 PM UTC-4, Eric Sosman wrote:
>      No: The "streams" Arne mentions are not the "XMLStreams" and
> "StreamSources" found at your link.  See
> 
> http://docs.oracle.com/javase/tutorial/collections/streams/index.html
> 
>      (Note also that Arne's example is slightly abbreviated, or perhaps
> slightly optimistic: The findFirst() operation produces an Optional
> that will be empty if nothing satisfies the filter() predicate, and
> performing get() on an empty Optional throws NoSuchElementException.
> "Serious" code would have to be cognizant of the possibility.)

Yeah I got a test program working using the XMLStreams.  It looks like that could be optimal on my previously mentioned XML format only if I need to read and write a single DataRecord each time, creating a new DataFile instead of reconstructing the DataFile object that is the root of the file.

Arne's example appears to be reconstructing the entire DataFile, storing the DataRecords in List form, and searching the entire List for records.  I don't know how efficient that is with the stream().  It sounds like a lot of overhead.  I have another working option to store the DataRecords in a TreeMap and write that to the XML with an XMLAdapter so I can find an individual record with a key value and traverse the map to return a range of values.  I normally only get a specific value or all values from a map but it looks like the higherEntry() method will handle that.  The downside is I need multiple key maps set up linking all records if I need to return a range on different keys ie a list of Customers by Name, or by State, with the key field(s) hard coded while the stream() method appears more dynamic.

[toc] | [prev] | [next] | [standalone]


#35368

FromArne Vajhøj <arne@vajhoej.dk>
Date2016-08-16 17:04 -0400
Message-ID<novv42$12m6$1@gioia.aioe.org>
In reply to#35360
On 8/16/2016 4:20 PM, Eric Douglas wrote:
> Yeah I got a test program working using the XMLStreams.  It looks
> like that could be optimal on my previously mentioned XML format only
> if I need to read and write a single DataRecord each time, creating a
> new DataFile instead of reconstructing the DataFile object that is
> the root of the file.
>
> Arne's example appears to be reconstructing the entire DataFile,
> storing the DataRecords in List form, and searching the entire List
> for records.  I don't know how efficient that is with the stream().
> It sounds like a lot of overhead.  I have another working option to
> store the DataRecords in a TreeMap and write that to the XML with an
> XMLAdapter so I can find an individual record with a key value and
> traverse the map to return a range of values.  I normally only get a
> specific value or all values from a map but it looks like the
> higherEntry() method will handle that.  The downside is I need
> multiple key maps set up linking all records if I need to return a
> range on different keys ie a list of Customers by Name, or by State,
> with the key field(s) hard coded while the stream() method appears
> more dynamic.

You need to separate issues.

1) Do you want to read/write all data from/to disk every time?

Yes => XML is fine, but no matter what you do then that is how XML
works (well - unless you do some very nasty things with
RandomAccessFIle)

No => do not use XML, use a database of some sort

2) Assuming XML what API do you want to read and write using?

You have an object model and you want everything in memory at
the same time and you want to write as little code as
possible => use JAXB

Other criteria could lead to other XML API's (DOM, StAX, SAX etc.).

3) Assuming you want an object model with everything in memory
    (fastest if data will fit in memory) how do you want to
    organize data in memory?

No special requirements => List<X> is a simple choice

Special requirements => pick something else

Note that you can convert data to a TreeMap using streams
and lambdas if you really need fast lookup by key.

Arne

[toc] | [prev] | [next] | [standalone]


#35370

FromArne Vajhøj <arne@vajhoej.dk>
Date2016-08-16 21:37 -0400
Message-ID<np0f3u$1kfc$1@gioia.aioe.org>
In reply to#35368
On 8/16/2016 5:04 PM, Arne Vajhøj wrote:
> Note that you can convert data to a TreeMap using streams
> and lambdas if you really need fast lookup by key.

Demo (well - it is converting to HashMap I think):

Previous code:

// 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();

Map code:

// search
Map<Integer, Data> lookupByIv = 
data2.getList().stream().collect(Collectors.toMap(item -> item.getIv(), 
item -> item));
Map<String, Data> lookupBySv = data2.getList().stream().filter(item -> 
item instanceof DataA).map(item -> 
(DataA)item).collect(Collectors.toMap(item -> item.getSv(), item -> item));
Data o1 = lookupByIv.get(456);
Data o2 = lookupBySv.get("ABC");

Arne

[toc] | [prev] | [next] | [standalone]


#35377

FromEric Douglas <e.d.programmer@gmail.com>
Date2016-08-17 05:22 -0700
Message-ID<6e0fcb84-8476-4daf-afae-b5afc046bd15@googlegroups.com>
In reply to#35370
On Tuesday, August 16, 2016 at 9:37:16 PM UTC-4, Arne Vajhøj wrote:
> > Note that you can convert data to a TreeMap using streams
> > and lambdas if you really need fast lookup by key.
> 
> Demo (well - it is converting to HashMap I think):
> 
> Previous code:
> 
> // 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();
> 
> Map code:
> 
> // search
> Map<Integer, Data> lookupByIv = 
> data2.getList().stream().collect(Collectors.toMap(item -> item.getIv(), 
> item -> item));
> Map<String, Data> lookupBySv = data2.getList().stream().filter(item -> 
> item instanceof DataA).map(item -> 
> (DataA)item).collect(Collectors.toMap(item -> item.getSv(), item -> item));
> Data o1 = lookupByIv.get(456);
> Data o2 = lookupBySv.get("ABC");
> 
> Arne

Will test, sounds great.

[toc] | [prev] | [next] | [standalone]


#35376

FromEric Douglas <e.d.programmer@gmail.com>
Date2016-08-17 05:21 -0700
Message-ID<7f9104b3-3f5a-4f40-b994-f53e877a0f5f@googlegroups.com>
In reply to#35368
On Tuesday, August 16, 2016 at 5:04:17 PM UTC-4, Arne Vajhøj wrote:
> You need to separate issues.
> 
> 1) Do you want to read/write all data from/to disk every time?
> 
> Yes => XML is fine, but no matter what you do then that is how XML
> works (well - unless you do some very nasty things with
> RandomAccessFIle)
> 
> No => do not use XML, use a database of some sort

The writing to disk every time is something I've been wondering.  Do I update the file when I call a .writeRecord() method if I have to write out every record, or do I update the file when I call a .closeFile().  Database is the thing we're trying to avoid because I want data files that can merge changes from 2 people using SourceTree so they have to be in a text format.
https://www.atlassian.com/software/sourcetree

> 2) Assuming XML what API do you want to read and write using?
> 
> You have an object model and you want everything in memory at
> the same time and you want to write as little code as
> possible => use JAXB
> 
> Other criteria could lead to other XML API's (DOM, StAX, SAX etc.).

That was the first question.  JAXB seems to be working (at least for now, though of course has a memory problem if needed for much larger files), which works with DOM by default and can work with a SAX (or StAX) parser to read in a sub-element instead of the entire file.  Our first attempt at an XML API used SAX which looks really annoying as it has to read everything separately, thus keep track of everything that's been read, so instead of elements you get one read for the start element, a read for anything inside the element, and another read for the end element.

> 3) Assuming you want an object model with everything in memory
>     (fastest if data will fit in memory) how do you want to
>     organize data in memory?
> 
> No special requirements => List<X> is a simple choice
> 
> Special requirements => pick something else
> 
> Note that you can convert data to a TreeMap using streams
> and lambdas if you really need fast lookup by key.
> 
> Arne

Yeah, organization is whatever is most efficient to read and write records as previously described (ie say it's a customer master and the primary key is customer ID but you also want to be able to return a set of customer records for a particular state).  This .stream() stuff sounds promising.

[toc] | [prev] | [next] | [standalone]


#35359

FromArne Vajhøj <arne@vajhoej.dk>
Date2016-08-16 16:13 -0400
Message-ID<novs53$u87$1@gioia.aioe.org>
In reply to#35357
On 8/16/2016 2:49 PM, Eric Douglas wrote:
> On Saturday, August 13, 2016 at 3:38:21 PM UTC-4, Arne Vajhøj wrote:
>> 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();
>>
>
> I haven't used this stream feature yet.

It is a new thing in Java 8. Some may say the new thing in Java 8.

>                              Is that doing the same thing this guy is doing?
> http://blog.bdoughan.com/2012/08/handle-middle-of-xml-document-with-jaxb.html

Not at all.

That link shows how to mix StAX and JAXB.

Arne

[toc] | [prev] | [next] | [standalone]


#35361

FromEric Douglas <e.d.programmer@gmail.com>
Date2016-08-16 13:22 -0700
Message-ID<889d2b95-2a05-496c-b807-2f1ea8a33b2f@googlegroups.com>
In reply to#35359
On Tuesday, August 16, 2016 at 4:13:40 PM UTC-4, Arne Vajhøj wrote:
> Not at all.
> 
> That link shows how to mix StAX and JAXB.
> 
> Arne

Right, it appears to be a difference of how to filter out a record while reading XML versus filtering records out after reconstructing an object containing all records in the XML.

[toc] | [prev] | [next] | [standalone]


#35366

FromArne Vajhøj <arne@vajhoej.dk>
Date2016-08-16 16:53 -0400
Message-ID<novufs$11iu$1@gioia.aioe.org>
In reply to#35361
On 8/16/2016 4:22 PM, Eric Douglas wrote:
> On Tuesday, August 16, 2016 at 4:13:40 PM UTC-4, Arne Vajhøj wrote:
>> Not at all.
>>
>> That link shows how to mix StAX and JAXB.
>
> Right, it appears to be a difference of how to filter out a record
> while reading XML versus filtering records out after reconstructing an
> object containing all records in the XML.

I would not call it filtering at all.

What it basically does is:

read/write boilerplate XML using StAX
read/write interesting XML using JAXB

Arne

[toc] | [prev] | [next] | [standalone]


#35363

FromEric Douglas <e.d.programmer@gmail.com>
Date2016-08-16 13:32 -0700
Message-ID<8cd46667-0abc-4283-b366-d229f99032bd@googlegroups.com>
In reply to#35359
On Tuesday, August 16, 2016 at 4:13:40 PM UTC-4, Arne Vajhøj wrote:
> It is a new thing in Java 8. Some may say the new thing in Java 8.
> 

Is it new to 8?  I couldn't remember what they added in 8 recently aside from lambdas.

I got that code working with my current setup which is storing the records in a TreeMap, using .getRecords().values().stream().filter(...
Now I just need to figure out the rest.  What if the filter returns no records or multiple records?
Is there a more efficient way?  It really wouldn't matter on my tiny files.  It would make a difference if we have 1,000,000 records.

[toc] | [prev] | [next] | [standalone]


#35365

FromArne Vajhøj <arne@vajhoej.dk>
Date2016-08-16 16:47 -0400
Message-ID<novu4s$11c4$1@gioia.aioe.org>
In reply to#35363
On 8/16/2016 4:32 PM, Eric Douglas wrote:
> On Tuesday, August 16, 2016 at 4:13:40 PM UTC-4, Arne Vajhøj wrote:
>> It is a new thing in Java 8. Some may say the new thing in Java 8.
>>
>
> Is it new to 8?  I couldn't remember what they added in 8 recently aside from lambdas.

streams and interface default methods was sort of part of lambda as
they were required to make lambdas useful for standard Java library
without breaking too much existing code.

> I got that code working with my current setup which is storing the
> records in a TreeMap, using
> .getRecords().values().stream().filter(... Now I just need to figure
> out the rest.  What if the filter returns no records or multiple
> records?
There are various ways of handling that.

The simplest is probably converting the stream to a List (collect).

> Is there a more efficient way?  It really wouldn't matter on my tiny
> files.  It would make a difference if we have 1,000,000 records.

The logic behind streams is that they are a high level abstraction
with a default implementation and the intention that specific
implementation provide more efficient implementation when it
makes sense.

I would not look for alternatives unless I have a real
performance problem.

Arne

[toc] | [prev] | [next] | [standalone]


#35367

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2016-08-16 16:59 -0400
Message-ID<novusd$ln1$1@dont-email.me>
In reply to#35363
On 8/16/2016 4:32 PM, Eric Douglas wrote:
>
> [...] using .getRecords().values().stream().filter(...
> Now I just need to figure out the rest.  What if the filter returns no records or multiple records?

     All explained in the Tutorial, to which you've already been pointed.

> Is there a more efficient way?

     Certainly: Assembly code, hand-crafted by a skilled craftsman, will
almost certainly run faster and in less memory.

-- 
esosman@comcast-dot-net.invalid
The Tooth Fairy has gone high-tech, now pays in Bitecoin.

[toc] | [prev] | [next] | [standalone]


#35159

FromArne Vajhøj <arne@vajhoej.dk>
Date2016-08-01 08:46 -0400
Message-ID<nnnga9$5c6$1@gioia.aioe.org>
In reply to#35157
On 8/1/2016 12:39 AM, Roedy Green wrote:
> On Sat, 30 Jul 2016 21:49:38 -0400, Arne Vajhøj <arne@vajhoej.dk>
> wrote:
>> That sounds like JAXB not JAXP!
>
> Both Java and Amazon refer to it only as JAX.

JAX is a common prefix for all Java XML standards.

> "%JDK64\bin\wsimport.exe" converts the description to Java code
> The description lives in
> http://ecs.amazonaws.com/AWSECommerceService/2013-08-01/AWSECommerceService.wsdl

WSDL is a decsription for SOAP web services.

wsimport generates stubs from WSDL using the JAX-WS standard.

> What marker would make it absolutely certain I have either JAXP or
> JAXB?

Both comes with Java SE.

JAXP is the basic parsers.

JAXB is binding between XML and Java objects.

I guess you could say that:

JAXP is like JDBC
JAXB is like JPA

in some ways.

Arne

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.java.programmer


csiph-web