template:
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <div id="hellomsg">here comes message.</div> </body> </html>
Java code:
package com.example;
import java.io.InputStream;
import org.mixer2.Mixer2Engine;
import org.mixer2.jaxb.xhtml.Div;
import org.mixer2.jaxb.xhtml.Html;
public class HelloWorld {
public static void main(String[] args) throws Exception {
InputStream is = HelloWorld.class.getClassLoader().getResourceAsStream("HelloWorld.html");
Mixer2Engine m2e = new Mixer2Engine();
Html html = m2e.loadHtmlTemplate(is);
html.getById("hellomsg", Div.class).replaceInner("Hello World !");
System.out.println(m2e.saveToString(html));
}
}result:
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"/> </head> <body> <div id="hellomsg">Hello World !</div> </body> </html>
<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>
<groupId>com.example</groupId>
<artifactId>myhelloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 1.8 means Java8. change to 1.6(java6) or 1.7(java7) as you like. -->
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.mixer2</groupId>
<artifactId>mixer2</artifactId>
<version>1.3.1</version><!-- write latest version ! -->
</dependency>
</dependencies>
</project>