List of usage examples for org.dom4j Document node
Node node(int index) throws IndexOutOfBoundsException;
Node
at the specified index position. From source file:architecture.common.xml.XmlWriter.java
License:Apache License
/** * <p>// ww w . ja va 2 s . c o m * This will print the <code>Document</code> to the current Writer. * </p> * * <p> * Warning: using your own Writer may cause the writer's preferred character * encoding to be ignored. If you use encodings other than UTF8, we * recommend using the method that takes an OutputStream instead. * </p> * * <p> * Note: as with all Writers, you may need to flush() yours after this * method returns. * </p> * * @param doc * <code>Document</code> to format. * @throws IOException * - if there's any problem writing. **/ public void write(Document doc) throws IOException { writeDeclaration(); if (doc.getDocType() != null) { indent(); writeDocType(doc.getDocType()); } for (int i = 0, size = doc.nodeCount(); i < size; i++) { Node node = doc.node(i); writeNode(node); } writePrintln(); if (autoFlush) { flush(); } }
From source file:org.apache.taglibs.xtags.xpath.ReplaceTag.java
License:Apache License
public int doEndTag() throws JspException { Object context = TagHelper.getInputNodes(pageContext, this, false); if (context == null) { logInfo("No current node to replace"); return EVAL_PAGE; }/*from w ww .j ava 2 s . c om*/ try { String xmlFragment = null; if (bodyContent != null) { xmlFragment = bodyContent.getString(); } if (context instanceof List) { List els = (List) context; if (els.size() > 1) { throw new JspException("Current context contains more than one node"); } if (els.size() == 1) { context = els.get(0); } } if (context instanceof Document) { if (xmlFragment == null) { throw new JspException("Cannot replace document with empty body"); } Document sourceDoc = (Document) context; Document newDoc = DocumentHelper.parseText(xmlFragment); // clear source doc contents sourceDoc.clearContent(); for (int i = 0, size = newDoc.nodeCount(); i < size; i++) { Node node = newDoc.node(i); // detach from new doc node.detach(); // add to source sourceDoc.add(node); } } else { if (!(context instanceof Element)) { throw new JspException("Current node is not an Element: " + context.getClass().getName()); } Element element = (Element) context; SAXReader reader = new SAXReader(); if (element.isRootElement()) { if (xmlFragment == null) { throw new JspException("Cannot replace root element with empty body"); } Document newDoc = DocumentHelper.parseText(xmlFragment); Document sourceDoc = element.getDocument(); Element newRoot = newDoc.getRootElement(); newRoot.detach(); sourceDoc.setRootElement(newRoot); } else { Element parent = element.getParent(); List parentContent = parent.content(); int index = parentContent.indexOf(element); parentContent.remove(index); if (xmlFragment != null) { Document newDoc = DocumentHelper.parseText("<dummy>" + xmlFragment + "</dummy>"); parentContent.addAll(index, newDoc.getRootElement().content()); } } } } catch (DocumentException e) { handleException(e); } return EVAL_PAGE; }
From source file:org.openxml4j.opc.signature.RelationshipTransform.java
License:Apache License
public static void RemoveAllTopLevelComments(Document doc) { List<Node> tobeRemovedNodes = new Vector<Node>(); for (int i = 0; i < doc.nodeCount(); i++) { if (doc.node(i).getNodeType() == Document.COMMENT_NODE) tobeRemovedNodes.add(doc.node(i)); }//from w w w . jav a 2 s . co m for (Node tempNode : tobeRemovedNodes) { doc.remove(tempNode); } }