@Override public Element clone(boolean recurse) { final Element result = new Element(this); if (recurse) { for (final Child child : getChildren()) { result.addChild(child.clone(true)); } } return result; }
/** * Adds a text as last child. * * @param content The text content. * @param merge If {@code true}, then if last child exists and is a text, * {@code content} is added to this last child.<br> * Otherwise, a text child is created and added to last position. * @return The modified or created text. */ public Text addText(String content, boolean merge) { if (merge) { final Child last = getLastChild(); if (last != null && last.getType() == NodeType.TEXT) { ((Text) last).appendContent(content); return (Text) last; } } final Text result = new Text(content); addChild(result); return result; }
@Test public void testGetRootElement() { final Comment x = new Comment(HELLO); assertEquals(null, x.getRootElement()); final Element root = new Element(NAME); root.addChild(x); assertEquals(root, x.getRootElement()); } }
@Test public void testGetRootElement() { final Text x = new Text(HELLO); assertEquals(null, x.getRootElement()); final Element root = new Element(NAME); root.addChild(x); assertEquals(root, x.getRootElement()); } }
public static void main(String[] args) throws IOException { LOGGER.info("Start " + XmlDataWriterDemo.class.getSimpleName()); Debug.printClassPath(); for (final URL url : Resources.getResources("log4j2.xml")) { LOGGER.info(" " + url); } final Document doc = new Document(); final Element root = new Element("root"); doc.addComment("Hello"); doc.addChild(root); doc.addComment("Hello"); root.addAttribute("att1", "value1"); final Element child = new Element("child"); child.addAttribute("att1", "value1"); root.addChild(child); doc.removeChildren(); doc.addComment("Hello"); doc.addChild(root); doc.addComment("Hello"); try (final XmlDataWriter writer = new XmlDataWriter(NonCloseableOutputStream.NON_CLOSABLE_SYSTEM_OUT)) { writer.getXmlWriter().setEnabled(XmlWriter.Feature.PRETTY_PRINT, true); writer.write(doc); writer.flush(); } } }