-
Notifications
You must be signed in to change notification settings - Fork 2
/
CDataJaxbExample.java
74 lines (61 loc) · 2.54 KB
/
CDataJaxbExample.java
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
/**
* Copyright (C) 2015 Michael Schnell. All rights reserved.
* http://www.fuin.org/
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option) any
* later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see http://www.gnu.org/licenses/.
*/
package org.fuin.utils4j.examples;
import jakarta.xml.bind.JAXBContext;
import org.fuin.utils4j.jaxb.CDataXmlAdapter;
import org.fuin.utils4j.jaxb.CDataXmlStreamWriter;
import org.fuin.utils4j.jaxb.MyClassWithCData;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;
import java.io.StringWriter;
import static org.fuin.utils4j.jaxb.JaxbUtils.marshal;
import static org.fuin.utils4j.jaxb.JaxbUtils.unmarshal;
/**
* Short example for using the {@link CDataXmlStreamWriter} together with the {@link CDataXmlAdapter}.
*/
// CHECKSTYLE:OFF
public class CDataJaxbExample {
/**
* Executes the example.
*
* @param args
* Not used.
*/
public static void main(String[] args) throws Exception {
// Create writers
final StringWriter writer = new StringWriter();
final XMLStreamWriter xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(writer);
final CDataXmlStreamWriter cdataWriter = new CDataXmlStreamWriter(xmlWriter);
// Create JAXB context with example class
final JAXBContext ctx = JAXBContext.newInstance(MyClassWithCData.class);
final MyClassWithCData testee = new MyClassWithCData("<whatever this=\"is\"/>");
// Convert instance to XML
marshal(ctx, testee, null, cdataWriter);
final String xml = writer.toString();
// Prints out the result
System.out.println(xml);
// <?xml version="1.0" ?><my-class-with-cdata><![CDATA[<whatever
// this="is"/>]]></my-class-with-cdata>
// Convert it back to object
final MyClassWithCData copy = unmarshal(xml, MyClassWithCData.class);
// Print out cdata content
System.out.println(copy.getContent());
// <whatever this="is"/>
}
}
// CHECKSTYLE:ON