* BAEL-2338

* Update Application.java

* merged and updated

* updated pom.xml

* improved and updated

* corrected per request

* removed annotations

* formatting
This commit is contained in:
Adam InTae Gerard 2019-03-12 20:55:59 -07:00 committed by KevinGilmore
parent 65cbf46e6b
commit 57a1096c1f
26 changed files with 715 additions and 0 deletions

View File

@ -4,3 +4,4 @@
- [XML Libraries Support in Java](http://www.baeldung.com/java-xml-libraries)
- [DOM parsing with Xerces](http://www.baeldung.com/java-xerces-dom-parsing)
- [Write an org.w3.dom.Document to a File](https://www.baeldung.com/java-write-xml-document-file)
- [Convert XML to HTML](https://www.baeldung.com/)

View File

@ -30,6 +30,22 @@
<version>${jdom2.version}</version>
</dependency>
<dependency>
<groupId>javax.xml</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>javax.xml</groupId>
<artifactId>jaxp-api</artifactId>
<version>1.4.2</version>
</dependency>
<dependency>
<groupId>javax.xml.stream</groupId>
<artifactId>stax-api</artifactId>
<version>1.0-2</version>
</dependency>
<!-- utils -->
<dependency>
<groupId>commons-io</groupId>

View File

@ -0,0 +1,10 @@
package com.baeldung.xmlhtml;
import com.baeldung.xmlhtml.helpers.XMLRunner;
public class Application {
public static void main(String[] args) {
XMLRunner.doWork();
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.xmlhtml;
public class Constants {
public static final String XML_FILE_IN = "src/main/resources/xml/in.xml";
public static final String JAXB_FILE_OUT = "src/main/resources/xml/jaxb.html";
public static final String JAXP_FILE_OUT = "src/main/resources/xml/jaxp.html";
public static final String STAX_FILE_OUT = "src/main/resources/xml/stax.html";
public static final String EXCEPTION_ENCOUNTERED = "Generic exception! Error: ";
public static final String LINE_BREAK = System.getProperty("line.separator");
public static final String WHITE_SPACE = " ";
public static final String DOCUMENT_START = "Document parsing has begun!";
public static final String DOCUMENT_END = "Document parsing has ended!";
public static final String ELEMENT_START = "Element parsing has begun!";
public static final String ELEMENT_END = "Element parsing has ended!";
public static final String BREAK = "\n";
public static final String TAB = "\t";
}

View File

@ -0,0 +1,16 @@
package com.baeldung.xmlhtml.helpers;
import com.baeldung.xmlhtml.helpers.jaxb.JAXBHelper;
import com.baeldung.xmlhtml.helpers.jaxp.JAXPHelper;
import com.baeldung.xmlhtml.helpers.stax.STAXHelper;
public class XMLRunner {
public static void doWork() {
JAXBHelper.example();
JAXPHelper.saxParser();
JAXPHelper.documentBuilder();
STAXHelper.write(STAXHelper.read());
}
}

View File

@ -0,0 +1,77 @@
package com.baeldung.xmlhtml.helpers.jaxb;
import com.baeldung.xmlhtml.pojo.jaxb.html.ExampleHTML;
import com.baeldung.xmlhtml.pojo.jaxb.html.elements.Body;
import com.baeldung.xmlhtml.pojo.jaxb.html.elements.CustomElement;
import com.baeldung.xmlhtml.pojo.jaxb.html.elements.Meta;
import com.baeldung.xmlhtml.pojo.jaxb.html.elements.NestedElement;
import com.baeldung.xmlhtml.pojo.jaxb.xml.XMLExample;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import static com.baeldung.xmlhtml.Constants.*;
public class JAXBHelper {
private static void print(String xmlContent) {
System.out.println(xmlContent);
}
private static Unmarshaller getContextUnmarshaller(Class clazz) {
Unmarshaller unmarshaller = null;
try {
JAXBContext context = JAXBContext.newInstance(clazz);
unmarshaller = context.createUnmarshaller();
} catch (Exception ex) {
System.out.println(EXCEPTION_ENCOUNTERED + ex);
}
return unmarshaller;
}
private static Marshaller getContextMarshaller(Class clazz) {
Marshaller marshaller = null;
try {
JAXBContext context = JAXBContext.newInstance(clazz);
marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty("jaxb.fragment", Boolean.TRUE);
} catch (Exception ex) {
System.out.println(EXCEPTION_ENCOUNTERED + ex);
}
return marshaller;
}
public static void example() {
try {
XMLExample xml = (XMLExample) JAXBHelper.getContextUnmarshaller(XMLExample.class).unmarshal(new File(XML_FILE_IN));
JAXBHelper.print(xml.toString());
ExampleHTML html = new ExampleHTML();
Body body = new Body();
CustomElement customElement = new CustomElement();
NestedElement nested = new NestedElement();
CustomElement child = new CustomElement();
customElement.setValue("descendantOne: " + xml.getAncestor().getDescendantOne().getValue());
child.setValue("descendantThree: " + xml.getAncestor().getDescendantTwo().getDescendantThree().getValue());
nested.setCustomElement(child);
body.setCustomElement(customElement);
body.setNestedElement(nested);
Meta meta = new Meta();
meta.setTitle("example");
html.getHead().add(meta);
html.setBody(body);
JAXBHelper.getContextMarshaller(ExampleHTML.class).marshal(html, new File(JAXB_FILE_OUT));
} catch (Exception ex) {
System.out.println(EXCEPTION_ENCOUNTERED + ex);
}
}
}

View File

@ -0,0 +1,34 @@
package com.baeldung.xmlhtml.helpers.jaxp;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import static com.baeldung.xmlhtml.Constants.*;
public class CustomHandler implements ContentHandler {
public void startDocument() {}
public void startElement(String uri, String localName, String qName, org.xml.sax.Attributes atts) { }
public void endDocument() {
System.out.println(DOCUMENT_END);
}
public void endElement(String uri, String localName, String qName) { }
public void startPrefixMapping(String prefix, String uri) { }
public void endPrefixMapping(String prefix) { }
public void setDocumentLocator(Locator locator) { }
public void characters(char[] ch, int start, int length) { }
public void ignorableWhitespace(char[] ch, int start, int length) { }
public void processingInstruction(String target, String data) { }
public void skippedEntity(String name) { }
}

View File

@ -0,0 +1,90 @@
package com.baeldung.xmlhtml.helpers.jaxp;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.XMLReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import static com.baeldung.xmlhtml.Constants.*;
public class JAXPHelper {
private static void print(Document document) {
NodeList list = document.getChildNodes();
try {
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
String message =
node.getNodeType()
+ WHITE_SPACE
+ node.getNodeName()
+ LINE_BREAK;
System.out.println(message);
}
} catch (Exception ex) {
System.out.println(EXCEPTION_ENCOUNTERED + ex);
}
}
public static void saxParser() {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
try {
SAXParser saxParser = spf.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.setContentHandler(new CustomHandler());
xmlReader.parse(XML_FILE_IN);
} catch (Exception ex) {
System.out.println(EXCEPTION_ENCOUNTERED + ex);
}
}
public static void documentBuilder() {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
Document parsed = db.parse(new File(XML_FILE_IN));
Element xml = parsed.getDocumentElement();
Document doc = db.newDocument();
Element html = doc.createElement("html");
Element head = doc.createElement("head");
html.appendChild(head);
Element body = doc.createElement("body");
Element descendantOne = doc.createElement("p");
descendantOne.setTextContent("descendantOne: " +
xml.getElementsByTagName("descendantOne")
.item(0).getTextContent());
Element descendantThree = doc.createElement("p");
descendantThree.setTextContent("descendantThree: " +
xml.getElementsByTagName("descendantThree")
.item(0).getTextContent());
Element nested = doc.createElement("div");
nested.appendChild(descendantThree);
body.appendChild(descendantOne);
body.appendChild(nested);
html.appendChild(body);
doc.appendChild(html);
TransformerFactory tFactory = TransformerFactory.newInstance();
tFactory
.newTransformer()
.transform(new DOMSource(doc), new StreamResult(new File(JAXP_FILE_OUT)));
} catch (Exception ex) {
System.out.println(EXCEPTION_ENCOUNTERED + ex);
}
}
}

View File

@ -0,0 +1,112 @@
package com.baeldung.xmlhtml.helpers.stax;
import com.baeldung.xmlhtml.pojo.stax.Body;
import com.baeldung.xmlhtml.pojo.stax.CustomElement;
import com.baeldung.xmlhtml.pojo.stax.NestedElement;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import static com.baeldung.xmlhtml.Constants.*;
public class STAXHelper {
private static XMLStreamReader reader() {
XMLStreamReader xmlStreamReader = null;
try {
xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream(XML_FILE_IN));
} catch (Exception ex) {
System.out.println(EXCEPTION_ENCOUNTERED + ex);
}
return xmlStreamReader;
}
private static XMLStreamWriter writer() {
XMLStreamWriter xmlStreamWriter = null;
try {
xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(new FileWriter(STAX_FILE_OUT));
} catch (Exception ex) {
System.out.println(EXCEPTION_ENCOUNTERED + ex);
}
return xmlStreamWriter;
}
public static Body read() {
Body body = new Body();
try {
XMLStreamReader xmlStreamReader = reader();
CustomElement customElement = new CustomElement();
NestedElement nestedElement = new NestedElement();
CustomElement child = new CustomElement();
while (xmlStreamReader.hasNext()) {
xmlStreamReader.next();
if (xmlStreamReader.isStartElement()) {
System.out.println(xmlStreamReader.getLocalName());
if (xmlStreamReader.getLocalName().equals("descendantOne")) {
customElement.setValue(xmlStreamReader.getElementText());
}
if (xmlStreamReader.getLocalName().equals("descendantThree")) {
child.setValue(xmlStreamReader.getElementText());
}
}
}
nestedElement.setCustomElement(child);
body.setCustomElement(customElement);
body.setNestedElement(nestedElement);
xmlStreamReader.close();
} catch (Exception ex) {
System.out.println(EXCEPTION_ENCOUNTERED + ex);
}
return body;
}
public static void write(Body body) {
try {
XMLStreamWriter xmlStreamWriter = writer();
xmlStreamWriter.writeStartElement("html");
xmlStreamWriter.writeCharacters(BREAK + TAB);
xmlStreamWriter.writeStartElement("head");
xmlStreamWriter.writeCharacters(BREAK + TAB + TAB);
xmlStreamWriter.writeStartElement("meta");
xmlStreamWriter.writeEndElement();
xmlStreamWriter.writeCharacters(BREAK + TAB);
xmlStreamWriter.writeEndElement();
xmlStreamWriter.writeCharacters(BREAK + TAB);
xmlStreamWriter.writeStartElement("body");
xmlStreamWriter.writeCharacters(BREAK + TAB + TAB);
xmlStreamWriter.writeStartElement("p");
xmlStreamWriter.writeCharacters("descendantOne: " + body.getCustomElement().getValue());
xmlStreamWriter.writeEndElement();
xmlStreamWriter.writeCharacters(BREAK + TAB + TAB);
xmlStreamWriter.writeStartElement("div");
xmlStreamWriter.writeCharacters(BREAK + TAB + TAB + TAB);
xmlStreamWriter.writeStartElement("p");
xmlStreamWriter.writeCharacters(BREAK);
xmlStreamWriter.writeCharacters(TAB + TAB + TAB + TAB + "descendantThree: " + body.getNestedElement().getCustomElement().getValue());
xmlStreamWriter.writeCharacters(BREAK + TAB + TAB + TAB);
xmlStreamWriter.writeEndElement();
xmlStreamWriter.writeCharacters(BREAK + TAB + TAB);
xmlStreamWriter.writeEndElement();
xmlStreamWriter.writeCharacters(BREAK + TAB);
xmlStreamWriter.writeEndElement();
xmlStreamWriter.writeCharacters(BREAK);
xmlStreamWriter.writeEndDocument();
xmlStreamWriter.flush();
xmlStreamWriter.close();
} catch (Exception ex) {
System.out.println(EXCEPTION_ENCOUNTERED + ex);
}
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.xmlhtml.pojo.jaxb.html;
import com.baeldung.xmlhtml.pojo.jaxb.html.elements.Body;
import com.baeldung.xmlhtml.pojo.jaxb.html.elements.Meta;
import javax.xml.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@XmlType(propOrder = {"head", "body"})
@XmlRootElement(name = "html")
public class ExampleHTML {
private List<Meta> head = new ArrayList<>();
private Body body;
public ExampleHTML() { }
public List<Meta> getHead() {
return head;
}
@XmlElementWrapper(name = "head")
@XmlElement(name = "meta")
public void setHead(List<Meta> head) {
this.head = head;
}
public Body getBody() {
return body;
}
@XmlElement(name = "body")
public void setBody(Body body) {
this.body = body;
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.xmlhtml.pojo.jaxb.html.elements;
import javax.xml.bind.annotation.XmlElement;
public class Body {
private CustomElement customElement;
public CustomElement getCustomElement() {
return customElement;
}
@XmlElement(name = "p")
public void setCustomElement(CustomElement customElement) {
this.customElement = customElement;
}
private NestedElement nestedElement;
public NestedElement getNestedElement() {
return nestedElement;
}
@XmlElement(name = "div")
public void setNestedElement(NestedElement nestedElement) {
this.nestedElement = nestedElement;
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.xmlhtml.pojo.jaxb.html.elements;
import javax.xml.bind.annotation.XmlValue;
public class CustomElement {
private String value;
@XmlValue
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}

View File

@ -0,0 +1,30 @@
package com.baeldung.xmlhtml.pojo.jaxb.html.elements;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlValue;
public class Meta {
private String title;
public String getTitle() {
return title;
}
@XmlAttribute(name = "title")
public void setTitle(String title) {
this.title = title;
}
private String value;
@XmlValue
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.xmlhtml.pojo.jaxb.html.elements;
import javax.xml.bind.annotation.XmlElement;
public class NestedElement {
private CustomElement customElement;
public CustomElement getCustomElement() {
return customElement;
}
@XmlElement(name = "p")
public void setCustomElement(CustomElement customElement) {
this.customElement = customElement;
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.xmlhtml.pojo.jaxb.xml;
import com.baeldung.xmlhtml.pojo.jaxb.xml.elements.Ancestor;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "xmlexample")
public class XMLExample {
private Ancestor ancestor;
@XmlElement(name = "ancestor")
public void setAncestor(Ancestor ancestor) {
this.ancestor = ancestor;
}
public Ancestor getAncestor() {
return ancestor;
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.xmlhtml.pojo.jaxb.xml.elements;
import javax.xml.bind.annotation.XmlElement;
public class Ancestor {
private DescendantOne descendantOne;
private DescendantTwo descendantTwo;
public DescendantOne getDescendantOne() {
return descendantOne;
}
@XmlElement(name = "descendantOne")
public void setDescendantOne(DescendantOne descendantOne) {
this.descendantOne = descendantOne;
}
public DescendantTwo getDescendantTwo() {
return descendantTwo;
}
@XmlElement(name = "descendantTwo")
public void setDescendantTwo(DescendantTwo descendantTwo) {
this.descendantTwo = descendantTwo;
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.xmlhtml.pojo.jaxb.xml.elements;
import javax.xml.bind.annotation.XmlValue;
public class DescendantOne {
private String value;
@XmlValue
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.xmlhtml.pojo.jaxb.xml.elements;
import javax.xml.bind.annotation.XmlValue;
public class DescendantThree {
private String value;
@XmlValue
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.xmlhtml.pojo.jaxb.xml.elements;
import javax.xml.bind.annotation.XmlElement;
public class DescendantTwo {
private DescendantThree descendantThree;
public DescendantThree getDescendantThree() {
return descendantThree;
}
@XmlElement(name = "descendantThree")
public void setDescendant(DescendantThree descendantThree) {
this.descendantThree = descendantThree;
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.xmlhtml.pojo.stax;
public class Body {
private CustomElement customElement;
public CustomElement getCustomElement() {
return customElement;
}
public void setCustomElement(CustomElement customElement) {
this.customElement = customElement;
}
private NestedElement nestedElement;
public NestedElement getNestedElement() {
return nestedElement;
}
public void setNestedElement(NestedElement nestedElement) {
this.nestedElement = nestedElement;
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.xmlhtml.pojo.stax;
public class CustomElement {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.xmlhtml.pojo.stax;
public class NestedElement {
private CustomElement customElement;
public CustomElement getCustomElement() {
return customElement;
}
public void setCustomElement(CustomElement customElement) {
this.customElement = customElement;
}
}

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xmlexample>
<ancestor>
<descendantOne>Yo</descendantOne>
<descendantTwo>
<descendantThree>
DustyOrb
</descendantThree>
</descendantTwo>
</ancestor>
</xmlexample>

View File

@ -0,0 +1,14 @@
<html>
<head>
<meta title="example"/>
</head>
<body>
<p>descendantOne: Yo</p>
<div>
<p>descendantThree:
DustyOrb
</p>
</div>
</body>
</html>

View File

@ -0,0 +1,13 @@
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>descendantOne: Yo</p>
<div>
<p>descendantThree:
DustyOrb
</p>
</div>
</body>
</html>

View File

@ -0,0 +1,15 @@
<html>
<head>
<meta></meta>
</head>
<body>
<p>descendantOne: Yo</p>
<div>
<p>
descendantThree:
DustyOrb
</p>
</div>
</body>
</html>