List of usage examples for org.w3c.dom NodeList NodeList
NodeList
From source file:Main.java
public static NodeList getChildsByTagName(Element root, String name) { final Vector<Node> v = new Vector<Node>(); NodeList nl = root.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node n = nl.item(i);//from w w w .j a va2s. c o m if (n.getNodeType() == Element.ELEMENT_NODE) { Element e = (Element) n; if (name.equals("*") || e.getNodeName().equalsIgnoreCase(name)) v.add(n); } } return new NodeList() { public Node item(int index) { if (index >= v.size() || index < 0) return null; else return v.get(index); } public int getLength() { return v.size(); } }; }
From source file:Main.java
/** * Provides a NodeList of multiple nodelists *///from w ww . jav a 2 s. c o m public static NodeList combine(final NodeList... nls) { return new NodeList() { public Node item(final int index) { int offset = 0; for (int i = 0; i < nls.length; i++) { if (index - offset < nls[i].getLength()) { return nls[i].item(index - offset); } else { offset += nls[i].getLength(); } } return null; } public int getLength() { int result = 0; for (int i = 0; i < nls.length; i++) { result += nls[i].getLength(); } return result; } }; }
From source file:Main.java
/** * XPath re-invented to be maximally compatible. * About as popular as reinventing the wheel. Bugger! * //from w ww . j a v a 2s. c o m * @param element to use as a starting point * @param path simple "xpath-like" expression, sans namespaces (uses only local part) * @return NodeList of matching nodes */ public static NodeList queryDomNodes(Element element, String path) { final List nodeList = new ArrayList(10); String steps[] = path.split("/"); collectResults(element, steps, 0, nodeList); return new NodeList() { public int getLength() { return nodeList.size(); } public Node item(int index) { if (index < 0 || index >= nodeList.size()) return null; return (Node) nodeList.get(index); } }; }
From source file:Main.java
/** * Serialise the supplied W3C DOM subtree. * * @param node The DOM node to be serialized. * @param format Format the output./*from w w w . j ava 2 s . c o m*/ * @param writer The target writer for serialization. * @throws DOMException Unable to serialise the DOM. */ public static void serialize(final Node node, boolean format, Writer writer) throws DOMException { if (node.getNodeType() == Node.DOCUMENT_NODE) { serialize(node.getChildNodes(), format, writer); } else { serialize(new NodeList() { public Node item(int index) { return node; } public int getLength() { return 1; } }, format, writer); } }
From source file:Main.java
/** * Get all immediate element children with the given name. This differs from {@link * org.w3c.dom.Element#getElementsByTagName getElementsByTagName} in that this only returns * direct children, not all descendents. * * @param parent the parent node//w ww . ja v a2 s.c om * @param childName the name of the children elements to get; may be null or "*" to indicate all * element children * @return the list of children; may be empty */ public static NodeList getChildren(Element parent, String childName) { final NodeList children = parent.getChildNodes(); final String filter = (childName != null && !childName.equals("*")) ? childName : null; return new NodeList() { private List elems; { elems = new ArrayList(); for (int idx = 0; idx < children.getLength(); idx++) { Node n = children.item(idx); if (n.getNodeType() == Node.ELEMENT_NODE && (filter == null || n.getNodeName().equals(filter))) elems.add(n); } } public Node item(int index) { return (Node) elems.get(index); } public int getLength() { return elems.size(); } }; }
From source file:com.marklogic.dom.ElementImpl.java
/** {@inheritDoc} */ @Override//from w w w . j a va 2s . c o m public NodeList getChildNodes() { return new NodeList() { public int getLength() { return tree.elemNodeNumChildren[tree.nodeRepID[node]]; } public Node item(int index) { return (index < getLength()) ? tree.node(tree.elemNodeChildNodeRepID[tree.nodeRepID[node]] + index) : null; } }; }
From source file:com.adaptris.core.services.jdbc.JdbcIteratingDataCaptureServiceImpl.java
private NodeList nodesToProcess(Document doc, XPath xpath) throws XPathExpressionException { // initially set the NodeList to be the whole document // this ensures it will have one node only and will therefore // enable the remaining structure to cope with the optionally // iterative process structure NodeList nodes = xpath.selectNodeList(doc, "/"); if (iterates()) { nodes = xpath.selectNodeList(doc, getIterationXpath()); }//ww w.j av a 2 s.c o m // This is a kludge to ensure that we don't get a null pointer exception // in the event of a null NodeList and yet still manage to reach the // executeUpdate call. if (nodes == null) { nodes = new NodeList() { @Override public int getLength() { return 1; } @Override public Node item(int i) { return null; } }; } return nodes; }
From source file:com.marklogic.dom.DocumentImpl.java
public NodeList getChildNodes() { return new NodeList() { public int getLength() { return getNumChildren(); }/*from w w w . j av a2s . c om*/ public Node item(int index) { return (index < getNumChildren()) ? tree.node(getFirstChildIndex() + index) : null; } }; }
From source file:com.marklogic.dom.NodeImpl.java
protected NodeList getElementsByTagNameNSOrNodeName(String namespaceURI, String name, final boolean nodeName) { final String tagname = name; final String ns = namespaceURI; final Node thisNode = this; return new NodeList() { protected ArrayList<Node> elementList = new ArrayList<Node>(); protected boolean done = false; protected void init() { if (done) return; Stack<Node> childrenStack = new Stack<Node>(); childrenStack.push(thisNode); boolean root = true; while (!childrenStack.isEmpty()) { Node curr = childrenStack.pop(); NodeList children = curr.getChildNodes(); for (int childi = children.getLength() - 1; childi >= 0; childi--) if (children.item(childi).getNodeType() == Node.ELEMENT_NODE) childrenStack.push(children.item(childi)); if (root) { root = false;// w w w. j ava 2 s . c o m continue; } if (nodeName) { if (curr.getNodeName().equals(tagname) || tagname.equals("*")) elementList.add(curr); } else { // do nothing if only one of the two is null if ("*".equals(ns) && "*".equals(tagname)) { elementList.add(curr); continue; } if (ns != null) { if ((ns.equals("*") || ns.equals(curr.getNamespaceURI())) && (tagname.equals("*") || tagname.equals(curr.getLocalName()))) elementList.add(curr); } else if (tagname.equals("*") || tagname.equals(curr.getLocalName())) elementList.add(curr); } } done = true; } public int getLength() { init(); return elementList.size(); } public Node item(int index) { init(); return (index < getLength()) ? elementList.get(index) : null; } }; }
From source file:sf.net.experimaestro.utils.JSUtils.java
/** * Converts a JavaScript object into an XML document * * @param srcObject The javascript object to convert * @param wrapName If the object is not already a document and has more than one * element child (or zero), use this to wrap the elements * @return// w w w .j a va 2s . c om */ public static Document toDocument(Scriptable scope, Object srcObject, QName wrapName) { final Object object = toDOM(scope, srcObject); if (object instanceof Document) return (Document) object; Document document = XMLUtils.newDocument(); // Add a new root element if needed NodeList childNodes; if (!(object instanceof Node)) { childNodes = (NodeList) object; } else { final Node dom = (Node) object; if (dom.getNodeType() == Node.ELEMENT_NODE) { childNodes = new NodeList() { @Override public Node item(int index) { if (index == 0) return dom; throw new IndexOutOfBoundsException(Integer.toString(index) + " out of bounds"); } @Override public int getLength() { return 1; } }; } else childNodes = dom.getChildNodes(); } int elementCount = 0; for (int i = 0; i < childNodes.getLength(); i++) if (childNodes.item(i).getNodeType() == Node.ELEMENT_NODE) elementCount++; Node root = document; if (elementCount != 1) { root = document.createElementNS(wrapName.getNamespaceURI(), wrapName.getLocalPart()); document.appendChild(root); } // Copy back in the DOM for (int i = 0; i < childNodes.getLength(); i++) { Node node = childNodes.item(i); node = node.cloneNode(true); document.adoptNode(node); root.appendChild(node); } return document; }