/
ch03.txt
1076 lines (812 loc) · 29.7 KB
/
ch03.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
chapter: Working with XML and Java
==================
<catalog>
<book sku="123_xaa">
<title>King Lear</title>
<author>William Shakespeare</author>
<price>6.95</price>
<category>classics</category>
</book>
<book sku="988_yty">
<title>Hamlet</title>
<author>William Shakespeare</author>
<price>5.95</price>
<category>classics</category>
</book>
<book sku="434_asd">
<title>1984</title>
<author>George Orwell</author>
<price>12.95</price>
<category>classics</category>
</book>
<book sku="876_pep">
<title>Java Generics and Collections</title>
<authors>
<author>Maurice Naftalin</author>
<author>Phillip Wadler</author>
</authors>
<price>34.99</price>
<category>programming</category>
</book>
</catalog>
====================================
Example: Using the Cursor parsing method of parsing in StAX.
package com.sc.ch02.stax;
import static java.lang.System.out;
import java.io.InputStream;
import java.util.Set;
import java.util.TreeSet;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.events.XMLEvent;
public class StaxCursor {
private static final String db = "/ch02/Catalog.xml";
//we'll hold values here as we find them
private Set<String> uniqueAuthors;
public static void main(String... args) {
StaxCursor p = new StaxCursor();
p.find();
}
//constructor
public StaxCursor() {
uniqueAuthors = new TreeSet<String>();
}
//parse the document and offload work to helpers
public void find() {
XMLInputFactory xif = XMLInputFactory.newInstance();
//forward-only, most efficient way to read
XMLStreamReader reader = null;
//get ahold of the file
final InputStream is =
StaxCursor.class.getResourceAsStream(db);
//whether current event represents elem, attrib, etc
int eventType;
String current = "";
try {
//create the reader from the stream
reader = xif.createXMLStreamReader(is);
//work with stream and get the type of event
//we're inspecting
while (reader.hasNext()) {
//because this is Cursor, we get an integer token to next event
eventType = reader.next();
//do different work depending on current event
switch (eventType) {
case XMLEvent.START_ELEMENT:
//save element name for later
current = reader.getName().toString();
printSkus(current, reader);
break;
case XMLEvent.CHARACTERS:
findAuthors(current, reader);
break;
}
} //end loop
out.println("Unique Authors=" + uniqueAuthors);
} catch (XMLStreamException e) {
out.println("Cannot parse: " + e);
}
}
//get the name and value of the book's sku attribute
private void printSkus(String current, XMLStreamReader r) {
current = r.getName().toString();
if ("book".equals(current)) {
String k = r.getAttributeName(0).toString();
String v = r.getAttributeValue(0);
out.println("AttribName " + k + "=" + v);
}
}
//inspect author elements and read their values.
private void findAuthors(String current, XMLStreamReader r)
throws XMLStreamException {
if ("author".equals(current)) {
String v = r.getText().trim();
//can get whitespace value, so ignore
if (v.length() > 0) {
uniqueAuthors.add(v);
}
}
}
}
====================================
AttribName sku=123_xaa
AttribName sku=988_yty
AttribName sku=434_asd
AttribName sku=876_pep
Unique Authors=[George Orwell, Maurice Naftalin, Phillip Wadler, William Shakespeare]
====================================
Example: Reading XML with StAX Iterator
public class StaxIterator {
public void find() {
XMLInputFactory xif = XMLInputFactory.newInstance();
//forward-only, most efficient way to read
XMLEventReader reader = null;
//get ahold of the file
final InputStream is =
StaxIterator.class.getResourceAsStream(db);
try {
//create the reader from the stream
reader = xif.createXMLEventReader(is);
//work with stream and get the type of event
//we're inspecting
while (reader.hasNext()) {
XMLEvent e = reader.nextEvent();
if (e.isStartElement()){
e = e.asStartElement().getAttributeByName(
new QName("sku"));
if (e != null){
out.println(e);
}
}
} //end loop
} catch (XMLStreamException e) {
out.println("Cannot parse: " + e);
}
}
}
====================================
sku='123_xaa'
sku='988_yty'
sku='434_asd'
sku='876_pep'
====================================
Example: Writing an XML File with XMLEventWriter
package com.sc.ch02.stax;
import static java.lang.System.out;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
public class WriteStax {
private static final String REPAIR_NS = "javax.xml.stream.isRepairingNamespaces";
private static final String NS = "http://ns.example.com/books";
public static void main(String... args) {
XMLOutputFactory factory = XMLOutputFactory.newInstance();
// autobox
factory.setProperty(REPAIR_NS, true);
try {
//setup a destination file
FileOutputStream fos =
new FileOutputStream("result.xml");
//create the writer
final XMLStreamWriter xsw = factory.createXMLStreamWriter(fos);
xsw.setDefaultNamespace(NS);
//open the document. Can also add encoding, etc
xsw.writeStartDocument("1.0");
xsw.writeEndDocument();
xsw.writeComment("Powered by StAX");
//make enclosing book
xsw.writeStartElement("book");
xsw.writeNamespace("b", NS);
xsw.writeAttribute("sku", "345_iui");
//make title child element
xsw.writeStartElement(NS, "title");
xsw.writeCharacters("White Noise");
xsw.writeEndElement(); //close title
xsw.writeEndElement(); //close book
//clean up
xsw.flush();
fos.close();
xsw.close();
out.print("All done.");
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (XMLStreamException xse) {
xse.printStackTrace();
}
}
}
====================================
<?xml version="1.0"?>
<!--Powered by StAX-->
<book xmlns="http://ns.example.com/books" xmlns:b="http://ns.example.com/books"
sku="345_iui">
<b:title>White Noise</b:title>
</book>
====================================
<book xmlns="http://ns.example.com/books" xmlns:b="http://ns.example.com/books"
sku="345_iui">
====================================
<?xml version="1.0"?>
<!--Powered by StAX-->
<book xmlns:b="http://ns.example.com/books" sku="345_iui">
//...
====================================
Example: StaxFiltered.java
package com.sc.ch02.stax;
import static java.lang.System.out;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;
import javax.xml.stream.*;
import javax.xml.stream.events.XMLEvent;
public class StaxFiltered {
private static final String fdb = "path/ch02/Catalog.xml";
private Map<String, Double> expensiveBooks;
private String lastTitle;
//constructor
public StaxFiltered() {
expensiveBooks = new HashMap<String, Double>();
}
public static void main(String[] args) {
StaxFiltered p = new StaxFiltered();
p.findByEvent();
}
/*
* Here our aim is to find book prices over $10. So we use a
* filter to give us only start elements so we have already
* filtered out items we know don't help us.
*/
public void findByEvent() {
try {
XMLInputFactory xif = XMLInputFactory.newInstance();
FileReader fr = new FileReader(fdb);
// wrap the XMLStreamReader with FilteredReader
XMLEventReader reader =
xif.createFilteredReader(
xif.createXMLEventReader(fr),
new StartElementEventFilter());
// work with stream and get the type of event
// we're inspecting
while (reader.hasNext()) {
XMLEvent event = (XMLEvent) reader.next();
int eventType = event.getEventType();
switch (eventType) {
case XMLEvent.START_ELEMENT:
findHighPrices(reader, event);
}
} // end loop
out.println("Expensive books=" + expensiveBooks);
} catch (FileNotFoundException fnfe) {
out.println("Cannot find source: " + fnfe);
} catch (XMLStreamException e) {
out.println("Cannot parse: " + e);
}
}
private void findHighPrices(XMLEventReader reader,
XMLEvent event) throws XMLStreamException {
String currentElem = event.asStartElement().toString();
// save off the title so we can match the price with it
if ("<title>".equals(currentElem)) {
lastTitle = reader.getElementText();
}
// get the current price and add to collection if high
if ("<price>".equals(currentElem)) {
double price;
try {
price = Double.parseDouble(reader
.getElementText());
if (price > 10.0D) {
expensiveBooks.put(lastTitle, price);
}
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
} catch (XMLStreamException xse) {
xse.printStackTrace();
}
}
}
}
/**
* Get only start elements for efficiency. If we returned only
* attributes, for example, we wouldn't be able to read the data
* we're interested in here (title and price values).
*/
class StartElementEventFilter implements EventFilter {
// only req'd method to implement
public boolean accept(XMLEvent event) {
return event.isStartElement();
}
}
====================================
Expensive books={Java Generics and Collections=34.99, 1984=12.95}
====================================
Example: BasicXPath.java
package com.sc.ch02.xpath;
import static java.lang.System.out;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
/**
* Accepts an XPath expression to perform searching against
* the Catalog.xml document.
*/
public class BasicXPath {
public static void main(String...args) throws Exception {
String xmlSource = "src/xml/ch02/Catalog.xml";
//Get all titles with price between $5 and $9.99
xpath = "//book[price > 5.00 and price < 9.99]/title";
/* Prints:
* Value=King Lear
Value=Hamlet
*/
search(xmlSource, xpath);
}
public static void search(String fileIn, String xpathExp)
throws IOException {
// Set up the DOM parser
DocumentBuilderFactory docFactory =
DocumentBuilderFactory.newInstance();
try {
//Parse XML document
DocumentBuilder docBuilder =
docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(fileIn);
//Create XPath instance
XPath xpath = XPathFactory.newInstance().newXPath();
//Evaluate XPath expression against parsed document
NodeList nodes = (NodeList) xpath.evaluate(xpathExp,
doc, XPathConstants.NODESET);
//We could return these instead to let caller deal
for (int i = 0, len = nodes.getLength(); i < len; i++) {
Node node = nodes.item(i);
String value = node.getTextContent();
out.println("Value=" + value);
}
} catch (XPathExpressionException xpee) {
out.println(xpee);
throw new IOException("Cannot parse XPath.", xpee);
} catch (DOMException dome) {
out.println(dome);
throw new IOException("Cannot create DOM tree", dome);
} catch (ParserConfigurationException pce) {
out.println(pce);
throw new IOException("Cannot create parser.", pce);
} catch (SAXException saxe) {
out.println(saxe);
throw new IOException("Error parsing XML document.", saxe);
}
}
}
====================================
"//book[price <" + uservalue + "]/title"
====================================
//Find the book titled 'Hamlet' and select its price.
String xpath = "/catalog/book[title='Hamlet']/price";
//Prints: Value=5.95
//Find titles of books with multiple authors
xpath = "/catalog/book[authors]/title";
//Prints:
Value=Java Generics and Collections
//Find all title AND price elements
xpath = "//title | //price";
//Prints:
Value=King Lear
Value=6.95
Value=Hamlet
Value=5.95
Value=1984
Value=12.95
Value=Java Generics and Collections
Value=34.99
//Get the author of the second book on the list
xpath = "//book[2]/author";
//Prints:
Value=William Shakespeare
//Get the SKU attrib value of the last book on the list
xpath = "//book[last()]/@sku";
//Prints:
Value=876_pep
//Get the entire book node for Hamlet
xpath = "//book[title='Hamlet']";
//Prints the entire Hamlet node
//Get the penultimate (one before the last) book that is a 'classic'
//whose price is between $5 and $10.
xpath = "//book[category='classics' and (price > 5 and price < 10)][last()-1]";
//Prints the entire King Lear node
//Get the title node with the value of Hamlet
xpath = "//title[.='Hamlet']";
//Get the title of the first book whose author starts with 'William'
xpath = "//book[1][author[starts-with(., 'William')]]/title";
//Prints: Value=King Lear
//Gets the categories of the books after the 2nd one in the tree
xpath = "//book[position() > 2]/category";
//Gets any authors that are co-authors,
//that is, all <author> nodes under an <authors> node
xpath = "//authors//author";
//Prints:
Value=Maurice Naftalin
Value=Phillip Wadler
====================================
Example: UpdateXMLValue.java
package com.sc.ch02.xpath;
import static java.lang.System.out;
import java.io.FileWriter;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
public class UpdateXMLValue {
public static void main(String[] args) throws Exception {
String xmlSource = "src/xml/ch02/Catalog.xml";
//find the book titled 'Hamlet' and select its price.
String xpath = "/catalog/book[title='Hamlet']/price";
//this is the new price
String value = "8.95";
//we're throwing any exception out
updateValueInXmlFile(xmlSource, xmlSource, xpath, value);
out.println("All done.");
}
public static void updateValueInXmlFile(String fileIn,
String fileOut, String xpathExpression,
String newValue) throws IOException {
// Set up the DOM evaluator
final DocumentBuilderFactory docFactory =
DocumentBuilderFactory.newInstance();
try {
final DocumentBuilder docBuilder =
docFactory.newDocumentBuilder();
final Document doc = docBuilder.parse(fileIn);
final XPath xpath =
XPathFactory.newInstance().newXPath();
NodeList nodes =
(NodeList) xpath.evaluate(xpathExpression,
doc, XPathConstants.NODESET);
// Update the nodes we found
for (int i = 0, len = nodes.getLength(); i < len; i++) {
Node node = nodes.item(i);
node.setTextContent(newValue);
}
// Get file ready to write
final Transformer transformer =
TransformerFactory.newInstance()
.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT,
"yes");
transformer.setOutputProperty(OutputKeys.ENCODING,
"UTF-8");
StreamResult result =
new StreamResult(new FileWriter(fileOut));
transformer.transform(new DOMSource(doc), result);
// Write file out
result.getWriter().flush();
result.getWriter().close();
} catch (XPathExpressionException xpee) {
out.println(xpee);
throw new IOException("Cannot parse XPath.", xpee);
} catch (DOMException dome) {
out.println(dome);
throw new IOException("Cannot create DOM tree", dome);
} catch (TransformerConfigurationException tce) {
out.println(tce);
throw new IOException("Cannot create transformer.",
tce);
} catch (IllegalArgumentException iae) {
out.println(iae);
throw new IOException("Illegal Argument.", iae);
} catch (ParserConfigurationException pce) {
out.println(pce);
throw new IOException("Cannot create parser.", pce);
} catch (SAXException saxe) {
out.println(saxe);
throw new IOException("Error reading XML document.",
saxe);
} catch (TransformerFactoryConfigurationError tfce) {
out.println(tfce);
throw new IOException("Cannot create trx factory.",
tfce);
} catch (TransformerException te) {
out.println(te);
throw new IOException("Cannot write values.", te);
}
}
}
====================================
Example: Book Class to Marshal into XML
package com.soacookbook.ch02.jaxb;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Book {
private String title;
private double price;
private Author author;
private Category category;
//... getters and setters omitted.
}
class Author {
private String firstName;
private String lastName;
//... getters and setters omitted.
}
enum Category {
LITERATURE,
PHILOSOPHY,
PROGRAMMING
;
}
====================================
Example: Using JAXB to Marshal a Book Object into XML and Print it to
the Console
package com.soacookbook.ch02.jaxb;
import static java.lang.System.out;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class MarshalToConsole {
public static void main(String...arg) {
try {
Book book = new Book();
Author a = new Author();
a.setFirstName("Jacques");
a.setLastName("Derrida");
book.setAuthor(a);
book.setPrice(34.95);
book.setTitle("Of Grammatology");
book.setCategory(Category.PHILOSOPHY);
Class[] c = {Book.class};
JAXBContext ctx = JAXBContext.newInstance(c);
Marshaller m = ctx.createMarshaller();
//could also use System.out here
m.marshal(book, out);
out.println("\nAll done.");
} catch (JAXBException ex) {
ex.printStackTrace();
}
}
}
====================================
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<book>
<author>
<firstName>Jacques</firstName>
<lastName>Derrida</lastName>
</author>
<category>PHILOSOPHY</category>
<price>34.95</price>
<title>Of Grammatology</title>
</book>
All done.
====================================
Example: Marshal to a DOM Node
//Create Document
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
//Send marshal result to Document
m.marshal(book, doc);
//Find a value
String title = doc.getDocumentElement().
getElementsByTagName("title").item(0).getTextContent();
System.console().printf("Read %s now!", title);
/*
Prints: Read Of Grammatology now!
All done.
*/
====================================
Example: Marshal to a New File
m.marshal(book, new FileOutputStream(new File("aBook.xml")));
====================================
Example: Store Marshal Result in a String Using StringWriter
StringWriter sw = new StringWriter();
m.marshal(book, sw);
System.out.println(sw);
====================================
Example: Using JAXBElement to Unmarshal an XML String
package com.soacookbook.ch02.jaxb;
import java.io.StringReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Document;
public class UnmarshalWithElement {
public static void main(String...arg) {
try {
//Create context
JAXBContext ctx = JAXBContext.newInstance(Book.class);
//Create marshaller
Unmarshaller um = ctx.createUnmarshaller();
//Read in the XML from anywhere
//In this case it is a complete XML book as string.
StringReader sr = new StringReader(getBookXml());
//Get XML from object
JAXBElement<Book> b = um.unmarshal(
new StreamSource(sr), Book.class);
//Start working with object
Book book = b.getValue();
System.console().printf("Title: %s", book.getTitle());
} catch (JAXBException ex) {
ex.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static String getBookXml(){
return "<com.soacookbook.ch02.xstream.Book>" +
"<title>On Friendship</title>" +
"<price>39.95</price>" + //etc...
}
}
====================================
Title: On Friendship
====================================
>java -jar C:/programs/trang/trang.jar C:/repository/src/xml/ch02/Catalog.xml Catalog.xsd
====================================
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="catalog">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="book"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element ref="title"/>
<xs:choice>
<xs:element ref="author"/>
<xs:element ref="authors"/>
</xs:choice>
<xs:element ref="price"/>
<xs:element ref="category"/>
</xs:sequence>
<xs:attribute name="sku" use="required" type="xs:NMTOKEN"/>
</xs:complexType>
</xs:element>
<xs:element name="title" type="xs:string"/>
<xs:element name="authors">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="author"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="price" type="xs:decimal"/>
<xs:element name="category" type="xs:NCName"/>
<xs:element name="author" type="xs:string"/>
</xs:schema>
====================================
-o disable-abstract-elements
====================================
<a:address>
<a:street>1212 Some Street</a:street>
<a:city>Washington,DC</a:city>
<a:state>VA</a:state>
</a:address>
====================================
<a:address xmlns:a="urn:ns:soacookbook:address">
<a:street>1212 Some Street</a:street>
<a:city>Washington,DC</a:city>
<a:state>VA</a:state>
</a:address>
====================================
Example: From Java to XML and Back with XStream
package com.soacookbook.ch02.xstream;
import com.soacookbook.ch02.jaxb.*;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import java.io.Serializable;
import static java.lang.System.out;
/**
* Shows Java to XML back to Java with no mapping
* using XStream.
*/
public class XMLStreamRoundTrip<T extends Serializable> {
public static void main(String...arg){
//Create a complex object to work with
Book book = new Book();
Author a = new Author();
a.setFirstName("Jacques");
a.setLastName("Derrida");
book.setAuthor(a);
book.setPrice(39.95);
book.setTitle("Glas");
book.setCategory(Category.PHILOSOPHY);
//Put the book into XML
XMLStreamRoundTrip<Book> x = new XMLStreamRoundTrip<Book>();
String bookXml = x.toXml(book);
//Print entire XML
System.console().printf("XML:\n%s\n", bookXml);
//Create a new object by rehydrating the XML
Book newBook = x.fromXml(bookXml);
//Show values
System.console().printf("Object:\n%s costs $%s\n",
newBook.getTitle(), newBook.getPrice());
}
public String toXml(T model) {
return new XStream().toXML(model);
}
@SuppressWarnings("unchecked")
public T fromXml(String modelAsString) {
XStream xstream = new XStream(new DomDriver());
T model = (T)xstream.fromXML(modelAsString);
return model;
}
}
====================================
XML:
<com.soacookbook.ch02.xstream.Book>
<title>Glas</title>
<price>39.95</price>
<author>
<firstName>Jacques</firstName>
<lastName>Derrida</lastName>
</author>
<category>PHILOSOPHY</category>
</com.soacookbook.ch02.xstream.Book>
Object:
Glas costs $39.95
====================================
> xjc -cp property-listener-injector.jar -Xinject-listener-code Book.xsd
====================================
<taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask">
<classpath>
<pathelement path="/path/jaxb-xjc.jar"/>
<pathelement path="/path/plugin.jar" />
</classpath>
</taskdef>