Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BAEL-2338 #6150

Merged
merged 10 commits into from
Mar 13, 2019
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions xml/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/convert-xml-to-html-in-java)
16 changes: 16 additions & 0 deletions xml/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
16 changes: 16 additions & 0 deletions xml/src/main/java/com/baeldung/xmlhtml/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.baeldung.xmlhtml;

import com.baeldung.xmlhtml.helpers.XMLRunner;

/*
* @author Adam In Tae Gerard
*
* Main method.
*/

public class Application {

public static void main(String[] args) {
XMLRunner.doWork();
}
}
46 changes: 46 additions & 0 deletions xml/src/main/java/com/baeldung/xmlhtml/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.baeldung.xmlhtml;

/*
* @Author Adam InTae Gerard
*/

public class Constants {

/**
* File-related 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";

/**
* Generic exceptions constants.
*/

public static final String EXCEPTION_ENCOUNTERED = "Oops! You done's broke'd it! Error: ";

/**
* Generic logging constants.
*/

public static final String LINE_BREAK = System.getProperty("line.separator");
public static final String WHITE_SPACE = " ";

/**
* SAX Parser console constants.
*/

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!";

/**
* STAX prefixes.
*/

public static final String BREAK = "\n";
public static final String TAB = "\t";
}
27 changes: 27 additions & 0 deletions xml/src/main/java/com/baeldung/xmlhtml/helpers/XMLRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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;

/**
* @author Adam In Tae Gerard
*
* Core executable code in here.
*/

public class XMLRunner {

public static void doWork() {

JAXBHelper.example();

JAXPHelper.saxParser();
JAXPHelper.documentBuilder();

STAXHelper.write(STAXHelper.read());

}

}
107 changes: 107 additions & 0 deletions xml/src/main/java/com/baeldung/xmlhtml/helpers/jaxb/JAXBHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
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.*;

/**
* @author Adam In Tae Gerard
* <p>
* All JAXB logic in here :)
*/

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);
//https://stackoverflow.com/questions/277996/remove-standalone-yes-from-generated-xml
marshaller.setProperty("jaxb.fragment", Boolean.TRUE);
} catch (Exception ex) {
System.out.println(EXCEPTION_ENCOUNTERED + ex);
}

return marshaller;
}

public static void example() {

try {

/**
* Cast to desired POJO.
*/

XMLExample xml = (XMLExample) JAXBHelper.getContextUnmarshaller(XMLExample.class).unmarshal(new File(XML_FILE_IN));
JAXBHelper.print(xml.toString());
//JAXBHelper.print(xml.getAncestor().getDescendantOne().getValue());
//JAXBHelper.print(xml.getAncestor().getDescendantTwo().getDescendantThree().getValue());

/**
* Transfer exchanged to writeable POJO.
*/

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);

/**
* Marshall into XML (HTML).
*/

JAXBHelper.getContextMarshaller(ExampleHTML.class).marshal(html, new File(JAXB_FILE_OUT));

} catch (Exception ex) {
System.out.println(EXCEPTION_ENCOUNTERED + ex);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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 {

/**
* Start document event.
*/

public void startDocument() {
//System.out.println(DOCUMENT_START);
}


/**
* New element event.
*/

public void startElement(String uri, String localName, String qName, org.xml.sax.Attributes atts) {
//System.out.println(ELEMENT_START);
}

/**
* End document event.
*/

public void endDocument() {
System.out.println(DOCUMENT_END);
}

/**
* End element event.
*/

public void endElement(String uri, String localName, String qName) {
//System.out.println(ELEMENT_END);
}

/**
* Other methods.
*/

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) { }

}
Loading