forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaReadXmlJaxbEx.java
More file actions
29 lines (21 loc) · 906 Bytes
/
JavaReadXmlJaxbEx.java
File metadata and controls
29 lines (21 loc) · 906 Bytes
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
package com.zetcode;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
public class JavaReadXmlJaxbEx {
private static final String BOOKSTORE_XML = "src/main/resources/bookstore.xml";
public static void main(String[] args) throws JAXBException, FileNotFoundException {
// create JAXB context and unmarshaller
var context = JAXBContext.newInstance(BookStore.class);
var um = context.createUnmarshaller();
var bookstore = (BookStore) um.unmarshal(new InputStreamReader(
new FileInputStream(BOOKSTORE_XML), StandardCharsets.UTF_8));
var bookList = bookstore.getBooksList();
bookList.forEach((book) -> {
System.out.println(book);
});
}
}