Created
March 31, 2015 17:58
-
-
Save RaffaeleSgarro/1c9e30305676126b7eb1 to your computer and use it in GitHub Desktop.
Demo JOOX program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package app; | |
import org.joox.JOOX; | |
import org.w3c.dom.Document; | |
import org.w3c.dom.Element; | |
import javax.xml.transform.*; | |
import javax.xml.transform.dom.DOMSource; | |
import javax.xml.transform.stream.StreamResult; | |
import static org.joox.JOOX.*; | |
public class Main { | |
public static void main(String... args) throws Exception { | |
Document document = JOOX.builder().newDocument(); | |
Element root = document.createElement("contacts"); | |
document.appendChild(root); | |
for (String name : new String[]{"John", "Jessica", "Peter"}) { | |
$(root).append( | |
$("contact" | |
, $("name", name) | |
, $("active", "true") | |
) | |
); | |
} | |
Transformer transformer = TransformerFactory.newInstance().newTransformer(); | |
Result output = new StreamResult(System.out); | |
Source input = new DOMSource(document); | |
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); | |
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); | |
transformer.transform(input, output); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8" standalone="no"?> | |
<contacts> | |
<contact> | |
<name>John</name> | |
<active>true</active> | |
</contact> | |
<contact> | |
<name>Jessica</name> | |
<active>true</active> | |
</contact> | |
<contact> | |
<name>Peter</name> | |
<active>true</active> | |
</contact> | |
</contacts> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment