List of usage examples for org.w3c.dom.traversal NodeFilter SHOW_ALL
int SHOW_ALL
To view the source code for org.w3c.dom.traversal NodeFilter SHOW_ALL.
Click Source Link
Nodes. From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder loader = factory.newDocumentBuilder(); Document document = loader.parse("sample.xml"); DocumentTraversal traversal = (DocumentTraversal) document; NodeIterator iterator = traversal.createNodeIterator(document.getDocumentElement(), NodeFilter.SHOW_ALL, new ItemFilter(), true); for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) { System.out.println("Element: " + ((Element) n).getTagName()); }/* w ww.j a v a2 s . c om*/ }
From source file:DemoTreeWalker.java
public static void main(String args[]) throws SAXException, IOException { DOMParser parser = new DOMParser(); parser.parse("games.xml"); Document doc = parser.getDocument(); DocumentTraversal docTraversal = (DocumentTraversal) doc; TreeWalker iter = docTraversal.createTreeWalker(doc.getDocumentElement(), NodeFilter.SHOW_ALL, null, false); Node n = null;/*from www . ja v a2 s. c om*/ while ((n = iter.nextNode()) != null) { System.out.println("Node name: " + n.getNodeName()); System.out.println("Node value: " + n.getNodeValue()); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document document = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData()))); DocumentTraversal traversal = (DocumentTraversal) document; NodeIterator iterator = traversal.createNodeIterator(document.getDocumentElement(), NodeFilter.SHOW_ALL, new ItemFilter(), true); for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) { System.out.println("Element: " + ((Element) n).getTagName()); }/*from ww w.j a v a 2s. c om*/ }
From source file:DOMTreeWalkerTreeModel.java
/** * This main() method demonstrates the use of this class, the use of the * Xerces DOM parser, and the creation of a DOM Level 2 TreeWalker object. *//*w ww. j a v a 2 s .c om*/ public static void main(String[] args) throws IOException, SAXException { // Obtain an instance of a Xerces parser to build a DOM tree. // Note that we are not using the JAXP API here, so this // code uses Apache Xerces APIs that are not standards DOMParser parser = new org.apache.xerces.parsers.DOMParser(); // Get a java.io.Reader for the input XML file and // wrap the input file in a SAX input source Reader in = new BufferedReader(new FileReader(args[0])); InputSource input = new org.xml.sax.InputSource(in); // Tell the Xerces parser to parse the input source parser.parse(input); // Ask the parser to give us our DOM Document. Once we've got the DOM // tree, we don't have to use the Apache Xerces APIs any more; from // here on, we use the standard DOM APIs Document document = parser.getDocument(); // If we're using a DOM Level 2 implementation, then our Document // object ought to implement DocumentTraversal DocumentTraversal traversal = (DocumentTraversal) document; // For this demonstration, we create a NodeFilter that filters out // Text nodes containing only space; these just clutter up the tree NodeFilter filter = new NodeFilter() { public short acceptNode(Node n) { if (n.getNodeType() == Node.TEXT_NODE) { // Use trim() to strip off leading and trailing space. // If nothing is left, then reject the node if (((Text) n).getData().trim().length() == 0) return NodeFilter.FILTER_REJECT; } return NodeFilter.FILTER_ACCEPT; } }; // This set of flags says to "show" all node types except comments int whatToShow = NodeFilter.SHOW_ALL & ~NodeFilter.SHOW_COMMENT; // Create a TreeWalker using the filter and the flags TreeWalker walker = traversal.createTreeWalker(document, whatToShow, filter, false); // Instantiate a TreeModel and a JTree to display it JTree tree = new JTree(new DOMTreeWalkerTreeModel(walker)); // Create a frame and a scrollpane to display the tree, and pop them up JFrame frame = new JFrame("DOMTreeWalkerTreeModel Demo"); frame.getContentPane().add(new JScrollPane(tree)); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); frame.setSize(500, 250); frame.setVisible(true); }
From source file:Main.java
/** * Creates a Node iterator from the given Document object, starting from the given node.<br> * @param doc document from which iterator will be created * @param node node from which iterator we will start * @return the resulting iterator//from w w w . ja v a 2s. c o m */ public static NodeIterator getNodeIterator(Document doc, Node node) { DocumentTraversal traversal = ((DocumentTraversal) doc); return (traversal).createNodeIterator(node, NodeFilter.SHOW_ALL, null, true); }
From source file:DOMTreeWalkerTreeModel.java
/** * Create a TreeModel for a TreeWalker that returns all nodes in the * specified document//ww w.ja va 2 s . co m */ public DOMTreeWalkerTreeModel(Document document) { DocumentTraversal dt = (DocumentTraversal) document; walker = dt.createTreeWalker(document, NodeFilter.SHOW_ALL, null, false); }
From source file:DOMTreeWalkerTreeModel.java
/** * Create a TreeModel for a TreeWalker that returns the specified element * and all of its descendant nodes./* w w w. j a v a 2s . c o m*/ */ public DOMTreeWalkerTreeModel(Element element) { DocumentTraversal dt = (DocumentTraversal) element.getOwnerDocument(); walker = dt.createTreeWalker(element, NodeFilter.SHOW_ALL, null, false); }
From source file:ItemSearcher.java
/** * <p>This method takes a file, and searches it for specific * pieces of data using DOM traversal.</p> * * @param filename name of XML file to search through. * @throws <code>Exception</code> - generic problem handling. *//*from ww w . ja v a 2s. co m*/ public void search(String filename) throws Exception { // Parse into a DOM tree File file = new File(filename); DOMParser parser = new DOMParser(); parser.parse(file.toURL().toString()); Document doc = parser.getDocument(); // Get node to start iterating with Element root = doc.getDocumentElement(); NodeList descriptionElements = root.getElementsByTagNameNS(docNS, "description"); Element description = (Element) descriptionElements.item(0); // Get a NodeIterator NodeIterator i = ((DocumentTraversal) doc).createNodeIterator(description, NodeFilter.SHOW_ALL, new FormattingNodeFilter(), true); Node n; while ((n = i.nextNode()) != null) { System.out.println("Search phrase found: '" + n.getNodeValue() + "'"); } }
From source file:de.betterform.xml.xforms.model.Instance.java
/** * Inserts the specified node.//from w ww . j a va 2 s. com * * @param parentNode the path pointing to the origin node. * @param beforeNode the path pointing to the node before which a clone of the * origin node will be inserted. * @return */ public Node insertNode(Node parentNode, Node originNode, Node beforeNode) throws XFormsException { // insert a deep clone of 'origin' node before 'before' node. if // 'before' node is null, the clone will be appended to 'parent' node. ModelItem miOrigin = getModelItem(originNode); Node insertedNode; if (originNode.getNodeType() == Node.ATTRIBUTE_NODE) { insertedNode = this.instanceDocument.importNode(originNode, true); ((Element) parentNode).setAttributeNode((Attr) insertedNode); } else { insertedNode = parentNode.insertBefore(this.instanceDocument.importNode(originNode, true), beforeNode); } String canonPath = DOMUtil.getCanonicalPath(insertedNode); ModelItem insertedModelItem = getModelItem(insertedNode); insertedModelItem.getDeclarationView().setDatatype(miOrigin.getDeclarationView().getDatatype()); insertedModelItem.getDeclarationView().setConstraints(miOrigin.getDeclarationView().getConstraints()); // get canonical path for inserted node String canonicalPath; if (beforeNode != null || originNode.getNodeType() == Node.ATTRIBUTE_NODE) { canonicalPath = BindingResolver.getCanonicalPath(insertedNode); } else { Node lastChild = ((DocumentTraversal) instanceDocument) .createTreeWalker(parentNode, NodeFilter.SHOW_ALL, null, false).lastChild(); canonicalPath = BindingResolver.getCanonicalPath(lastChild); } String[] canonicalParts = XPathUtil.getNodesetAndPredicates(canonicalPath); if (originNode.hasChildNodes()) { setDatatypeOnChilds(originNode, insertedNode); } // dispatch internal betterform event (for instant repeat updating) HashMap map = new HashMap(); map.put("nodeset", canonicalParts[0]); map.put("position", canonicalParts.length > 1 ? canonicalParts[1] : "1"); map.put("canonPath", canonPath); this.container.dispatch(this.target, BetterFormEventNames.NODE_INSERTED, map); if (getLogger().isDebugEnabled()) { getLogger().debug( this + " insert node: instance data after manipulation" + toString(this.instanceDocument)); } return insertedNode; }
From source file:org.alfresco.repo.template.XSLTProcessorMethodInvoker.java
public Object invokeMethod(final String id, Object[] arguments) throws Exception { if (!PROCESSOR_METHODS.containsKey(id)) { throw new NullPointerException("unable to find method " + id); }/*from w w w . j ava 2 s .c o m*/ final TemplateProcessorMethod method = PROCESSOR_METHODS.get(id); arguments = this.convertArguments(arguments); log.debug("invoking " + id + " with " + arguments.length); Object result = method.exec(arguments); log.debug(id + " returned a " + result); if (result == null) { return null; } else if (result.getClass().isArray() && Node.class.isAssignableFrom(result.getClass().getComponentType())) { log.debug("converting " + result + " to a node iterator"); final Node[] array = (Node[]) result; return new NodeIterator() { private int index = 0; private boolean detached = false; public void detach() { if (log.isDebugEnabled()) log.debug("detaching NodeIterator"); this.detached = true; } public boolean getExpandEntityReferences() { return true; } public int getWhatToShow() { return NodeFilter.SHOW_ALL; } public Node getRoot() { return (array.length == 0 ? null : array[0].getOwnerDocument().getDocumentElement()); } public NodeFilter getFilter() { return new NodeFilter() { public short acceptNode(final Node n) { return NodeFilter.FILTER_ACCEPT; } }; } public Node nextNode() throws DOMException { if (log.isDebugEnabled()) log.debug("NodeIterator.nextNode(" + index + ")"); if (this.detached) throw new DOMException(DOMException.INVALID_STATE_ERR, null); return index == array.length ? null : array[index++]; } public Node previousNode() throws DOMException { if (log.isDebugEnabled()) log.debug("NodeIterator.previousNode(" + index + ")"); if (this.detached) throw new DOMException(DOMException.INVALID_STATE_ERR, null); return index == -1 ? null : array[index--]; } }; } else if (result instanceof String || result instanceof Number || result instanceof Node) { log.debug("returning " + result + " as is"); return result; } else { throw new IllegalArgumentException("unable to convert " + result.getClass().getName()); } }