@Immutable @Loggable(value=1) public final class Xembler extends Object
For example, to modify a DOM document:
Document dom = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().newDocument();
dom.appendChild(dom.createElement("root"));
new Xembler(
new Directives()
.xpath("/root")
.addIfAbsent("employees")
.add("employee")
.attr("id", 6564)
).apply(dom);
You can also convert your Xembly directives directly to XML document:
String xml = new Xembler(
new Directives()
.xpath("/root")
.addIfAbsent("employees")
.add("employee")
.attr("id", 6564)
).xml("root");
Since version 0.18 you can convert directives to XML without
a necessity to catch checked exceptions.
Use *Quietly() methods for that:
xmlQuietly(), domQuietly(),
and applyQuietly(org.w3c.dom.Node).
| Constructor and Description |
|---|
Xembler(Iterable<Directive> dirs)
Public ctor.
|
| Modifier and Type | Method and Description |
|---|---|
Node |
apply(Node dom)
Apply all changes to the document/node.
|
Node |
applyQuietly(Node dom)
Apply all changes to the document/node, without any checked exceptions.
|
Document |
dom()
Apply all changes to an empty DOM.
|
Document |
domQuietly()
Apply all changes to an empty DOM, without checked exceptions.
|
static String |
escape(String text)
Utility method to escape text before using it as a text value
in XML.
|
String |
xml()
Convert to XML document.
|
String |
xmlQuietly()
Convert to XML document, without checked exceptions.
|
public Node applyQuietly(Node dom)
dom - DOM document/node@Loggable(value=1, ignore=ImpossibleModificationException.class) public Node apply(@NotNull(message="DOM can\'t be NULL") Node dom) throws ImpossibleModificationException
dom - DOM document/nodeImpossibleModificationException - If can't modifypublic Document domQuietly()
@Loggable(value=1, ignore=ImpossibleModificationException.class) public Document dom() throws ImpossibleModificationException
ImpossibleModificationException - If can't modifypublic String xmlQuietly()
@Loggable(value=1, ignore=ImpossibleModificationException.class) public String xml() throws ImpossibleModificationException
ImpossibleModificationException - If can't modifypublic static String escape(String text)
Use it like this, in order to avoid runtime exceptions:
new Directives().xpath("/test")
.set(Xembler.escape("illegal: "));text - Text to escapeCopyright © 2013–2015 Xembly. All rights reserved.