Add docs for xml process
This commit is contained in:
parent
387d88d36c
commit
3aa0921688
|
@ -0,0 +1 @@
|
|||
.idea
|
|
@ -0,0 +1,14 @@
|
|||
## XML
|
||||
|
||||
本页面中有关的内容是关于 eXtensible Markup Language (XML) 的。
|
||||
|
||||
### 相关文章:
|
||||
- [Intro to XPath with Java](https://www.baeldung.com/java-xpath)
|
||||
- [Introduction to JiBX](https://www.baeldung.com/jibx)
|
||||
- [XML Libraries Support in Java](https://www.baeldung.com/java-xml-libraries)
|
||||
- [Working with XML Files in Java Using DOM Parsing](https://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)
|
||||
- [Modifying an XML Attribute in Java](https://www.baeldung.com/java-modify-xml-attribute)
|
||||
- [Convert XML to HTML in Java](https://www.baeldung.com/java-convert-xml-to-html)
|
||||
- [Parsing an XML File Using StAX](https://www.baeldung.com/java-stax)
|
||||
- [Parsing an XML File Using SAX Parser](https://www.baeldung.com/java-sax-parser)
|
|
@ -0,0 +1,385 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>xml</artifactId>
|
||||
<version>${parent.version}</version>
|
||||
<name>xml</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.ossez</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>0.0.2-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- xml libraries -->
|
||||
<dependency>
|
||||
<groupId>org.dom4j</groupId>
|
||||
<artifactId>dom4j</artifactId>
|
||||
<version>${dom4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>jaxen</groupId>
|
||||
<artifactId>jaxen</artifactId>
|
||||
<version>${jaxen.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jooq</groupId>
|
||||
<artifactId>joox-java-6</artifactId>
|
||||
<version>${joox.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jdom</groupId>
|
||||
<artifactId>jdom2</artifactId>
|
||||
<version>${jdom2.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.xml.bind</groupId>
|
||||
<artifactId>jaxb-api</artifactId>
|
||||
<version>${jaxb-api.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.xml.bind</groupId>
|
||||
<artifactId>jaxb-impl</artifactId>
|
||||
<version>${jaxb-impl.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.xml.bind</groupId>
|
||||
<artifactId>jaxb-core</artifactId>
|
||||
<version>${jaxb-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.xml</groupId>
|
||||
<artifactId>jaxp-api</artifactId>
|
||||
<version>${jaxp-api.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.xml.stream</groupId>
|
||||
<artifactId>stax-api</artifactId>
|
||||
<version>${stax-api.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.freemarker</groupId>
|
||||
<artifactId>freemarker</artifactId>
|
||||
<version>${freemarker.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.spullara.mustache.java</groupId>
|
||||
<artifactId>compiler</artifactId>
|
||||
<version>${mustache.version}</version>
|
||||
</dependency>
|
||||
<!-- JMH Libraries -->
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
<version>${jmh-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh-generator.version}</version>
|
||||
</dependency>
|
||||
<!-- utils -->
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>${commons-io.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>${commons-collections4.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jibx</groupId>
|
||||
<artifactId>jibx-run</artifactId>
|
||||
<version>${jibx-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-lang</groupId>
|
||||
<artifactId>commons-lang</artifactId>
|
||||
<version>${commons-lang.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.xmlunit</groupId>
|
||||
<artifactId>xmlunit-assertj</artifactId>
|
||||
<version>${xmlunit-assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>1.2.5</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>xml</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.jibx</groupId>
|
||||
<artifactId>maven-jibx-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.jibx</groupId>
|
||||
<artifactId>maven-jibx-plugin</artifactId>
|
||||
<version>${maven-jibx-plugin.version}</version>
|
||||
<configuration>
|
||||
<directory>src/main/resources</directory>
|
||||
<includes>
|
||||
<includes>*-binding.xml</includes>
|
||||
</includes>
|
||||
<excludes>
|
||||
<exclude>template-binding.xml</exclude>
|
||||
</excludes>
|
||||
<schemaBindingDirectory>src/main/resources</schemaBindingDirectory>
|
||||
<includeSchemaBindings>
|
||||
<includeSchemaBindings>*-binding.xml</includeSchemaBindings>
|
||||
</includeSchemaBindings>
|
||||
<verbose>true</verbose>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>process-classes</id>
|
||||
<phase>process-classes</phase>
|
||||
<goals>
|
||||
<goal>bind</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>process-test-classes</id>
|
||||
<phase>process-test-classes</phase>
|
||||
<goals>
|
||||
<goal>test-bind</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>schemaGen</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.jibx</groupId>
|
||||
<artifactId>maven-jibx-plugin</artifactId>
|
||||
<version>${maven-jibx-plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>generate-java-code-from-schema</id>
|
||||
<goals>
|
||||
<goal>schema-codegen</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<directory>src/main/resources</directory>
|
||||
<includes>
|
||||
<include>Order.xsd</include>
|
||||
</includes>
|
||||
<verbose>true</verbose>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>compile-binding</id>
|
||||
<goals>
|
||||
<goal>bind</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<directory>target/generated-sources</directory>
|
||||
<load>true</load>
|
||||
<validate>true</validate>
|
||||
<!--<verbose>true</verbose> -->
|
||||
<verify>true</verify>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>generate-test-code-from-schema</id>
|
||||
<phase>generate-test-sources</phase>
|
||||
<goals>
|
||||
<goal>test-schema-codegen</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>compile-test-binding</id>
|
||||
<phase>process-test-classes</phase>
|
||||
<goals>
|
||||
<goal>test-bind</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<directory>target/generated-test-sources</directory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>CustomerTest.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
<profile>
|
||||
<id>bindGen</id>
|
||||
<build>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.jibx</groupId>
|
||||
<artifactId>maven-jibx-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.jibx</groupId>
|
||||
<artifactId>maven-jibx-plugin</artifactId>
|
||||
<version>${maven-jibx-plugin.version}</version>
|
||||
<configuration>
|
||||
<directory>src/main/resources</directory>
|
||||
<includes>
|
||||
<includes>*-binding.xml</includes>
|
||||
</includes>
|
||||
<excludes>
|
||||
<exclude>template-binding.xml</exclude>
|
||||
</excludes>
|
||||
<schemaBindingDirectory>src/main/resources</schemaBindingDirectory>
|
||||
<includeSchemaBindings>
|
||||
<includeSchemaBindings>*-binding.xml</includeSchemaBindings>
|
||||
</includeSchemaBindings>
|
||||
<verbose>true</verbose>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>process-classes</id>
|
||||
<phase>process-classes</phase>
|
||||
<goals>
|
||||
<goal>bind</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>process-test-classes</id>
|
||||
<phase>process-test-classes</phase>
|
||||
<goals>
|
||||
<goal>test-bind</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<!-- NOTE: We don't need a groupId specification because the group is org.apache.maven.plugins
|
||||
...which is assumed by default. -->
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<configuration>
|
||||
<archiveBaseDirectory>${project.basedir}</archiveBaseDirectory>
|
||||
<descriptorRefs>
|
||||
<descriptorRef>
|
||||
jar-with-dependencies
|
||||
</descriptorRef>
|
||||
</descriptorRefs>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>
|
||||
com.baeldung.xml.jibx.JiBXDemoApplication
|
||||
</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>make-assembly</id><!-- this is used for inheritance merges -->
|
||||
<phase>package</phase><!-- append to the packaging phase. -->
|
||||
<goals>
|
||||
<goal>attached</goal><!-- goals == mojos -->
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
<properties>
|
||||
<dom4j.version>2.1.1</dom4j.version>
|
||||
<jaxen.version>1.2.0</jaxen.version>
|
||||
<jdom2.version>2.0.6</jdom2.version>
|
||||
<joox.version>1.6.2</joox.version>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<jibx-version>1.2.4.5</jibx-version>
|
||||
<jaxb-api.version>2.3.1</jaxb-api.version>
|
||||
<jaxp-api.version>1.4.2</jaxp-api.version>
|
||||
<jaxb-core.version>2.3.0.1</jaxb-core.version>
|
||||
<jaxb-impl.version>2.3.2</jaxb-impl.version>
|
||||
<stax-api.version>1.0-2</stax-api.version>
|
||||
<assertj-core.version>3.12.2</assertj-core.version>
|
||||
<xmlunit-assertj.version>2.6.3</xmlunit-assertj.version>
|
||||
<junit-jupiter.version>5.5.0</junit-jupiter.version>
|
||||
<freemarker.version>2.3.29</freemarker.version>
|
||||
<mustache.version>0.9.6</mustache.version>
|
||||
<!-- util -->
|
||||
<commons-lang.version>2.4</commons-lang.version>
|
||||
<!-- maven plugins -->
|
||||
<maven-jibx-plugin.version>1.3.1</maven-jibx-plugin.version>
|
||||
<maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,120 @@
|
|||
package com.ossez.sax;
|
||||
|
||||
import org.xml.sax.Attributes;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.helpers.DefaultHandler;
|
||||
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.parsers.SAXParser;
|
||||
import javax.xml.parsers.SAXParserFactory;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SaxParserMain {
|
||||
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
|
||||
SAXParserFactory factory = SAXParserFactory.newInstance();
|
||||
SAXParser saxParser = factory.newSAXParser();
|
||||
|
||||
BaeldungHandler baeldungHandler = new BaeldungHandler();
|
||||
saxParser.parse("xml/src/main/resources/sax/baeldung.xml", baeldungHandler);
|
||||
System.out.println(baeldungHandler.getWebsite());
|
||||
}
|
||||
|
||||
public static class BaeldungHandler extends DefaultHandler {
|
||||
private static final String ARTICLES = "articles";
|
||||
private static final String ARTICLE = "article";
|
||||
private static final String TITLE = "title";
|
||||
private static final String CONTENT = "content";
|
||||
|
||||
private Baeldung website;
|
||||
private StringBuilder elementValue;
|
||||
|
||||
@Override
|
||||
public void characters(char[] ch, int start, int length) throws SAXException {
|
||||
if (elementValue == null) {
|
||||
elementValue = new StringBuilder();
|
||||
} else {
|
||||
elementValue.append(ch, start, length);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startDocument() throws SAXException {
|
||||
website = new Baeldung();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
|
||||
switch (qName) {
|
||||
case ARTICLES:
|
||||
website.setArticleList(new ArrayList<>());
|
||||
break;
|
||||
case ARTICLE:
|
||||
website.getArticleList().add(new BaeldungArticle());
|
||||
break;
|
||||
case TITLE:
|
||||
elementValue = new StringBuilder();
|
||||
break;
|
||||
case CONTENT:
|
||||
elementValue = new StringBuilder();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endElement(String uri, String localName, String qName) throws SAXException {
|
||||
switch (qName) {
|
||||
case TITLE:
|
||||
latestArticle().setTitle(elementValue.toString());
|
||||
break;
|
||||
case CONTENT:
|
||||
latestArticle().setContent(elementValue.toString());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private BaeldungArticle latestArticle() {
|
||||
List<BaeldungArticle> articleList = website.getArticleList();
|
||||
int latestArticleIndex = articleList.size() - 1;
|
||||
return articleList.get(latestArticleIndex);
|
||||
}
|
||||
|
||||
public Baeldung getWebsite() {
|
||||
return website;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Baeldung {
|
||||
private List<BaeldungArticle> articleList;
|
||||
|
||||
public void setArticleList(List<BaeldungArticle> articleList) {
|
||||
this.articleList = articleList;
|
||||
}
|
||||
|
||||
public List<BaeldungArticle> getArticleList() {
|
||||
return this.articleList;
|
||||
}
|
||||
}
|
||||
|
||||
public static class BaeldungArticle {
|
||||
private String title;
|
||||
private String content;
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return this.title;
|
||||
}
|
||||
|
||||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return this.content;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,193 @@
|
|||
package com.ossez.xml;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
|
||||
import javax.xml.namespace.NamespaceContext;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.xpath.XPath;
|
||||
import javax.xml.xpath.XPathConstants;
|
||||
import javax.xml.xpath.XPathExpressionException;
|
||||
import javax.xml.xpath.XPathFactory;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
public class DefaultParser {
|
||||
|
||||
private File file;
|
||||
|
||||
public DefaultParser(File file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
public NodeList getFirstLevelNodeList() {
|
||||
NodeList nodeList = null;
|
||||
try {
|
||||
FileInputStream fileIS = new FileInputStream(this.getFile());
|
||||
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
|
||||
|
||||
DocumentBuilder builder = builderFactory.newDocumentBuilder();
|
||||
|
||||
Document xmlDocument = builder.parse(fileIS);
|
||||
|
||||
XPath xPath = XPathFactory.newInstance().newXPath();
|
||||
|
||||
String expression = "/tutorials/tutorial";
|
||||
|
||||
nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
|
||||
|
||||
} catch (SAXException | IOException | ParserConfigurationException | XPathExpressionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return nodeList;
|
||||
}
|
||||
|
||||
public Node getNodeById(String id) {
|
||||
Node node = null;
|
||||
try {
|
||||
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
|
||||
|
||||
DocumentBuilder builder = builderFactory.newDocumentBuilder();
|
||||
|
||||
Document xmlDocument = builder.parse(this.getFile());
|
||||
|
||||
XPath xPath = XPathFactory.newInstance().newXPath();
|
||||
|
||||
String expression = "/tutorials/tutorial[@tutId=" + "'" + id + "'" + "]";
|
||||
|
||||
node = (Node) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODE);
|
||||
|
||||
} catch (SAXException | IOException | ParserConfigurationException | XPathExpressionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
public NodeList getNodeListByTitle(String name) {
|
||||
NodeList nodeList = null;
|
||||
try {
|
||||
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
|
||||
|
||||
DocumentBuilder builder = builderFactory.newDocumentBuilder();
|
||||
|
||||
Document xmlDocument = builder.parse(this.getFile());
|
||||
|
||||
this.clean(xmlDocument);
|
||||
|
||||
XPath xPath = XPathFactory.newInstance().newXPath();
|
||||
|
||||
String expression = "//tutorial[descendant::title[text()=" + "'" + name + "'" + "]]";
|
||||
|
||||
nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
|
||||
|
||||
} catch (SAXException | IOException | ParserConfigurationException | XPathExpressionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return nodeList;
|
||||
}
|
||||
|
||||
public NodeList getElementsByDate(String date) {
|
||||
NodeList nodeList = null;
|
||||
|
||||
try {
|
||||
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
|
||||
|
||||
DocumentBuilder builder = builderFactory.newDocumentBuilder();
|
||||
|
||||
Document xmlDocument = builder.parse(this.getFile());
|
||||
|
||||
this.clean(xmlDocument);
|
||||
|
||||
XPath xPath = XPathFactory.newInstance().newXPath();
|
||||
|
||||
String expression = "//tutorial[number(translate(date, '/', '')) > " + date + "]";
|
||||
|
||||
nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
|
||||
|
||||
} catch (SAXException | IOException | ParserConfigurationException | XPathExpressionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return nodeList;
|
||||
}
|
||||
|
||||
public NodeList getAllTutorials() {
|
||||
NodeList nodeList = null;
|
||||
try {
|
||||
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
|
||||
builderFactory.setNamespaceAware(true);
|
||||
DocumentBuilder builder = builderFactory.newDocumentBuilder();
|
||||
|
||||
Document xmlDocument = builder.parse(this.getFile());
|
||||
|
||||
this.clean(xmlDocument);
|
||||
|
||||
XPath xPath = XPathFactory.newInstance().newXPath();
|
||||
|
||||
xPath.setNamespaceContext(new NamespaceContext() {
|
||||
|
||||
@Override
|
||||
public Iterator getPrefixes(String arg0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPrefix(String arg0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getNamespaceURI(String arg0) {
|
||||
if ("bdn".equals(arg0)) {
|
||||
return "http://www.baeldung.com/full_archive";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
String expression = "/bdn:tutorials/bdn:tutorial";
|
||||
|
||||
nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
|
||||
|
||||
} catch (SAXException | IOException | ParserConfigurationException | XPathExpressionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return nodeList;
|
||||
}
|
||||
|
||||
private void clean(Node node) {
|
||||
|
||||
NodeList childs = node.getChildNodes();
|
||||
|
||||
for (int n = childs.getLength() - 1; n >= 0; n--) {
|
||||
Node child = childs.item(n);
|
||||
short nodeType = child.getNodeType();
|
||||
|
||||
if (nodeType == Node.ELEMENT_NODE)
|
||||
clean(child);
|
||||
else if (nodeType == Node.TEXT_NODE) {
|
||||
String trimmedNodeVal = child.getNodeValue().trim();
|
||||
if (trimmedNodeVal.length() == 0)
|
||||
node.removeChild(child);
|
||||
else
|
||||
child.setNodeValue(trimmedNodeVal);
|
||||
} else if (nodeType == Node.COMMENT_NODE)
|
||||
node.removeChild(child);
|
||||
}
|
||||
}
|
||||
|
||||
public File getFile() {
|
||||
return file;
|
||||
}
|
||||
|
||||
public void setFile(File file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,130 @@
|
|||
package com.ossez.xml;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.dom4j.Document;
|
||||
import org.dom4j.DocumentException;
|
||||
import org.dom4j.DocumentHelper;
|
||||
import org.dom4j.Element;
|
||||
import org.dom4j.Node;
|
||||
import org.dom4j.io.OutputFormat;
|
||||
import org.dom4j.io.SAXReader;
|
||||
import org.dom4j.io.XMLWriter;
|
||||
|
||||
public class Dom4JParser {
|
||||
|
||||
private File file;
|
||||
|
||||
public Dom4JParser(File file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
public Element getRootElement() {
|
||||
try {
|
||||
SAXReader reader = new SAXReader();
|
||||
Document document = reader.read(file);
|
||||
return document.getRootElement();
|
||||
} catch (DocumentException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public List<Element> getFirstElementList() {
|
||||
try {
|
||||
SAXReader reader = new SAXReader();
|
||||
Document document = reader.read(file);
|
||||
return document.getRootElement().elements();
|
||||
} catch (DocumentException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Node getNodeById(String id) {
|
||||
try {
|
||||
SAXReader reader = new SAXReader();
|
||||
Document document = reader.read(file);
|
||||
List<Node> elements = document.selectNodes("//*[@tutId='" + id + "']");
|
||||
return elements.get(0);
|
||||
} catch (DocumentException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Node getElementsListByTitle(String name) {
|
||||
try {
|
||||
SAXReader reader = new SAXReader();
|
||||
Document document = reader.read(file);
|
||||
List<Node> elements = document.selectNodes("//tutorial[descendant::title[text()=" + "'" + name + "'" + "]]");
|
||||
return elements.get(0);
|
||||
} catch (DocumentException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void generateModifiedDocument() {
|
||||
try {
|
||||
SAXReader reader = new SAXReader();
|
||||
Document document = reader.read(file);
|
||||
List<Node> nodes = document.selectNodes("/tutorials/tutorial");
|
||||
for (Node node : nodes) {
|
||||
Element element = (Element) node;
|
||||
Iterator<Element> iterator = element.elementIterator("title");
|
||||
while (iterator.hasNext()) {
|
||||
Element title = (Element) iterator.next();
|
||||
title.setText(title.getText() + " updated");
|
||||
}
|
||||
}
|
||||
XMLWriter writer = new XMLWriter(new FileWriter(new File("src/test/resources/example_dom4j_updated.xml")));
|
||||
writer.write(document);
|
||||
writer.close();
|
||||
} catch (DocumentException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void generateNewDocument() {
|
||||
try {
|
||||
Document document = DocumentHelper.createDocument();
|
||||
Element root = document.addElement("XMLTutorials");
|
||||
Element tutorialElement = root.addElement("tutorial").addAttribute("tutId", "01");
|
||||
tutorialElement.addAttribute("type", "xml");
|
||||
|
||||
tutorialElement.addElement("title").addText("XML with Dom4J");
|
||||
|
||||
tutorialElement.addElement("description").addText("XML handling with Dom4J");
|
||||
|
||||
tutorialElement.addElement("date").addText("14/06/2016");
|
||||
|
||||
tutorialElement.addElement("author").addText("Dom4J tech writer");
|
||||
|
||||
OutputFormat format = OutputFormat.createPrettyPrint();
|
||||
XMLWriter writer = new XMLWriter(new FileWriter(new File("src/test/resources/example_dom4j_new.xml")), format);
|
||||
writer.write(document);
|
||||
writer.close();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public File getFile() {
|
||||
return file;
|
||||
}
|
||||
|
||||
public void setFile(File file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
package com.ossez.xml;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import org.jdom2.Document;
|
||||
import org.jdom2.Element;
|
||||
import org.jdom2.JDOMException;
|
||||
import org.jdom2.filter.Filters;
|
||||
import org.jdom2.input.SAXBuilder;
|
||||
import org.jdom2.xpath.XPathExpression;
|
||||
import org.jdom2.xpath.XPathFactory;
|
||||
|
||||
public class JDomParser {
|
||||
|
||||
private File file;
|
||||
|
||||
public JDomParser(File file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
public List<Element> getAllTitles() {
|
||||
try {
|
||||
SAXBuilder builder = new SAXBuilder();
|
||||
Document doc = builder.build(this.getFile());
|
||||
Element tutorials = doc.getRootElement();
|
||||
List<Element> titles = tutorials.getChildren("tutorial");
|
||||
return titles;
|
||||
} catch (JDOMException | IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Element getNodeById(String id) {
|
||||
try {
|
||||
SAXBuilder builder = new SAXBuilder();
|
||||
Document document = (Document) builder.build(file);
|
||||
String filter = "//*[@tutId='" + id + "']";
|
||||
XPathFactory xFactory = XPathFactory.instance();
|
||||
XPathExpression<Element> expr = xFactory.compile(filter, Filters.element());
|
||||
List<Element> node = expr.evaluate(document);
|
||||
|
||||
return node.get(0);
|
||||
} catch (JDOMException | IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public File getFile() {
|
||||
return file;
|
||||
}
|
||||
|
||||
public void setFile(File file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
package com.ossez.xml;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Marshaller;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
|
||||
import com.ossez.xml.binding.Tutorial;
|
||||
import com.ossez.xml.binding.Tutorials;
|
||||
|
||||
public class JaxbParser {
|
||||
|
||||
private File file;
|
||||
|
||||
public JaxbParser(File file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
public Tutorials getFullDocument() {
|
||||
try {
|
||||
JAXBContext jaxbContext = JAXBContext.newInstance(Tutorials.class);
|
||||
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
|
||||
Tutorials tutorials = (Tutorials) jaxbUnmarshaller.unmarshal(this.getFile());
|
||||
return tutorials;
|
||||
} catch (JAXBException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void createNewDocument() {
|
||||
Tutorials tutorials = new Tutorials();
|
||||
tutorials.setTutorial(new ArrayList<Tutorial>());
|
||||
Tutorial tut = new Tutorial();
|
||||
tut.setTutId("01");
|
||||
tut.setType("XML");
|
||||
tut.setTitle("XML with Jaxb");
|
||||
tut.setDescription("XML Binding with Jaxb");
|
||||
tut.setDate("04/02/2015");
|
||||
tut.setAuthor("Jaxb author");
|
||||
tutorials.getTutorial().add(tut);
|
||||
|
||||
try {
|
||||
JAXBContext jaxbContext = JAXBContext.newInstance(Tutorials.class);
|
||||
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
|
||||
|
||||
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
|
||||
|
||||
jaxbMarshaller.marshal(tutorials, file);
|
||||
|
||||
} catch (JAXBException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public File getFile() {
|
||||
return file;
|
||||
}
|
||||
|
||||
public void setFile(File file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
package com.ossez.xml;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
|
||||
import org.jaxen.JaxenException;
|
||||
import org.jaxen.XPath;
|
||||
import org.jaxen.dom.DOMXPath;
|
||||
import org.w3c.dom.Document;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
public class JaxenDemo {
|
||||
|
||||
private File file;
|
||||
|
||||
public JaxenDemo(File file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
public List getAllTutorial() {
|
||||
try {
|
||||
FileInputStream fileIS = new FileInputStream(this.getFile());
|
||||
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
|
||||
|
||||
DocumentBuilder builder = builderFactory.newDocumentBuilder();
|
||||
|
||||
Document xmlDocument = builder.parse(fileIS);
|
||||
|
||||
String expression = "/tutorials/tutorial";
|
||||
|
||||
XPath path = new DOMXPath(expression);
|
||||
List result = path.selectNodes(xmlDocument);
|
||||
return result;
|
||||
|
||||
} catch (SAXException | IOException | ParserConfigurationException | JaxenException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public File getFile() {
|
||||
return file;
|
||||
}
|
||||
|
||||
public void setFile(File file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,120 @@
|
|||
package com.ossez.xml;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import javax.xml.stream.XMLEventReader;
|
||||
import javax.xml.stream.XMLInputFactory;
|
||||
import javax.xml.stream.XMLStreamConstants;
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
import javax.xml.stream.events.Attribute;
|
||||
import javax.xml.stream.events.Characters;
|
||||
import javax.xml.stream.events.EndElement;
|
||||
import javax.xml.stream.events.StartElement;
|
||||
import javax.xml.stream.events.XMLEvent;
|
||||
|
||||
import com.ossez.xml.binding.Tutorial;
|
||||
|
||||
public class StaxParser {
|
||||
|
||||
private File file;
|
||||
|
||||
public StaxParser(File file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
public List<Tutorial> getAllTutorial() {
|
||||
boolean bTitle = false;
|
||||
boolean bDescription = false;
|
||||
boolean bDate = false;
|
||||
boolean bAuthor = false;
|
||||
List<Tutorial> tutorials = new ArrayList<Tutorial>();
|
||||
try {
|
||||
XMLInputFactory factory = XMLInputFactory.newInstance();
|
||||
XMLEventReader eventReader = factory.createXMLEventReader(new FileReader(this.getFile()));
|
||||
Tutorial current = null;
|
||||
while (eventReader.hasNext()) {
|
||||
XMLEvent event = eventReader.nextEvent();
|
||||
switch (event.getEventType()) {
|
||||
case XMLStreamConstants.START_ELEMENT:
|
||||
StartElement startElement = event.asStartElement();
|
||||
String qName = startElement.getName().getLocalPart();
|
||||
if (qName.equalsIgnoreCase("tutorial")) {
|
||||
current = new Tutorial();
|
||||
Iterator<Attribute> attributes = startElement.getAttributes();
|
||||
while (attributes.hasNext()) {
|
||||
Attribute currentAt = attributes.next();
|
||||
if (currentAt.getName().toString().equalsIgnoreCase("tutId")) {
|
||||
current.setTutId(currentAt.getValue());
|
||||
} else if (currentAt.getName().toString().equalsIgnoreCase("type")) {
|
||||
current.setType(currentAt.getValue());
|
||||
}
|
||||
}
|
||||
} else if (qName.equalsIgnoreCase("title")) {
|
||||
bTitle = true;
|
||||
} else if (qName.equalsIgnoreCase("description")) {
|
||||
bDescription = true;
|
||||
} else if (qName.equalsIgnoreCase("date")) {
|
||||
bDate = true;
|
||||
} else if (qName.equalsIgnoreCase("author")) {
|
||||
bAuthor = true;
|
||||
}
|
||||
break;
|
||||
case XMLStreamConstants.CHARACTERS:
|
||||
Characters characters = event.asCharacters();
|
||||
if (bTitle) {
|
||||
if (current != null) {
|
||||
current.setTitle(characters.getData());
|
||||
}
|
||||
bTitle = false;
|
||||
}
|
||||
if (bDescription) {
|
||||
if (current != null) {
|
||||
current.setDescription(characters.getData());
|
||||
}
|
||||
bDescription = false;
|
||||
}
|
||||
if (bDate) {
|
||||
if (current != null) {
|
||||
current.setDate(characters.getData());
|
||||
}
|
||||
bDate = false;
|
||||
}
|
||||
if (bAuthor) {
|
||||
if (current != null) {
|
||||
current.setAuthor(characters.getData());
|
||||
}
|
||||
bAuthor = false;
|
||||
}
|
||||
break;
|
||||
case XMLStreamConstants.END_ELEMENT:
|
||||
EndElement endElement = event.asEndElement();
|
||||
if (endElement.getName().getLocalPart().equalsIgnoreCase("tutorial")) {
|
||||
if (current != null) {
|
||||
tutorials.add(current);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
} catch (FileNotFoundException | XMLStreamException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return tutorials;
|
||||
}
|
||||
|
||||
public File getFile() {
|
||||
return file;
|
||||
}
|
||||
|
||||
public void setFile(File file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.ossez.xml;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.xml.transform.OutputKeys;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerConfigurationException;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
public class XMLDocumentWriter {
|
||||
|
||||
public void write(Document document, String fileName, boolean excludeDeclaration, boolean prettyPrint) {
|
||||
try(FileWriter writer = new FileWriter(new File(fileName))) {
|
||||
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
||||
Transformer transformer = transformerFactory.newTransformer();
|
||||
if(excludeDeclaration) {
|
||||
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
|
||||
}
|
||||
if(prettyPrint) {
|
||||
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
|
||||
}
|
||||
DOMSource source = new DOMSource(document);
|
||||
StreamResult result = new StreamResult(writer);
|
||||
transformer.transform(source, result);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
} catch (TransformerConfigurationException e) {
|
||||
throw new IllegalStateException(e);
|
||||
} catch (TransformerException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
package com.ossez.xml.attribute;
|
||||
|
||||
import org.dom4j.*;
|
||||
import org.dom4j.io.DocumentSource;
|
||||
import org.dom4j.io.SAXReader;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import javax.xml.XMLConstants;
|
||||
import javax.xml.transform.OutputKeys;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.util.List;
|
||||
|
||||
public class Dom4jTransformer {
|
||||
private final Document input;
|
||||
|
||||
public Dom4jTransformer(String resourcePath) throws DocumentException, SAXException {
|
||||
// 1- Build the doc from the XML file
|
||||
SAXReader xmlReader = new SAXReader();
|
||||
xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
|
||||
xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", false);
|
||||
xmlReader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
|
||||
this.input = xmlReader.read(resourcePath);
|
||||
}
|
||||
|
||||
public String modifyAttribute(String attribute, String oldValue, String newValue) throws TransformerException {
|
||||
// 2- Locate the node(s) with xpath, we can use index and iterator too.
|
||||
String expr = String.format("//*[contains(@%s, '%s')]", attribute, oldValue);
|
||||
XPath xpath = DocumentHelper.createXPath(expr);
|
||||
List<Node> nodes = xpath.selectNodes(input);
|
||||
// 3- Make the change on the selected nodes
|
||||
for (int i = 0; i < nodes.size(); i++) {
|
||||
Element element = (Element) nodes.get(i);
|
||||
element.addAttribute(attribute, newValue);
|
||||
}
|
||||
// 4- Save the result to a new XML doc
|
||||
TransformerFactory factory = TransformerFactory.newInstance();
|
||||
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
|
||||
Transformer xformer = factory.newTransformer();
|
||||
xformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||
Writer output = new StringWriter();
|
||||
xformer.transform(new DocumentSource(input), new StreamResult(output));
|
||||
return output.toString();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
package com.ossez.xml.attribute;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
|
||||
import javax.xml.XMLConstants;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.transform.OutputKeys;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.TransformerFactoryConfigurationError;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
import javax.xml.xpath.XPath;
|
||||
import javax.xml.xpath.XPathConstants;
|
||||
import javax.xml.xpath.XPathExpressionException;
|
||||
import javax.xml.xpath.XPathFactory;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
public class JaxpTransformer {
|
||||
|
||||
private Document input;
|
||||
|
||||
public JaxpTransformer(String resourcePath) throws SAXException, IOException, ParserConfigurationException {
|
||||
// 1- Build the doc from the XML file
|
||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
|
||||
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
|
||||
this.input = factory.newDocumentBuilder()
|
||||
.parse(resourcePath);
|
||||
}
|
||||
|
||||
public String modifyAttribute(String attribute, String oldValue, String newValue) throws XPathExpressionException, TransformerFactoryConfigurationError, TransformerException {
|
||||
// 2- Locate the node(s) with xpath
|
||||
XPath xpath = XPathFactory.newInstance()
|
||||
.newXPath();
|
||||
NodeList nodes = (NodeList) xpath.evaluate(String.format("//*[contains(@%s, '%s')]", attribute, oldValue), this.input, XPathConstants.NODESET);
|
||||
// 3- Make the change on the selected nodes
|
||||
for (int i = 0; i < nodes.getLength(); i++) {
|
||||
Element value = (Element) nodes.item(i);
|
||||
value.setAttribute(attribute, newValue);
|
||||
}
|
||||
// Stream api syntax
|
||||
// IntStream
|
||||
// .range(0, nodes.getLength())
|
||||
// .mapToObj(i -> (Element) nodes.item(i))
|
||||
// .forEach(value -> value.setAttribute(attribute, newValue));
|
||||
// 4- Save the result to a new XML doc
|
||||
TransformerFactory factory = TransformerFactory.newInstance();
|
||||
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
|
||||
Transformer xformer = factory.newTransformer();
|
||||
xformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||
Writer output = new StringWriter();
|
||||
xformer.transform(new DOMSource(this.input), new StreamResult(output));
|
||||
return output.toString();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package com.ossez.xml.attribute;
|
||||
|
||||
import org.joox.JOOX;
|
||||
import org.joox.Match;
|
||||
import org.w3c.dom.Document;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.transform.TransformerFactoryConfigurationError;
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.joox.JOOX.$;
|
||||
|
||||
public class JooxTransformer {
|
||||
|
||||
private final Document input;
|
||||
|
||||
public JooxTransformer(String resourcePath) throws SAXException, IOException {
|
||||
// 1- Build the doc from the XML file
|
||||
DocumentBuilder builder = JOOX.builder();
|
||||
input = builder.parse(resourcePath);
|
||||
}
|
||||
|
||||
public String modifyAttribute(String attribute, String oldValue, String newValue) throws TransformerFactoryConfigurationError {
|
||||
// 2- Select the document
|
||||
Match $ = $(input);
|
||||
// 3 - Find node to modify
|
||||
String expr = String.format("//*[contains(@%s, '%s')]", attribute, oldValue);
|
||||
$
|
||||
// .find("to") or with xpath
|
||||
.xpath(expr)
|
||||
.get()
|
||||
.stream()
|
||||
.forEach(e -> e.setAttribute(attribute, newValue));
|
||||
// 4- Return result as String
|
||||
return $.toString();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
package com.ossez.xml.attribute.jmh;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.xpath.XPathExpressionException;
|
||||
|
||||
import com.ossez.xml.attribute.JaxpTransformer;
|
||||
import org.dom4j.DocumentException;
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||
import org.openjdk.jmh.annotations.Scope;
|
||||
import org.openjdk.jmh.annotations.State;
|
||||
import org.openjdk.jmh.runner.Runner;
|
||||
import org.openjdk.jmh.runner.RunnerException;
|
||||
import org.openjdk.jmh.runner.options.Options;
|
||||
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import com.ossez.xml.attribute.Dom4jTransformer;
|
||||
import com.ossez.xml.attribute.JooxTransformer;
|
||||
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
||||
@State(Scope.Benchmark)
|
||||
public class AttributeBenchMark {
|
||||
|
||||
public static void main(String[] args) throws RunnerException {
|
||||
Options opt = new OptionsBuilder().include(AttributeBenchMark.class.getSimpleName())
|
||||
.forks(1)
|
||||
.build();
|
||||
new Runner(opt).run();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public String dom4jBenchmark() throws DocumentException, TransformerException, SAXException {
|
||||
String path = this.getClass()
|
||||
.getResource("/xml/attribute.xml")
|
||||
.toString();
|
||||
Dom4jTransformer transformer = new Dom4jTransformer(path);
|
||||
String attribute = "customer";
|
||||
String oldValue = "true";
|
||||
String newValue = "false";
|
||||
|
||||
return transformer.modifyAttribute(attribute, oldValue, newValue);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public String jooxBenchmark() throws IOException, SAXException {
|
||||
String path = this.getClass()
|
||||
.getResource("/xml/attribute.xml")
|
||||
.toString();
|
||||
JooxTransformer transformer = new JooxTransformer(path);
|
||||
String attribute = "customer";
|
||||
String oldValue = "true";
|
||||
String newValue = "false";
|
||||
|
||||
return transformer.modifyAttribute(attribute, oldValue, newValue);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public String jaxpBenchmark() throws TransformerException, ParserConfigurationException, SAXException, IOException, XPathExpressionException {
|
||||
String path = this.getClass()
|
||||
.getResource("/xml/attribute.xml")
|
||||
.toString();
|
||||
JaxpTransformer transformer = new JaxpTransformer(path);
|
||||
String attribute = "customer";
|
||||
String oldValue = "true";
|
||||
String newValue = "false";
|
||||
|
||||
return transformer.modifyAttribute(attribute, oldValue, newValue);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
package com.ossez.xml.binding;
|
||||
|
||||
import javax.xml.bind.annotation.XmlAttribute;
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
|
||||
public class Tutorial {
|
||||
|
||||
private String tutId;
|
||||
private String type;
|
||||
private String title;
|
||||
private String description;
|
||||
private String date;
|
||||
private String author;
|
||||
|
||||
public String getTutId() {
|
||||
return tutId;
|
||||
}
|
||||
|
||||
@XmlAttribute
|
||||
public void setTutId(String tutId) {
|
||||
this.tutId = tutId;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@XmlAttribute
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
@XmlElement
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
@XmlElement
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
@XmlElement
|
||||
public void setDate(String date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
@XmlElement
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.ossez.xml.binding;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
@XmlRootElement
|
||||
public class Tutorials {
|
||||
|
||||
private List<Tutorial> tutorial;
|
||||
|
||||
public List<Tutorial> getTutorial() {
|
||||
return tutorial;
|
||||
}
|
||||
|
||||
@XmlElement
|
||||
public void setTutorial(List<Tutorial> tutorial) {
|
||||
this.tutorial = tutorial;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
* Customer.java 06.06.2008
|
||||
*
|
||||
* Copyright 2008 Stefan Jäger
|
||||
*
|
||||
*/
|
||||
package com.ossez.xml.jibx;
|
||||
|
||||
import org.apache.commons.lang.builder.ToStringBuilder;
|
||||
|
||||
public class Customer {
|
||||
private Person person;
|
||||
private String city;
|
||||
|
||||
private Phone homePhone;
|
||||
private Phone officePhone;
|
||||
|
||||
public Person getPerson() {
|
||||
return person;
|
||||
}
|
||||
|
||||
public void setPerson(Person person) {
|
||||
this.person = person;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public Phone getHomePhone() {
|
||||
return homePhone;
|
||||
}
|
||||
|
||||
public void setHomePhone(Phone homePhone) {
|
||||
this.homePhone = homePhone;
|
||||
}
|
||||
|
||||
public Phone getOfficePhone() {
|
||||
return officePhone;
|
||||
}
|
||||
|
||||
public void setOfficePhone(Phone officePhone) {
|
||||
this.officePhone = officePhone;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return ToStringBuilder.reflectionToString(this);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.ossez.xml.jibx;
|
||||
|
||||
public class Identity {
|
||||
|
||||
long customerId;
|
||||
|
||||
public long getCustomerId() {
|
||||
return customerId;
|
||||
}
|
||||
|
||||
public void setCustomerId(long customerId) {
|
||||
this.customerId = customerId;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
/*
|
||||
* Person.java 06.06.2008
|
||||
*
|
||||
* Copyright 2008 Stefan Jäger
|
||||
*
|
||||
*/
|
||||
package com.ossez.xml.jibx;
|
||||
|
||||
import org.apache.commons.lang.builder.ToStringBuilder;
|
||||
|
||||
public class Person extends Identity {
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return ToStringBuilder.reflectionToString(this);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.ossez.xml.jibx;
|
||||
|
||||
public class Phone {
|
||||
|
||||
private String number;
|
||||
|
||||
public String getNumber() {
|
||||
return number;
|
||||
}
|
||||
|
||||
public void setNumber(String number) {
|
||||
this.number = number;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
package com.ossez.xml.stax;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javax.xml.stream.events.XMLEvent;
|
||||
import javax.xml.stream.XMLEventReader;
|
||||
import javax.xml.stream.XMLInputFactory;
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
import javax.xml.stream.events.Attribute;
|
||||
import javax.xml.stream.events.EndElement;
|
||||
import javax.xml.stream.events.StartElement;
|
||||
import javax.xml.namespace.QName;
|
||||
|
||||
public class StaxParser {
|
||||
|
||||
public static List<WebSite> parse(String path) {
|
||||
List<WebSite> websites = new ArrayList<WebSite>();
|
||||
WebSite website = null;
|
||||
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
|
||||
try {
|
||||
XMLEventReader reader = xmlInputFactory.createXMLEventReader(new FileInputStream(path));
|
||||
while (reader.hasNext()) {
|
||||
XMLEvent nextEvent = reader.nextEvent();
|
||||
if (nextEvent.isStartElement()) {
|
||||
StartElement startElement = nextEvent.asStartElement();
|
||||
switch (startElement.getName()
|
||||
.getLocalPart()) {
|
||||
case "website":
|
||||
website = new WebSite();
|
||||
Attribute url = startElement.getAttributeByName(new QName("url"));
|
||||
if (url != null) {
|
||||
website.setUrl(url.getValue());
|
||||
}
|
||||
break;
|
||||
case "name":
|
||||
nextEvent = reader.nextEvent();
|
||||
website.setName(nextEvent.asCharacters()
|
||||
.getData());
|
||||
break;
|
||||
case "category":
|
||||
nextEvent = reader.nextEvent();
|
||||
website.setCategory(nextEvent.asCharacters()
|
||||
.getData());
|
||||
break;
|
||||
case "status":
|
||||
nextEvent = reader.nextEvent();
|
||||
website.setStatus(nextEvent.asCharacters()
|
||||
.getData());
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (nextEvent.isEndElement()) {
|
||||
EndElement endElement = nextEvent.asEndElement();
|
||||
if (endElement.getName()
|
||||
.getLocalPart()
|
||||
.equals("website")) {
|
||||
websites.add(website);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (XMLStreamException xse) {
|
||||
System.out.println("XMLStreamException");
|
||||
xse.printStackTrace();
|
||||
} catch (FileNotFoundException fnfe) {
|
||||
System.out.println("FileNotFoundException");
|
||||
fnfe.printStackTrace();
|
||||
}
|
||||
return websites;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.ossez.xml.stax;
|
||||
public class WebSite {
|
||||
|
||||
private String url;
|
||||
private String name;
|
||||
private String category;
|
||||
private String status;
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getCategory() {
|
||||
return category;
|
||||
}
|
||||
|
||||
public void setCategory(String category) {
|
||||
this.category = category;
|
||||
}
|
||||
|
||||
public String getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(String status) {
|
||||
this.status = status;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.ossez.xmlhtml;
|
||||
|
||||
import com.ossez.xmlhtml.helpers.XMLRunner;
|
||||
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
XMLRunner.doWork();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.ossez.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";
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.ossez.xmlhtml.freemarker;
|
||||
|
||||
import com.ossez.xmlhtml.stax.StaxTransformer;
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.Template;
|
||||
import freemarker.template.TemplateException;
|
||||
import freemarker.template.TemplateExceptionHandler;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class FreemarkerTransformer {
|
||||
private StaxTransformer staxTransformer;
|
||||
private String templateDirectory;
|
||||
private String templateFile;
|
||||
|
||||
public FreemarkerTransformer(StaxTransformer staxTransformer, String templateDirectory, String templateFile) {
|
||||
this.staxTransformer = staxTransformer;
|
||||
this.templateDirectory = templateDirectory;
|
||||
this.templateFile = templateFile;
|
||||
}
|
||||
|
||||
public String html() throws IOException, TemplateException {
|
||||
Configuration cfg = new Configuration(Configuration.VERSION_2_3_29);
|
||||
cfg.setDirectoryForTemplateLoading(new File(templateDirectory));
|
||||
cfg.setDefaultEncoding(StandardCharsets.UTF_8.toString());
|
||||
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
|
||||
cfg.setLogTemplateExceptions(false);
|
||||
cfg.setWrapUncheckedExceptions(true);
|
||||
cfg.setFallbackOnNullLoopVariable(false);
|
||||
Template temp = cfg.getTemplate(templateFile);
|
||||
try (Writer output = new StringWriter()) {
|
||||
temp.process(staxTransformer.getMap(), output);
|
||||
return output.toString();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.ossez.xmlhtml.helpers;
|
||||
|
||||
|
||||
import com.ossez.xmlhtml.helpers.jaxb.JAXBHelper;
|
||||
|
||||
public class XMLRunner {
|
||||
|
||||
public static void doWork() {
|
||||
JAXBHelper.example();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
package com.ossez.xmlhtml.helpers.jaxb;
|
||||
|
||||
import com.ossez.xmlhtml.pojo.jaxb.html.ExampleHTML;
|
||||
import com.ossez.xmlhtml.pojo.jaxb.html.elements.Body;
|
||||
import com.ossez.xmlhtml.pojo.jaxb.html.elements.CustomElement;
|
||||
import com.ossez.xmlhtml.pojo.jaxb.html.elements.Meta;
|
||||
import com.ossez.xmlhtml.pojo.jaxb.html.elements.NestedElement;
|
||||
import com.ossez.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.ossez.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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,103 @@
|
|||
package com.ossez.xmlhtml.jaxp;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import javax.xml.XMLConstants;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class JaxpTransformer {
|
||||
|
||||
private Document input;
|
||||
private DocumentBuilderFactory factory;
|
||||
|
||||
public JaxpTransformer(String resourcePath) throws ParserConfigurationException, IOException, SAXException {
|
||||
// 1- Build the doc from the XML file
|
||||
factory = DocumentBuilderFactory.newInstance();
|
||||
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
|
||||
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
|
||||
input = factory
|
||||
.newDocumentBuilder()
|
||||
.parse(resourcePath);
|
||||
}
|
||||
|
||||
public String html() throws ParserConfigurationException, TransformerException, IOException {
|
||||
Element xml = input.getDocumentElement();
|
||||
Document doc = factory
|
||||
.newDocumentBuilder()
|
||||
.newDocument();
|
||||
//Build Map
|
||||
Map<String, String> map = buildMap(xml);
|
||||
//Head
|
||||
Element html = doc.createElement("html");
|
||||
html.setAttribute("lang", "en");
|
||||
Element head = buildHead(map, doc);
|
||||
html.appendChild(head);
|
||||
//Body
|
||||
Element body = buildBody(map, doc);
|
||||
html.appendChild(body);
|
||||
doc.appendChild(html);
|
||||
return String.format("<!DOCTYPE html>%n%s", applyTransformation(doc));
|
||||
}
|
||||
|
||||
private String applyTransformation(Document doc) throws TransformerException, IOException {
|
||||
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
||||
transformerFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
|
||||
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
|
||||
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
|
||||
try (Writer output = new StringWriter()) {
|
||||
Transformer transformer = transformerFactory.newTransformer();
|
||||
transformer.transform(new DOMSource(doc), new StreamResult(output));
|
||||
return output.toString();
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, String> buildMap(Element xml) {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("heading", xml
|
||||
.getElementsByTagName("heading")
|
||||
.item(0)
|
||||
.getTextContent());
|
||||
map.put("from", String.format("from: %s", xml
|
||||
.getElementsByTagName("from")
|
||||
.item(0)
|
||||
.getTextContent()));
|
||||
map.put("content", xml
|
||||
.getElementsByTagName("content")
|
||||
.item(0)
|
||||
.getTextContent());
|
||||
return map;
|
||||
}
|
||||
|
||||
private Element buildHead(Map<String, String> map, Document doc) {
|
||||
Element head = doc.createElement("head");
|
||||
Element title = doc.createElement("title");
|
||||
title.setTextContent(map.get("heading"));
|
||||
head.appendChild(title);
|
||||
return head;
|
||||
}
|
||||
|
||||
private Element buildBody(Map<String, String> map, Document doc) {
|
||||
Element body = doc.createElement("body");
|
||||
Element from = doc.createElement("p");
|
||||
from.setTextContent(map.get("from"));
|
||||
Element success = doc.createElement("p");
|
||||
success.setTextContent(map.get("content"));
|
||||
body.appendChild(from);
|
||||
body.appendChild(success);
|
||||
return body;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.ossez.xmlhtml.mustache;
|
||||
|
||||
import com.ossez.xmlhtml.stax.StaxTransformer;
|
||||
import com.github.mustachejava.DefaultMustacheFactory;
|
||||
import com.github.mustachejava.Mustache;
|
||||
import com.github.mustachejava.MustacheFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
|
||||
public class MustacheTransformer {
|
||||
private StaxTransformer staxTransformer;
|
||||
private String templateFile;
|
||||
|
||||
public MustacheTransformer(StaxTransformer staxTransformer, String templateFile) {
|
||||
this.staxTransformer = staxTransformer;
|
||||
this.templateFile = templateFile;
|
||||
}
|
||||
|
||||
public String html() throws IOException {
|
||||
MustacheFactory mf = new DefaultMustacheFactory();
|
||||
Mustache mustache = mf.compile(templateFile);
|
||||
try (Writer output = new StringWriter()) {
|
||||
mustache.execute(output, staxTransformer.getMap());
|
||||
output.flush();
|
||||
return output.toString();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package com.ossez.xmlhtml.pojo.jaxb.html;
|
||||
|
||||
import com.ossez.xmlhtml.pojo.jaxb.html.elements.Body;
|
||||
import com.ossez.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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.ossez.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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.ossez.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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.ossez.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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.ossez.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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.ossez.xmlhtml.pojo.jaxb.xml;
|
||||
|
||||
import com.ossez.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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.ossez.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;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
package com.ossez.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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
package com.ossez.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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.ossez.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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,99 @@
|
|||
package com.ossez.xmlhtml.stax;
|
||||
|
||||
import javax.xml.stream.*;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class StaxTransformer {
|
||||
|
||||
private Map<String, String> map;
|
||||
|
||||
public StaxTransformer(String resourcePath) throws IOException, XMLStreamException {
|
||||
XMLInputFactory factory = XMLInputFactory.newInstance();
|
||||
factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
|
||||
factory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
|
||||
XMLStreamReader input = null;
|
||||
try (FileInputStream file = new FileInputStream(resourcePath)) {
|
||||
input = factory.createXMLStreamReader(file);
|
||||
map = buildMap(input);
|
||||
} finally {
|
||||
if (input != null) {
|
||||
input.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String html() throws XMLStreamException, IOException {
|
||||
try (Writer output = new StringWriter()) {
|
||||
XMLStreamWriter writer = XMLOutputFactory
|
||||
.newInstance()
|
||||
.createXMLStreamWriter(output);
|
||||
//Head
|
||||
writer.writeDTD("<!DOCTYPE html>");
|
||||
writer.writeCharacters(String.format("%n"));
|
||||
writer.writeStartElement("html");
|
||||
writer.writeAttribute("lang", "en");
|
||||
writer.writeCharacters(String.format("%n"));
|
||||
writer.writeStartElement("head");
|
||||
writer.writeCharacters(String.format("%n"));
|
||||
writer.writeDTD("<META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
|
||||
writer.writeCharacters(String.format("%n"));
|
||||
writer.writeStartElement("title");
|
||||
writer.writeCharacters(map.get("heading"));
|
||||
writer.writeEndElement();
|
||||
writer.writeCharacters(String.format("%n"));
|
||||
writer.writeEndElement();
|
||||
writer.writeCharacters(String.format("%n"));
|
||||
//Body
|
||||
writer.writeStartElement("body");
|
||||
writer.writeCharacters(String.format("%n"));
|
||||
writer.writeStartElement("p");
|
||||
writer.writeCharacters(map.get("from"));
|
||||
writer.writeEndElement();
|
||||
writer.writeCharacters(String.format("%n"));
|
||||
writer.writeStartElement("p");
|
||||
writer.writeCharacters(map.get("content"));
|
||||
writer.writeEndElement();
|
||||
writer.writeCharacters(String.format("%n"));
|
||||
writer.writeEndElement();
|
||||
writer.writeCharacters(String.format("%n"));
|
||||
writer.writeEndDocument();
|
||||
writer.writeCharacters(String.format("%n"));
|
||||
writer.flush();
|
||||
return output.toString();
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, String> buildMap(XMLStreamReader input) throws XMLStreamException {
|
||||
Map<String, String> tempMap = new HashMap<>();
|
||||
while (input.hasNext()) {
|
||||
input.next();
|
||||
if (input.isStartElement()) {
|
||||
if (input
|
||||
.getLocalName()
|
||||
.equals("heading")) {
|
||||
tempMap.put("heading", input.getElementText());
|
||||
}
|
||||
if (input
|
||||
.getLocalName()
|
||||
.equals("from")) {
|
||||
tempMap.put("from", String.format("from: %s", input.getElementText()));
|
||||
}
|
||||
if (input
|
||||
.getLocalName()
|
||||
.equals("content")) {
|
||||
tempMap.put("content", input.getElementText());
|
||||
}
|
||||
}
|
||||
}
|
||||
return tempMap;
|
||||
}
|
||||
|
||||
public Map<String, String> getMap() {
|
||||
return map;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
<xs:schema attributeFormDefault="unqualified"
|
||||
elementFormDefault="qualified" targetNamespace="http://www.jibx.xml.baeldung.com/"
|
||||
xmlns:xs="http://www.w3.org/2001/XMLSchema">
|
||||
<xs:element name="Order">
|
||||
<xs:complexType>
|
||||
<xs:sequence>
|
||||
<xs:element name="Address" maxOccurs="unbounded"
|
||||
minOccurs="0">
|
||||
<xs:complexType>
|
||||
<xs:sequence>
|
||||
<xs:element type="xs:string" name="Name" />
|
||||
</xs:sequence>
|
||||
<xs:attribute type="xs:string" name="Type" use="optional" />
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
</xs:sequence>
|
||||
<xs:attribute type="xs:int" name="OrderNumber" />
|
||||
<xs:attribute type="xs:date" name="OrderDate" />
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
</xs:schema>
|
|
@ -0,0 +1,26 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<binding>
|
||||
<mapping class="com.ossez.xml.jibx.Identity" abstract="true">
|
||||
<value name="customer-id" field="customerId" />
|
||||
</mapping>
|
||||
|
||||
<mapping name="customer" class="com.ossez.xml.jibx.Customer">
|
||||
<structure field="person" />
|
||||
<structure name="home-phone" field="homePhone" usage="optional" />
|
||||
<structure name="office-phone" field="officePhone" usage="optional" />
|
||||
<value name="city" field="city" />
|
||||
</mapping>
|
||||
|
||||
<mapping name="person" class="com.ossez.xml.jibx.Person"
|
||||
extends="com.ossez.xml.jibx.Identity">
|
||||
<structure map-as="com.ossez.xml.jibx.Identity" />
|
||||
<value name="name" field="name" />
|
||||
|
||||
</mapping>
|
||||
|
||||
<mapping class="com.ossez.xml.jibx.Phone" abstract="true">
|
||||
<value name="number" field="number" />
|
||||
</mapping>
|
||||
|
||||
|
||||
</binding>
|
|
@ -0,0 +1,13 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
|
@ -0,0 +1,16 @@
|
|||
<baeldung>
|
||||
<articles>
|
||||
<article>
|
||||
<title>Parsing an XML File Using SAX Parser</title>
|
||||
<content>SAX Parser's Lorem ipsum...</content>
|
||||
</article>
|
||||
<article>
|
||||
<title>Parsing an XML File Using DOM Parser</title>
|
||||
<content>DOM Parser's Lorem ipsum...</content>
|
||||
</article>
|
||||
<article>
|
||||
<title>Parsing an XML File Using StAX Parser</title>
|
||||
<content>StAX Parser's Lorem ipsum...</content>
|
||||
</article>
|
||||
</articles>
|
||||
</baeldung>
|
|
@ -0,0 +1,5 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<notification id="5">
|
||||
<to customer="true">john@email.com</to>
|
||||
<from>mary@email.com</from>
|
||||
</notification>
|
|
@ -0,0 +1,5 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<notification id="5">
|
||||
<to customer="false">john@email.com</to>
|
||||
<from>mary@email.com</from>
|
||||
</notification>
|
|
@ -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>
|
|
@ -0,0 +1,14 @@
|
|||
|
||||
<html>
|
||||
<head>
|
||||
<meta title="example"/>
|
||||
</head>
|
||||
<body>
|
||||
<p>descendantOne: Yo</p>
|
||||
<div>
|
||||
<p>descendantThree:
|
||||
DustyOrb
|
||||
</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
|
@ -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>
|
|
@ -0,0 +1,15 @@
|
|||
<html>
|
||||
<head>
|
||||
<meta></meta>
|
||||
</head>
|
||||
<body>
|
||||
<p>descendantOne: Yo</p>
|
||||
<div>
|
||||
<p>
|
||||
descendantThree:
|
||||
DustyOrb
|
||||
|
||||
</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,44 @@
|
|||
package com.ossez.sax;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.parsers.SAXParser;
|
||||
import javax.xml.parsers.SAXParserFactory;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class SaxParserMainUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenAProperXMLFile_whenItIsParsed_ThenAnObjectContainsAllItsElements() throws IOException, SAXException, ParserConfigurationException {
|
||||
SAXParserFactory factory = SAXParserFactory.newInstance();
|
||||
SAXParser saxParser = factory.newSAXParser();
|
||||
|
||||
SaxParserMain.BaeldungHandler baeldungHandler = new SaxParserMain.BaeldungHandler();
|
||||
saxParser.parse("src/test/resources/sax/baeldung.xml", baeldungHandler);
|
||||
|
||||
SaxParserMain.Baeldung result = baeldungHandler.getWebsite();
|
||||
|
||||
assertNotNull(result);
|
||||
List<SaxParserMain.BaeldungArticle> articles = result.getArticleList();
|
||||
|
||||
assertNotNull(articles);
|
||||
assertEquals(3, articles.size());
|
||||
|
||||
SaxParserMain.BaeldungArticle articleOne = articles.get(0);
|
||||
assertEquals("Parsing an XML File Using SAX Parser", articleOne.getTitle());
|
||||
assertEquals("SAX Parser's Lorem ipsum...", articleOne.getContent());
|
||||
|
||||
SaxParserMain.BaeldungArticle articleTwo = articles.get(1);
|
||||
assertEquals("Parsing an XML File Using DOM Parser", articleTwo.getTitle());
|
||||
assertEquals("DOM Parser's Lorem ipsum...", articleTwo.getContent());
|
||||
|
||||
SaxParserMain.BaeldungArticle articleThree = articles.get(2);
|
||||
assertEquals("Parsing an XML File Using StAX Parser", articleThree.getTitle());
|
||||
assertEquals("StAX Parser's Lorem ipsum...", articleThree.getContent());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,82 @@
|
|||
package com.ossez.xml;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
public class DefaultParserUnitTest {
|
||||
|
||||
final String fileName = "src/test/resources/example_default_parser.xml";
|
||||
|
||||
final String fileNameSpace = "src/test/resources/example_default_parser_namespace.xml";
|
||||
|
||||
DefaultParser parser;
|
||||
|
||||
@Test
|
||||
public void getFirstLevelNodeListTest() {
|
||||
parser = new DefaultParser(new File(fileName));
|
||||
NodeList list = parser.getFirstLevelNodeList();
|
||||
|
||||
assertNotNull(list);
|
||||
assertTrue(list.getLength() == 4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getNodeListByTitleTest() {
|
||||
parser = new DefaultParser(new File(fileName));
|
||||
NodeList list = parser.getNodeListByTitle("XML");
|
||||
|
||||
for (int i = 0; null != list && i < list.getLength(); i++) {
|
||||
Node nod = list.item(i);
|
||||
assertEquals("java", nod.getAttributes().getNamedItem("type").getTextContent());
|
||||
assertEquals("02", nod.getAttributes().getNamedItem("tutId").getTextContent());
|
||||
assertEquals("XML", nod.getFirstChild().getTextContent());
|
||||
assertEquals("title", nod.getFirstChild().getNodeName());
|
||||
assertEquals("description", nod.getChildNodes().item(1).getNodeName());
|
||||
assertEquals("Introduction to XPath", nod.getChildNodes().item(1).getTextContent());
|
||||
assertEquals("author", nod.getLastChild().getNodeName());
|
||||
assertEquals("XMLAuthor", nod.getLastChild().getTextContent());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getNodeByIdTest() {
|
||||
parser = new DefaultParser(new File(fileName));
|
||||
Node node = parser.getNodeById("03");
|
||||
|
||||
String type = node.getAttributes().getNamedItem("type").getNodeValue();
|
||||
assertEquals("android", type);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getNodeListByDateTest() {
|
||||
parser = new DefaultParser(new File(fileName));
|
||||
NodeList list = parser.getNodeListByTitle("04022016");
|
||||
for (int i = 0; null != list && i < list.getLength(); i++) {
|
||||
Node nod = list.item(i);
|
||||
assertEquals("java", nod.getAttributes().getNamedItem("type").getTextContent());
|
||||
assertEquals("04", nod.getAttributes().getNamedItem("tutId").getTextContent());
|
||||
assertEquals("Spring", nod.getFirstChild().getTextContent());
|
||||
assertEquals("title", nod.getFirstChild().getNodeName());
|
||||
assertEquals("description", nod.getChildNodes().item(1).getNodeName());
|
||||
assertEquals("Introduction to Spring", nod.getChildNodes().item(1).getTextContent());
|
||||
assertEquals("author", nod.getLastChild().getNodeName());
|
||||
assertEquals("SpringAuthor", nod.getLastChild().getTextContent());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getNodeListWithNamespaceTest() {
|
||||
parser = new DefaultParser(new File(fileNameSpace));
|
||||
NodeList list = parser.getAllTutorials();
|
||||
assertNotNull(list);
|
||||
assertTrue(list.getLength() == 4);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,133 @@
|
|||
package com.ossez.xml;
|
||||
|
||||
import org.dom4j.Document;
|
||||
import org.dom4j.DocumentHelper;
|
||||
import org.dom4j.Element;
|
||||
import org.dom4j.Node;
|
||||
import org.dom4j.io.OutputFormat;
|
||||
import org.dom4j.io.XMLWriter;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class Dom4JParserUnitTest {
|
||||
private static final Logger logger = LoggerFactory.getLogger(Dom4JParserUnitTest.class);
|
||||
|
||||
final String fileName = "src/test/resources/example_dom4j.xml";
|
||||
|
||||
Dom4JParser parser;
|
||||
|
||||
@Test
|
||||
public void getRootElementTest() {
|
||||
parser = new Dom4JParser(new File(fileName));
|
||||
Element root = parser.getRootElement();
|
||||
|
||||
assertNotNull(root);
|
||||
assertTrue(root.elements().size() == 4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getFirstElementListTest() {
|
||||
parser = new Dom4JParser(new File(fileName));
|
||||
List<Element> firstList = parser.getFirstElementList();
|
||||
|
||||
assertNotNull(firstList);
|
||||
assertTrue(firstList.size() == 4);
|
||||
assertTrue(firstList.get(0).attributeValue("type").equals("java"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getElementByIdTest() {
|
||||
parser = new Dom4JParser(new File(fileName));
|
||||
Node element = parser.getNodeById("03");
|
||||
|
||||
String type = element.valueOf("@type");
|
||||
assertEquals("android", type);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getElementsListByTitleTest() {
|
||||
parser = new Dom4JParser(new File(fileName));
|
||||
Node element = parser.getElementsListByTitle("XML");
|
||||
|
||||
assertEquals("java", element.valueOf("@type"));
|
||||
assertEquals("02", element.valueOf("@tutId"));
|
||||
assertEquals("XML", element.selectSingleNode("title").getText());
|
||||
assertEquals("title", element.selectSingleNode("title").getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void generateModifiedDocumentTest() {
|
||||
parser = new Dom4JParser(new File(fileName));
|
||||
parser.generateModifiedDocument();
|
||||
|
||||
File generatedFile = new File("src/test/resources/example_dom4j_updated.xml");
|
||||
assertTrue(generatedFile.exists());
|
||||
|
||||
parser.setFile(generatedFile);
|
||||
Node element = parser.getNodeById("02");
|
||||
|
||||
assertEquals("XML updated", element.selectSingleNode("title").getText());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void generateNewDocumentTest() {
|
||||
parser = new Dom4JParser(new File(fileName));
|
||||
parser.generateNewDocument();
|
||||
|
||||
File newFile = new File("src/test/resources/example_dom4j_new.xml");
|
||||
assertTrue(newFile.exists());
|
||||
|
||||
parser.setFile(newFile);
|
||||
Node element = parser.getNodeById("01");
|
||||
|
||||
assertEquals("XML with Dom4J", element.selectSingleNode("title").getText());
|
||||
}
|
||||
|
||||
/**
|
||||
* testCreateXMLFromSystemProperties
|
||||
*/
|
||||
@Test
|
||||
public void testCreateXMLFromSystemProperties() {
|
||||
Document document = DocumentHelper.createDocument();
|
||||
Element root = document.addElement("system");
|
||||
|
||||
Properties properties = System.getProperties();
|
||||
for (Enumeration elements = properties.propertyNames(); elements
|
||||
.hasMoreElements(); ) {
|
||||
String name = (String) elements.nextElement();
|
||||
String value = properties.getProperty(name);
|
||||
Element element = root.addElement("property");
|
||||
element.addAttribute("name", name);
|
||||
element.addText(value);
|
||||
}
|
||||
|
||||
try {
|
||||
OutputFormat format = OutputFormat.createPrettyPrint();
|
||||
format.setEncoding("utf-8");
|
||||
|
||||
Writer out = new StringWriter();
|
||||
XMLWriter writer = new XMLWriter(out, format);
|
||||
writer.write(document);
|
||||
writer.close();
|
||||
logger.debug("{}", out);
|
||||
|
||||
} catch (IOException e) {
|
||||
logger.error("Write XML Error.", e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package com.ossez.xml;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import org.jdom2.Element;
|
||||
import org.junit.Test;
|
||||
|
||||
public class JDomParserUnitTest {
|
||||
|
||||
final String fileName = "src/test/resources/example_jdom.xml";
|
||||
|
||||
JDomParser parser;
|
||||
|
||||
@Test
|
||||
public void getFirstElementListTest() {
|
||||
parser = new JDomParser(new File(fileName));
|
||||
List<Element> firstList = parser.getAllTitles();
|
||||
|
||||
assertNotNull(firstList);
|
||||
assertTrue(firstList.size() == 4);
|
||||
assertTrue(firstList.get(0).getAttributeValue("type").equals("java"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getElementByIdTest() {
|
||||
parser = new JDomParser(new File(fileName));
|
||||
Element el = parser.getNodeById("03");
|
||||
|
||||
String type = el.getAttributeValue("type");
|
||||
assertEquals("android", type);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package com.ossez.xml;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.ossez.xml.binding.Tutorials;
|
||||
|
||||
public class JaxbParserUnitTest {
|
||||
|
||||
final String fileName = "src/test/resources/example_jaxb.xml";
|
||||
|
||||
JaxbParser parser;
|
||||
|
||||
@Test
|
||||
public void getFullDocumentTest() {
|
||||
parser = new JaxbParser(new File(fileName));
|
||||
Tutorials tutorials = parser.getFullDocument();
|
||||
|
||||
assertNotNull(tutorials);
|
||||
assertTrue(tutorials.getTutorial().size() == 4);
|
||||
assertTrue(tutorials.getTutorial().get(0).getType().equalsIgnoreCase("java"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createNewDocumentTest() {
|
||||
File newFile = new File("src/test/resources/example_jaxb_new.xml");
|
||||
parser = new JaxbParser(newFile);
|
||||
parser.createNewDocument();
|
||||
|
||||
assertTrue(newFile.exists());
|
||||
|
||||
Tutorials tutorials = parser.getFullDocument();
|
||||
|
||||
assertNotNull(tutorials);
|
||||
assertTrue(tutorials.getTutorial().size() == 1);
|
||||
assertTrue(tutorials.getTutorial().get(0).getTitle().equalsIgnoreCase("XML with Jaxb"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.ossez.xml;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class JaxenDemoUnitTest {
|
||||
|
||||
final String fileName = "src/test/resources/example_jaxen.xml";
|
||||
|
||||
JaxenDemo jaxenDemo;
|
||||
|
||||
@Test
|
||||
public void getFirstLevelNodeListTest() {
|
||||
jaxenDemo = new JaxenDemo(new File(fileName));
|
||||
List<?> list = jaxenDemo.getAllTutorial();
|
||||
|
||||
assertNotNull(list);
|
||||
assertTrue(list.size() == 4);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.ossez.xml;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.ossez.xml.binding.Tutorial;
|
||||
|
||||
public class StaxParserUnitTest {
|
||||
|
||||
final String fileName = "src/test/resources/example_stax.xml";
|
||||
|
||||
StaxParser parser;
|
||||
|
||||
@Test
|
||||
public void getAllTutorialsTest() {
|
||||
parser = new StaxParser(new File(fileName));
|
||||
List<Tutorial> tutorials = parser.getAllTutorial();
|
||||
|
||||
assertNotNull(tutorials);
|
||||
assertTrue(tutorials.size() == 4);
|
||||
assertTrue(tutorials.get(0).getType().equalsIgnoreCase("java"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
package com.ossez.xml;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class XMLDocumentWriterUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenXMLDocumentWhenWriteIsCalledThenXMLIsWrittenToFile() throws Exception {
|
||||
Document document = createSampleDocument();
|
||||
new XMLDocumentWriter().write(document, "company_simple.xml", false, false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenXMLDocumentWhenWriteIsCalledWithPrettyPrintThenFormattedXMLIsWrittenToFile() throws Exception {
|
||||
Document document = createSampleDocument();
|
||||
new XMLDocumentWriter().write(document, "company_prettyprinted.xml", false, true);
|
||||
}
|
||||
|
||||
private Document createSampleDocument() throws ParserConfigurationException {
|
||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
|
||||
Document document = documentBuilder.newDocument();
|
||||
Element companyElement = document.createElement("Company");
|
||||
document.appendChild(companyElement);
|
||||
Element departmentElement = document.createElement("Department");
|
||||
departmentElement.setAttribute("name", "Sales");
|
||||
companyElement.appendChild(departmentElement);
|
||||
Element employee1 = document.createElement("Employee");
|
||||
employee1.setAttribute("name", "John Smith");
|
||||
departmentElement.appendChild(employee1);
|
||||
Element employee2 = document.createElement("Employee");
|
||||
employee2.setAttribute("name", "Tim Dellor");
|
||||
departmentElement.appendChild(employee2);
|
||||
return document;
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanUp() throws Exception {
|
||||
FileUtils.deleteQuietly(new File("company_simple.xml"));
|
||||
FileUtils.deleteQuietly(new File("company_prettyprinted.xml"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,139 @@
|
|||
package com.ossez.xml;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.NamedNodeMap;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
public class XercesDomUnitTest {
|
||||
|
||||
final private String FILE_NAME = "src/test/resources/example_jdom.xml";
|
||||
final private String OUTPUT_DOM = "src/test/resources/Xerces_dom.xml";
|
||||
|
||||
private Document doc;
|
||||
private DocumentBuilder builder;
|
||||
|
||||
@Before
|
||||
public void loadXmlFile() throws Exception {
|
||||
if (doc == null) {
|
||||
builder = DocumentBuilderFactory.newInstance()
|
||||
.newDocumentBuilder();
|
||||
doc = builder.parse(new File(FILE_NAME));
|
||||
doc.getDocumentElement()
|
||||
.normalize();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetElementByTag_thenSuccess() {
|
||||
NodeList nodeList = doc.getElementsByTagName("tutorial");
|
||||
Node first = nodeList.item(0);
|
||||
|
||||
assertEquals(4, nodeList.getLength());
|
||||
assertEquals(Node.ELEMENT_NODE, first.getNodeType());
|
||||
assertEquals("tutorial", first.getNodeName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetFirstElementAttributes_thenSuccess() {
|
||||
Node first = doc.getElementsByTagName("tutorial")
|
||||
.item(0);
|
||||
NamedNodeMap attrList = first.getAttributes();
|
||||
|
||||
assertEquals(2, attrList.getLength());
|
||||
|
||||
assertEquals("tutId", attrList.item(0)
|
||||
.getNodeName());
|
||||
assertEquals("01", attrList.item(0)
|
||||
.getNodeValue());
|
||||
|
||||
assertEquals("type", attrList.item(1)
|
||||
.getNodeName());
|
||||
assertEquals("java", attrList.item(1)
|
||||
.getNodeValue());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenTraverseChildNodes_thenSuccess() {
|
||||
Node first = doc.getElementsByTagName("tutorial")
|
||||
.item(0);
|
||||
NodeList nodeList = first.getChildNodes();
|
||||
int n = nodeList.getLength();
|
||||
|
||||
Node current;
|
||||
for (int i = 0; i < n; i++) {
|
||||
current = nodeList.item(i);
|
||||
if (current.getNodeType() == Node.ELEMENT_NODE) {
|
||||
System.out.println(current.getNodeName() + ": " + current.getTextContent());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenModifyElementAttribute_thenModified() {
|
||||
NodeList nodeList = doc.getElementsByTagName("tutorial");
|
||||
Element first = (Element) nodeList.item(0);
|
||||
assertEquals("java", first.getAttribute("type"));
|
||||
|
||||
first.setAttribute("type", "other");
|
||||
assertEquals("other", first.getAttribute("type"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenCreateNewDocument_thenCreated() throws Exception {
|
||||
Document newDoc = builder.newDocument();
|
||||
Element root = newDoc.createElement("users");
|
||||
newDoc.appendChild(root);
|
||||
|
||||
Element first = newDoc.createElement("user");
|
||||
root.appendChild(first);
|
||||
first.setAttribute("id", "1");
|
||||
|
||||
Element email = newDoc.createElement("email");
|
||||
email.appendChild(newDoc.createTextNode("john@example.com"));
|
||||
first.appendChild(email);
|
||||
|
||||
assertEquals(1, newDoc.getChildNodes()
|
||||
.getLength());
|
||||
assertEquals("users", newDoc.getChildNodes()
|
||||
.item(0)
|
||||
.getNodeName());
|
||||
|
||||
printDom(newDoc);
|
||||
saveDomToFile(newDoc, OUTPUT_DOM);
|
||||
}
|
||||
|
||||
private void printDom(Document document) throws Exception {
|
||||
DOMSource dom = new DOMSource(document);
|
||||
Transformer transformer = TransformerFactory.newInstance()
|
||||
.newTransformer();
|
||||
|
||||
transformer.transform(dom, new StreamResult(System.out));
|
||||
}
|
||||
|
||||
private void saveDomToFile(Document document, String fileName) throws Exception {
|
||||
DOMSource dom = new DOMSource(document);
|
||||
Transformer transformer = TransformerFactory.newInstance()
|
||||
.newTransformer();
|
||||
|
||||
StreamResult result = new StreamResult(new File(fileName));
|
||||
transformer.transform(dom, result);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
package com.ossez.xml.attribute;
|
||||
|
||||
import org.dom4j.DocumentException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.TransformerFactoryConfigurationError;
|
||||
import javax.xml.xpath.XPathExpressionException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.xmlunit.assertj.XmlAssert.assertThat;
|
||||
|
||||
/**
|
||||
* Unit test for {@link Dom4jTransformer}.
|
||||
*/
|
||||
public class Dom4jProcessorUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenXmlWithAttributes_whenModifyAttribute_thenGetXmlUpdated() throws TransformerFactoryConfigurationError, TransformerException, DocumentException, SAXException {
|
||||
String path = getClass().getResource("/xml/attribute.xml")
|
||||
.toString();
|
||||
Dom4jTransformer transformer = new Dom4jTransformer(path);
|
||||
String attribute = "customer";
|
||||
String oldValue = "true";
|
||||
String newValue = "false";
|
||||
|
||||
String result = transformer.modifyAttribute(attribute, oldValue, newValue);
|
||||
|
||||
assertThat(result).hasXPath("//*[contains(@customer, 'false')]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoXml_whenModifyAttribute_thenGetSimilarXml() throws IOException, TransformerFactoryConfigurationError, TransformerException, URISyntaxException, DocumentException, SAXException {
|
||||
String path = getClass().getResource("/xml/attribute.xml")
|
||||
.toString();
|
||||
Dom4jTransformer transformer = new Dom4jTransformer(path);
|
||||
String attribute = "customer";
|
||||
String oldValue = "true";
|
||||
String newValue = "false";
|
||||
String expectedXml = new String(Files.readAllBytes((Paths.get(getClass().getResource("/xml/attribute_expected.xml")
|
||||
.toURI()))));
|
||||
|
||||
String result = transformer
|
||||
.modifyAttribute(attribute, oldValue, newValue)
|
||||
.replaceAll("(?m)^[ \t]*\r?\n", "");//Delete extra spaces added by Java 11
|
||||
|
||||
assertThat(result).and(expectedXml)
|
||||
.areSimilar();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenXmlXee_whenInit_thenThrowException() throws IOException, SAXException, ParserConfigurationException, XPathExpressionException, TransformerFactoryConfigurationError, TransformerException {
|
||||
String path = getClass().getResource("/xml/xee_attribute.xml")
|
||||
.toString();
|
||||
|
||||
assertThatThrownBy(() -> {
|
||||
|
||||
new Dom4jTransformer(path);
|
||||
|
||||
}).isInstanceOf(DocumentException.class);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package com.ossez.xml.attribute;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.xmlunit.assertj.XmlAssert.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.TransformerFactoryConfigurationError;
|
||||
import javax.xml.xpath.XPathExpressionException;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.SAXParseException;
|
||||
|
||||
/**
|
||||
* Unit test for {@link JaxpTransformer}.
|
||||
*/
|
||||
public class JaxpProcessorUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenXmlWithAttributes_whenModifyAttribute_thenGetXmlUpdated() throws IOException, SAXException, ParserConfigurationException, XPathExpressionException, TransformerFactoryConfigurationError, TransformerException {
|
||||
String path = getClass().getResource("/xml/attribute.xml")
|
||||
.toString();
|
||||
JaxpTransformer transformer = new JaxpTransformer(path);
|
||||
String attribute = "customer";
|
||||
String oldValue = "true";
|
||||
String newValue = "false";
|
||||
|
||||
String result = transformer.modifyAttribute(attribute, oldValue, newValue);
|
||||
|
||||
assertThat(result).hasXPath("//*[contains(@customer, 'false')]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenXmlXee_whenInit_thenThrowException() throws IOException, SAXException, ParserConfigurationException, XPathExpressionException, TransformerFactoryConfigurationError, TransformerException {
|
||||
String path = getClass().getResource("/xml/xee_attribute.xml")
|
||||
.toString();
|
||||
|
||||
assertThatThrownBy(() -> {
|
||||
|
||||
new JaxpTransformer(path);
|
||||
|
||||
}).isInstanceOf(SAXParseException.class);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
package com.ossez.xml.attribute;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.SAXParseException;
|
||||
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.TransformerFactoryConfigurationError;
|
||||
import javax.xml.xpath.XPathExpressionException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.xmlunit.assertj.XmlAssert.assertThat;
|
||||
|
||||
/**
|
||||
* Unit test for {@link JooxTransformer}.
|
||||
*/
|
||||
public class JooxProcessorUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenXmlWithAttributes_whenModifyAttribute_thenGetXmlUpdated() throws IOException, SAXException, TransformerFactoryConfigurationError {
|
||||
String path = getClass().getResource("/xml/attribute.xml")
|
||||
.toString();
|
||||
JooxTransformer transformer = new JooxTransformer(path);
|
||||
String attribute = "customer";
|
||||
String oldValue = "true";
|
||||
String newValue = "false";
|
||||
|
||||
String result = transformer.modifyAttribute(attribute, oldValue, newValue);
|
||||
|
||||
assertThat(result).hasXPath("//*[contains(@customer, 'false')]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoXml_whenModifyAttribute_thenGetSimilarXml() throws IOException, TransformerFactoryConfigurationError, URISyntaxException, SAXException {
|
||||
String path = getClass().getResource("/xml/attribute.xml")
|
||||
.toString();
|
||||
JooxTransformer transformer = new JooxTransformer(path);
|
||||
String attribute = "customer";
|
||||
String oldValue = "true";
|
||||
String newValue = "false";
|
||||
String expectedXml = new String(Files.readAllBytes((Paths.get(getClass().getResource("/xml/attribute_expected.xml")
|
||||
.toURI()))));
|
||||
|
||||
String result = transformer
|
||||
.modifyAttribute(attribute, oldValue, newValue)
|
||||
.replaceAll("(?m)^[ \t]*\r?\n", "");//Delete extra spaces added by Java 11
|
||||
|
||||
assertThat(result).and(expectedXml)
|
||||
.areSimilar();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenXmlXee_whenInit_thenThrowException() throws IOException, SAXException, ParserConfigurationException, XPathExpressionException, TransformerFactoryConfigurationError, TransformerException {
|
||||
String path = getClass().getResource("/xml/xee_attribute.xml")
|
||||
.toString();
|
||||
|
||||
assertThatThrownBy(() -> {
|
||||
|
||||
new JooxTransformer(path);
|
||||
|
||||
}).isInstanceOf(SAXParseException.class);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
package com.ossez.xml.jibx;
|
||||
|
||||
import org.jibx.runtime.BindingDirectory;
|
||||
import org.jibx.runtime.IBindingFactory;
|
||||
import org.jibx.runtime.IUnmarshallingContext;
|
||||
import org.jibx.runtime.JiBXException;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
|
||||
public class CustomerUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenUnmarshalXML_ThenFieldsAreMapped() throws JiBXException, FileNotFoundException {
|
||||
IBindingFactory bfact = BindingDirectory.getFactory(Customer.class);
|
||||
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
|
||||
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
||||
InputStream inputStream = classLoader.getResourceAsStream("Customer1.xml");
|
||||
Customer customer = (Customer) uctx.unmarshalDocument(inputStream, null);
|
||||
|
||||
assertEquals("Stefan Jaeger", customer.getPerson().getName());
|
||||
assertEquals("Davos Dorf", customer.getCity());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void WhenUnmarshal_ThenMappingInherited() throws JiBXException, FileNotFoundException {
|
||||
IBindingFactory bfact = BindingDirectory.getFactory(Customer.class);
|
||||
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
|
||||
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
||||
InputStream inputStream = classLoader.getResourceAsStream("Customer1.xml");
|
||||
Customer customer = (Customer) uctx.unmarshalDocument(inputStream, null);
|
||||
|
||||
assertEquals(12345, customer.getPerson().getCustomerId());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void WhenUnmarshal_ThenPhoneMappingRead() throws JiBXException, FileNotFoundException {
|
||||
IBindingFactory bfact = BindingDirectory.getFactory(Customer.class);
|
||||
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
|
||||
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
||||
InputStream inputStream = classLoader.getResourceAsStream("Customer1.xml");
|
||||
Customer customer = (Customer) uctx.unmarshalDocument(inputStream, null);
|
||||
|
||||
assertEquals("234678", customer.getHomePhone().getNumber());
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.ossez.xml.stax;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
public class StaxParserUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenWebsitesXML_whenParsed_thenNotNull() {
|
||||
List<WebSite> websites = StaxParser.parse("src/test/resources/xml/websites.xml");
|
||||
assertNotNull(websites);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWebsitesXML_whenParsed_thenSizeIsThree() {
|
||||
List<WebSite> websites = StaxParser.parse("src/test/resources/xml/websites.xml");
|
||||
assertEquals(3, websites.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWebsitesXML_whenParsed_thenLocalhostExists() {
|
||||
List<WebSite> websites = StaxParser.parse("src/test/resources/xml/websites.xml");
|
||||
assertEquals("Localhost", websites.get(2).getName());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.ossez.xmlhtml.freemarker;
|
||||
|
||||
import com.ossez.xmlhtml.stax.StaxTransformer;
|
||||
import freemarker.template.TemplateException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class FreemarkerTransformerUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenXml_whenTransform_thenGetHtml() throws IOException, URISyntaxException, XMLStreamException, TemplateException {
|
||||
String expectedHtml = new String(Files.readAllBytes((Paths.get(getClass()
|
||||
.getResource("/xmlhtml/notification.html")
|
||||
.toURI()))));
|
||||
StaxTransformer staxTransformer = new StaxTransformer("src/test/resources/xmlhtml/notification.xml");
|
||||
String templateFile = "freemarker.html";
|
||||
String templateDirectory = "src/test/resources/templates";
|
||||
FreemarkerTransformer transformer = new FreemarkerTransformer(staxTransformer, templateDirectory, templateFile);
|
||||
|
||||
String result = transformer.html();
|
||||
|
||||
assertThat(result).isEqualTo(expectedHtml);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.ossez.xmlhtml.jaxp;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class JaxpTransformerUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenXml_whenTransform_thenGetHtml() throws IOException, SAXException, ParserConfigurationException, TransformerException, URISyntaxException {
|
||||
String path = getClass()
|
||||
.getResource("/xmlhtml/notification.xml")
|
||||
.toString();
|
||||
String expectedHtml = new String(Files.readAllBytes((Paths.get(getClass()
|
||||
.getResource("/xmlhtml/notification.html")
|
||||
.toURI()))));
|
||||
JaxpTransformer transformer = new JaxpTransformer(path);
|
||||
|
||||
String result = transformer
|
||||
.html()
|
||||
.replaceAll("(?m)^\\s+", "");//Delete extra spaces added by Java 11
|
||||
|
||||
assertThat(result).isEqualTo(expectedHtml);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.ossez.xmlhtml.mustache;
|
||||
|
||||
import com.ossez.xmlhtml.stax.StaxTransformer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class MustacheTransformerUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenXml_whenTransform_thenGetHtml() throws IOException, URISyntaxException, XMLStreamException {
|
||||
String expectedHtml = new String(Files.readAllBytes((Paths.get(getClass()
|
||||
.getResource("/xmlhtml/notification.html")
|
||||
.toURI()))));
|
||||
StaxTransformer staxTransformer = new StaxTransformer("src/test/resources/xmlhtml/notification.xml");
|
||||
String templateFile = "src/test/resources/templates/template.mustache";
|
||||
MustacheTransformer transformer = new MustacheTransformer(staxTransformer, templateFile);
|
||||
|
||||
String result = transformer.html();
|
||||
|
||||
assertThat(result).isEqualTo(expectedHtml);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.ossez.xmlhtml.stax;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class StaxTransformerUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenXml_whenTransform_thenGetHtml() throws IOException, URISyntaxException, XMLStreamException {
|
||||
String path = "src/test/resources/xmlhtml/notification.xml";
|
||||
String expectedHtml = new String(Files.readAllBytes((Paths.get(getClass()
|
||||
.getResource("/xmlhtml/notification.html")
|
||||
.toURI()))));
|
||||
StaxTransformer transformer = new StaxTransformer(path);
|
||||
|
||||
String result = transformer.html();
|
||||
|
||||
assertThat(result).isEqualTo(expectedHtml);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<customer>
|
||||
<person>
|
||||
<customer-id>12345</customer-id>
|
||||
<name>Stefan Jaeger</name>
|
||||
</person>
|
||||
<home-phone>
|
||||
<number>234678</number>
|
||||
</home-phone>
|
||||
|
||||
<office-phone>
|
||||
<number>234678</number>
|
||||
</office-phone>
|
||||
<city>Davos Dorf</city>
|
||||
|
||||
</customer>
|
|
@ -0,0 +1 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="no"?><users><user id="1"><email>john@example.com</email></user></users>
|
|
@ -0,0 +1,32 @@
|
|||
<?xml version="1.0"?>
|
||||
<tutorials>
|
||||
<tutorial tutId="01" type="java">
|
||||
<title>Guava</title>
|
||||
<description>Introduction to Guava</description>
|
||||
<date>04/04/2016</date>
|
||||
<author>GuavaAuthor</author>
|
||||
</tutorial>
|
||||
<tutorial tutId="02" type="java">
|
||||
<title>XML</title>
|
||||
<description>Introduction to XPath</description>
|
||||
<date>04/05/2016</date>
|
||||
<author>XMLAuthor</author>
|
||||
</tutorial>
|
||||
<tutorial tutId="03" type="android">
|
||||
<title>Android</title>
|
||||
<description>Introduction to Android</description>
|
||||
<date>04/03/2016</date>
|
||||
<author>AndroidAuthor</author>
|
||||
</tutorial>
|
||||
<tutorial tutId="04" type="java">
|
||||
<title>Spring</title>
|
||||
<description>Introduction to Spring</description>
|
||||
<date>04/02/2016</date>
|
||||
<author>SpringAuthor</author>
|
||||
<sections>
|
||||
<section name="core">Spring Core</section>
|
||||
<section name="mvc">Spring MVC</section>
|
||||
<section name="batch">Spring Batch</section>
|
||||
</sections>
|
||||
</tutorial>
|
||||
</tutorials>
|
|
@ -0,0 +1,32 @@
|
|||
<?xml version="1.0"?>
|
||||
<tutorials xmlns="http://www.baeldung.com/full_archive">
|
||||
<tutorial tutId="01" type="java">
|
||||
<title>Guava</title>
|
||||
<description>Introduction to Guava</description>
|
||||
<date>04/04/2016</date>
|
||||
<author>GuavaAuthor</author>
|
||||
</tutorial>
|
||||
<tutorial tutId="02" type="java">
|
||||
<title>XML</title>
|
||||
<description>Introduction to XPath</description>
|
||||
<date>04/05/2016</date>
|
||||
<author>XMLAuthor</author>
|
||||
</tutorial>
|
||||
<tutorial tutId="03" type="android">
|
||||
<title>Android</title>
|
||||
<description>Introduction to Android</description>
|
||||
<date>04/03/2016</date>
|
||||
<author>AndroidAuthor</author>
|
||||
</tutorial>
|
||||
<tutorial tutId="04" type="java">
|
||||
<title>Spring</title>
|
||||
<description>Introduction to Spring</description>
|
||||
<date>04/02/2016</date>
|
||||
<author>SpringAuthor</author>
|
||||
<sections>
|
||||
<section name="core">Spring Core</section>
|
||||
<section name="mvc">Spring MVC</section>
|
||||
<section name="batch">Spring Batch</section>
|
||||
</sections>
|
||||
</tutorial>
|
||||
</tutorials>
|
|
@ -0,0 +1,32 @@
|
|||
<?xml version="1.0"?>
|
||||
<tutorials>
|
||||
<tutorial tutId="01" type="java">
|
||||
<title>Guava</title>
|
||||
<description>Introduction to Guava</description>
|
||||
<date>04/04/2016</date>
|
||||
<author>GuavaAuthor</author>
|
||||
</tutorial>
|
||||
<tutorial tutId="02" type="java">
|
||||
<title>XML</title>
|
||||
<description>Introduction to XPath</description>
|
||||
<date>04/05/2016</date>
|
||||
<author>XMLAuthor</author>
|
||||
</tutorial>
|
||||
<tutorial tutId="03" type="android">
|
||||
<title>Android</title>
|
||||
<description>Introduction to Android</description>
|
||||
<date>04/03/2016</date>
|
||||
<author>AndroidAuthor</author>
|
||||
</tutorial>
|
||||
<tutorial tutId="04" type="java">
|
||||
<title>Spring</title>
|
||||
<description>Introduction to Spring</description>
|
||||
<date>04/02/2016</date>
|
||||
<author>SpringAuthor</author>
|
||||
<sections>
|
||||
<section name="core">Spring Core</section>
|
||||
<section name="mvc">Spring MVC</section>
|
||||
<section name="batch">Spring Batch</section>
|
||||
</sections>
|
||||
</tutorial>
|
||||
</tutorials>
|
|
@ -0,0 +1,10 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<XMLTutorials>
|
||||
<tutorial tutId="01" type="xml">
|
||||
<title>XML with Dom4J</title>
|
||||
<description>XML handling with Dom4J</description>
|
||||
<date>14/06/2016</date>
|
||||
<author>Dom4J tech writer</author>
|
||||
</tutorial>
|
||||
</XMLTutorials>
|
|
@ -0,0 +1,32 @@
|
|||
<?xml version="1.0"?>
|
||||
<tutorials>
|
||||
<tutorial tutId="01" type="java">
|
||||
<title>Guava</title>
|
||||
<description>Introduction to Guava</description>
|
||||
<date>04/04/2016</date>
|
||||
<author>GuavaAuthor</author>
|
||||
</tutorial>
|
||||
<tutorial tutId="02" type="java">
|
||||
<title>XML</title>
|
||||
<description>Introduction to XPath</description>
|
||||
<date>04/05/2016</date>
|
||||
<author>XMLAuthor</author>
|
||||
</tutorial>
|
||||
<tutorial tutId="03" type="android">
|
||||
<title>Android</title>
|
||||
<description>Introduction to Android</description>
|
||||
<date>04/03/2016</date>
|
||||
<author>AndroidAuthor</author>
|
||||
</tutorial>
|
||||
<tutorial tutId="04" type="java">
|
||||
<title>Spring</title>
|
||||
<description>Introduction to Spring</description>
|
||||
<date>04/02/2016</date>
|
||||
<author>SpringAuthor</author>
|
||||
<sections>
|
||||
<section name="core">Spring Core</section>
|
||||
<section name="mvc">Spring MVC</section>
|
||||
<section name="batch">Spring Batch</section>
|
||||
</sections>
|
||||
</tutorial>
|
||||
</tutorials>
|
|
@ -0,0 +1,32 @@
|
|||
<?xml version="1.0"?>
|
||||
<tutorials>
|
||||
<tutorial tutId="01" type="java">
|
||||
<title>Guava</title>
|
||||
<description>Introduction to Guava</description>
|
||||
<date>04/04/2016</date>
|
||||
<author>GuavaAuthor</author>
|
||||
</tutorial>
|
||||
<tutorial tutId="02" type="java">
|
||||
<title>XML</title>
|
||||
<description>Introduction to XPath</description>
|
||||
<date>04/05/2016</date>
|
||||
<author>XMLAuthor</author>
|
||||
</tutorial>
|
||||
<tutorial tutId="03" type="android">
|
||||
<title>Android</title>
|
||||
<description>Introduction to Android</description>
|
||||
<date>04/03/2016</date>
|
||||
<author>AndroidAuthor</author>
|
||||
</tutorial>
|
||||
<tutorial tutId="04" type="java">
|
||||
<title>Spring</title>
|
||||
<description>Introduction to Spring</description>
|
||||
<date>04/02/2016</date>
|
||||
<author>SpringAuthor</author>
|
||||
<sections>
|
||||
<section name="core">Spring Core</section>
|
||||
<section name="mvc">Spring MVC</section>
|
||||
<section name="batch">Spring Batch</section>
|
||||
</sections>
|
||||
</tutorial>
|
||||
</tutorials>
|
|
@ -0,0 +1,32 @@
|
|||
<?xml version="1.0"?>
|
||||
<tutorials>
|
||||
<tutorial tutId="01" type="java">
|
||||
<title>Guava</title>
|
||||
<description>Introduction to Guava</description>
|
||||
<date>04/04/2016</date>
|
||||
<author>GuavaAuthor</author>
|
||||
</tutorial>
|
||||
<tutorial tutId="02" type="java">
|
||||
<title>XML</title>
|
||||
<description>Introduction to XPath</description>
|
||||
<date>04/05/2016</date>
|
||||
<author>XMLAuthor</author>
|
||||
</tutorial>
|
||||
<tutorial tutId="03" type="android">
|
||||
<title>Android</title>
|
||||
<description>Introduction to Android</description>
|
||||
<date>04/03/2016</date>
|
||||
<author>AndroidAuthor</author>
|
||||
</tutorial>
|
||||
<tutorial tutId="04" type="java">
|
||||
<title>Spring</title>
|
||||
<description>Introduction to Spring</description>
|
||||
<date>04/02/2016</date>
|
||||
<author>SpringAuthor</author>
|
||||
<sections>
|
||||
<section name="core">Spring Core</section>
|
||||
<section name="mvc">Spring MVC</section>
|
||||
<section name="batch">Spring Batch</section>
|
||||
</sections>
|
||||
</tutorial>
|
||||
</tutorials>
|
|
@ -0,0 +1,32 @@
|
|||
<?xml version="1.0"?>
|
||||
<tutorials>
|
||||
<tutorial tutId="01" type="java">
|
||||
<title>Guava</title>
|
||||
<description>Introduction to Guava</description>
|
||||
<date>04/04/2016</date>
|
||||
<author>GuavaAuthor</author>
|
||||
</tutorial>
|
||||
<tutorial tutId="02" type="java">
|
||||
<title>XML</title>
|
||||
<description>Introduction to XPath</description>
|
||||
<date>04/05/2016</date>
|
||||
<author>XMLAuthor</author>
|
||||
</tutorial>
|
||||
<tutorial tutId="03" type="android">
|
||||
<title>Android</title>
|
||||
<description>Introduction to Android</description>
|
||||
<date>04/03/2016</date>
|
||||
<author>AndroidAuthor</author>
|
||||
</tutorial>
|
||||
<tutorial tutId="04" type="java">
|
||||
<title>Spring</title>
|
||||
<description>Introduction to Spring</description>
|
||||
<date>04/02/2016</date>
|
||||
<author>SpringAuthor</author>
|
||||
<sections>
|
||||
<section name="core">Spring Core</section>
|
||||
<section name="mvc">Spring MVC</section>
|
||||
<section name="batch">Spring Batch</section>
|
||||
</sections>
|
||||
</tutorial>
|
||||
</tutorials>
|
|
@ -0,0 +1,13 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="TRACE">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
|
@ -0,0 +1,16 @@
|
|||
<baeldung>
|
||||
<articles>
|
||||
<article>
|
||||
<title>Parsing an XML File Using SAX Parser</title>
|
||||
<content>SAX Parser's Lorem ipsum...</content>
|
||||
</article>
|
||||
<article>
|
||||
<title>Parsing an XML File Using DOM Parser</title>
|
||||
<content>DOM Parser's Lorem ipsum...</content>
|
||||
</article>
|
||||
<article>
|
||||
<title>Parsing an XML File Using StAX Parser</title>
|
||||
<content>StAX Parser's Lorem ipsum...</content>
|
||||
</article>
|
||||
</articles>
|
||||
</baeldung>
|
|
@ -0,0 +1,11 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||||
<title>${heading}</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>${from}</p>
|
||||
<p>${content}</p>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,11 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||||
<title>{{heading}}</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>{{from}}</p>
|
||||
<p>{{content}}</p>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,5 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<notification id="5">
|
||||
<to customer="true">john@email.com</to>
|
||||
<from>mary@email.com</from>
|
||||
</notification>
|
|
@ -0,0 +1,5 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<notification id="5">
|
||||
<to customer="false">john@email.com</to>
|
||||
<from>mary@email.com</from>
|
||||
</notification>
|
|
@ -0,0 +1,18 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<websites>
|
||||
<website url="https://baeldung.com">
|
||||
<name>Baeldung</name>
|
||||
<category>Online Courses</category>
|
||||
<status>Online</status>
|
||||
</website>
|
||||
<website url="http://example.com">
|
||||
<name>Example</name>
|
||||
<category>Examples</category>
|
||||
<status>Offline</status>
|
||||
</website>
|
||||
<website url="http://localhost:8080">
|
||||
<name>Localhost</name>
|
||||
<category>Tests</category>
|
||||
<status>Offline</status>
|
||||
</website>
|
||||
</websites>
|
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE notification [
|
||||
<!ENTITY xxe SYSTEM "file:///etc/passwd" >]>
|
||||
<notification id="5">
|
||||
<bad_entry>&xxe;
|
||||
</bad_entry>
|
||||
<to customer="true">john@email.com</to>
|
||||
<from>mary@email.com</from>
|
||||
</notification>
|
|
@ -0,0 +1,11 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||||
<title>Build #7 passed</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>from: builds@baeldung.com</p>
|
||||
<p>Success: The Jenkins CI build passed</p>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<notification>
|
||||
<from>builds@baeldung.com</from>
|
||||
<heading>Build #7 passed</heading>
|
||||
<content>Success: The Jenkins CI build passed</content>
|
||||
</notification>
|
Loading…
Reference in New Issue