Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #35238
| From | Stanimir Stamenkov <s7an10@netscape.net> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: XML |
| Date | 2016-08-09 23:57 +0300 |
| Organization | A noiseless patient Spider |
| Message-ID | <nodg3g$elh$1@dont-email.me> (permalink) |
| References | (4 earlier) <0176078c-ab98-4e66-ba3e-b14ee88b58e0@googlegroups.com> <nnngk6$603$1@gioia.aioe.org> <d7e3e460-1025-4670-b425-672f4196d9e2@googlegroups.com> <no7m5v$f24$1@dont-email.me> <487a50aa-8e4b-460a-9824-bd23c26d4c02@googlegroups.com> |
Mon, 8 Aug 2016 05:38:22 -0700 (PDT), Eric Douglas:
> On Sunday, August 7, 2016 at 12:04:28 PM UTC-4, Stanimir Stamenkov
> wrote:
>
>> That's because your Customer class is not annotated as/bound to a
>> top-level XML element type (it really helps understanding the XML
>> Schema model in this context). Try using @XmlRootElement on it:
>>
>> @XmlRootElement
>> public class Customer extends DataRecord {
>>
>> I think @XmlSeeAlso(DataFile.class) on the Customer class is
>> superfluous. In certain situations you may need it the other way
>> around:
>>
>> @XmlSeeAlso(Customer.class)
>> public class DataRecord {
>
> There are a number of confusing things in the JAXB, those are a
> couple of them.
>
> Does it make any sense to put @XmlRootElement on Customer?
That what my remark about understanding the XML Schema model was.
The @XmlRootElement declares a top-level element type which might be
used as document root element, but might appear as a child in
certain content models as well. Because you've used this DataRecord
abstraction you need to have top-level element types if you want
different elements to appear in a List<DataRecord>.
> It appears to work the same with or without it, and when
> marshalled to the file it will never be the root. DataFile is the
> root, that is it's purpose. The only purpose of DataRecord is to
> be something generic the DataFile can store as a collection.
> Customer, and other classes, extend the DataRecord to define a
> single record. One file starts with none of these, typically has
> many Customers (or items, orders, etc).
>
> Does it make sense to use the @XmlSeeAlso?
I've finally tried the whole of your example and discovered more
discrepancies – see my reworked example at the end.
> If I put the XmlSeeAlso on the Customer to point to the DataFile I
> can just pass the Customer class to the JAXBContext.newInstance,
> which can accept multiple classes, and gets the same output if I
> add the DataFile class there instead. Perhaps this tag doesn't
> make sense in either direction at this level, but to cascade
> further down such as storing a collection of something ie shipping
> addresses within each customer?
>
> Is every XML file supposed to have that
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" at least on
> the root node? I tried what methods I could find and couldn't get
> it to go away, or write a different value. That looks like a
> useful url but seems like a waste of space to write it to every file.
Try my example (single "JAXBtoXML.java" file):
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.bind.annotation.XmlValue;
@XmlRootElement(name = "DataRecord")
@XmlSeeAlso(Customer.class)
class DataRecord {
@XmlValue
String text;
}
@XmlRootElement(name = "DataFile")
class DataFile<E> {
List<DataRecord> records;
public List<DataRecord> getRecords() {
return records;
}
@XmlElementRef
public void setRecords(List<DataRecord> newRecords) {
records = newRecords;
}
public void add(DataRecord newRec) {
if (records == null) {
records = new ArrayList<>();
}
records.add(newRec);
}
}
@XmlRootElement(name = "Customer")
class Customer extends DataRecord {
String name;
int age;
int id;
public String getName() {
return name;
}
@XmlElement
public void setName(final String name) {
this.name = name;
}
public int getAge() {
return age;
}
@XmlElement
public void setAge(final int age) {
this.age = age;
}
public int getId() {
return id;
}
@XmlAttribute
public void setId(final int id) {
this.id = id;
}
}
public class JAXBtoXML {
public static void main(final String[] args) throws Exception {
final Customer customer = new Customer();
customer.setId(100);
customer.setName("John Smith");
customer.setAge(29);
final Customer customer2 = new Customer();
customer2.setId(101);
customer2.setName("Jane Doe");
customer2.setAge(30);
final DataRecord generic = new DataRecord();
generic.text = "Eric";
final JAXBContext jaxbContext = JAXBContext.newInstance(DataFile.class);
final Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
DataFile<Customer> customerList = new DataFile();
customerList.add(customer);
customerList.add(customer2);
customerList.add(generic);
jaxbMarshaller.marshal(customerList, System.out);
}
}
This produces the following output:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<DataFile>
<Customer id="100">
<age>29</age>
<name>John Smith</name>
</Customer>
<Customer id="101">
<age>30</age>
<name>Jane Doe</name>
</Customer>
<DataRecord>Eric</DataRecord>
</DataFile>
See if that suits you.
--
Stanimir
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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