-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathJAXBDocument.java
More file actions
154 lines (123 loc) · 4.67 KB
/
JAXBDocument.java
File metadata and controls
154 lines (123 loc) · 4.67 KB
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
/*
* Copyright (c) 2010-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
*/
package com.marklogic.client.example.cookbook;
import com.marklogic.client.DatabaseClient;
import com.marklogic.client.DatabaseClientFactory;
import com.marklogic.client.document.XMLDocumentManager;
import com.marklogic.client.example.cookbook.Util.ExampleProperties;
import com.marklogic.client.io.JAXBHandle;
import com.marklogic.client.io.StringHandle;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.annotation.XmlRootElement;
import java.io.IOException;
/**
* JAXBDocument illustrates how to write and read a POJO structure as a database document.
*/
public class JAXBDocument {
public static void main(String[] args) throws JAXBException, IOException {
run(Util.loadProperties());
}
/**
* Product provides an example of a class with JAXB annotations.
*/
@XmlRootElement
static public class Product {
private String name;
private String industry;
private String description;
public Product() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIndustry() {
return industry;
}
public void setIndustry(String industry) {
this.industry = industry;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
public static void run(ExampleProperties props) throws JAXBException {
System.out.println("example: "+JAXBDocument.class.getName()+"\n");
// use either shortcut or strong typed IO
runShortcut(props);
runStrongTyped(props);
System.out.println("Wrote, read, and deleted "+Product.class.getName()+" using JAXB");
}
public static void runShortcut(ExampleProperties props) throws JAXBException {
// register the POJO classes
DatabaseClientFactory.getHandleRegistry().register(
JAXBHandle.newFactory(Product.class)
);
DatabaseClient client = Util.newClient(props);
// create a manager for XML documents
XMLDocumentManager docMgr = client.newXMLDocumentManager();
// create an instance of the POJO class
Product product = new Product();
product.setName("FashionForward");
product.setIndustry("Retail");
product.setDescription(
"(Shortcut) Creates demand with high prices, hours from midnight to dawn, and frequent moves");
// create an identifier for the document
String docId = "/example/"+product.getName()+".xml";
// write the POJO as the document content
docMgr.writeAs(docId, product);
// ... at some other time ...
// read the POJO from the document content
product = docMgr.readAs(docId, Product.class);
// log the persisted XML document
System.out.println(docMgr.readAs(docId, String.class));
// delete the document
docMgr.delete(docId);
// release the client
client.release();
}
public static void runStrongTyped(ExampleProperties props) throws JAXBException {
DatabaseClient client = Util.newClient(props);
JAXBContext context = JAXBContext.newInstance(Product.class);
// create a manager for XML documents
XMLDocumentManager docMgr = client.newXMLDocumentManager();
// create a handle for a POJO class marshaled by the JAXB context
// Note: to use a single handle for any of the POJO classes
// identified to the JAXB context, use a <?> wildcard instead of
// a specific class and call the get(class) method instead of
// the get() method.
JAXBHandle<Product> handle = new JAXBHandle<>(context);
// create an instance of the POJO class
Product product = new Product();
product.setName("FashionForward");
product.setIndustry("Retail");
product.setDescription(
"(Strong Typed) Creates demand with high prices, hours from midnight to dawn, and frequent moves");
// create an identifier for the document
String docId = "/example/"+product.getName()+".xml";
// set the handle to the POJO instance
handle.set(product);
// write the POJO as the document content
docMgr.write(docId, handle);
// ... at some other time ...
// read the POJO from the document content
docMgr.read(docId, handle);
// access the document content
product = handle.get();
// ... do something with the POJO ...
// read the persisted XML document for the logging message
System.out.println(docMgr.read(docId, new StringHandle()).get());
// delete the document
docMgr.delete(docId);
// release the client
client.release();
}
}