List of usage examples for org.w3c.dom Node ELEMENT_NODE
short ELEMENT_NODE
To view the source code for org.w3c.dom Node ELEMENT_NODE.
Click Source Link
Element
. From source file:Main.java
/** * Check whether the given nodes containes an attribute with the given name and value.<br> * The name and value of the attribute MUST be the same of the node ones in order to have true as * a result.<br> The check is case insensitive.<br> * // w w w. j av a 2 s . c o m * @param node node to be analyzed * @param attrName the attribute name to be matched * @param attrValue the attribute value to be matched * @return <code>true</code> if node containes the given attributes, <code>false</code> otherwise (even if node is null) */ public static boolean nodeAttributeEquals(Node node, String attrName, String attrValue) { if (node != null && node.getNodeType() == Node.ELEMENT_NODE) { String value = ((Element) node).getAttribute(attrName); return (value != null && value.equalsIgnoreCase(attrValue)); } else { return false; } }
From source file:Main.java
/** Look for Element node of given name. * * <p>Checks the node itself and its siblings for an {@link Element}. * Does not descent down the 'child' links. * * @param node Node where to start.//from w w w .j a va 2 s . c o m * @param name Name of the node to look for. * @return Returns node, the next matching sibling, or <code>null</code>. */ private static final Element findElementByName(Node node, final String name) { while (node != null) { if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(name)) return (Element) node; node = node.getNextSibling(); } return null; }
From source file:Main.java
private static void removeEmptyChildElements(Element parentElement) { List<Element> toRemove = new LinkedList<Element>(); NodeList children = parentElement.getChildNodes(); int childrenCount = children.getLength(); for (int i = 0; i < childrenCount; ++i) { Node child = children.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { Element childElement = (Element) child; removeEmptyChildElements(childElement); if (elementIsRedundant(childElement)) { toRemove.add(childElement); }/* w w w. j av a 2s .c o m*/ } } for (Element childElement : toRemove) { parentElement.removeChild(childElement); } parentElement.normalize(); }
From source file:Main.java
public static String nodeToString(Node n) { StringBuilder sb = new StringBuilder(); if (n instanceof Text) return n.getNodeValue().trim(); else {/*from w w w . j av a2s . co m*/ NodeList nl = n.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node child = nl.item(i); sb.append(nodeToString(child)); } if (n.getNodeType() == Node.ELEMENT_NODE) { if ("td".equalsIgnoreCase(n.getNodeName())) { sb.append("\t"); } else if ("th".equalsIgnoreCase(n.getNodeName())) { sb.append("\t"); } else if ("tr".equalsIgnoreCase(n.getNodeName())) { sb.append("\n"); } else if ("br".equalsIgnoreCase(n.getNodeName())) { sb.append("\n"); } } return sb.toString(); } }
From source file:Main.java
/** * Remove <tt>xml:base</tt> attributes from the given node and all of its * descendants. According to section 1.3.4 of the DOM Level 3 Core * specification, when expanding an external entity reference, DOM parser * will add <tt>xml:base</tt> attributes to all elements appearing in the * external entity. These attributes don't appear in the reference output * files provided in the test suite (see * {@link XMLConformanceTest#getOutput()}). This method can be used to * remove <tt>xml:base</tt> attributes in DOM documents. * /*from w ww .ja v a 2s . c om*/ * @param node * the node to process */ public static void removeXmlBaseAttributes(Node node) { if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; element.removeAttribute("xml:base"); } Node child = node.getFirstChild(); while (child != null) { removeXmlBaseAttributes(child); child = child.getNextSibling(); } }
From source file:MainClass.java
public void processNode(Node node, String spacer) throws IOException { if (node == null) return;//from w w w. j a v a2s. c o m switch (node.getNodeType()) { case Node.ELEMENT_NODE: String name = node.getNodeName(); System.out.print(spacer + "<" + name); NamedNodeMap nnm = node.getAttributes(); for (int i = 0; i < nnm.getLength(); i++) { Node current = nnm.item(i); System.out.print(" " + current.getNodeName() + "= " + current.getNodeValue()); } System.out.print(">"); NodeList nl = node.getChildNodes(); if (nl != null) { for (int i = 0; i < nl.getLength(); i++) { processNode(nl.item(i), ""); } } System.out.println(spacer + "</" + name + ">"); break; case Node.TEXT_NODE: System.out.print(node.getNodeValue()); break; case Node.CDATA_SECTION_NODE: System.out.print("" + node.getNodeValue() + ""); break; case Node.ENTITY_REFERENCE_NODE: System.out.print("&" + node.getNodeName() + ";"); break; case Node.ENTITY_NODE: System.out.print("<ENTITY: " + node.getNodeName() + "> </" + node.getNodeName() + "/>"); break; case Node.DOCUMENT_NODE: NodeList nodes = node.getChildNodes(); if (nodes != null) { for (int i = 0; i < nodes.getLength(); i++) { processNode(nodes.item(i), ""); } } break; case Node.DOCUMENT_TYPE_NODE: DocumentType docType = (DocumentType) node; System.out.print("<!DOCTYPE " + docType.getName()); if (docType.getPublicId() != null) { System.out.print(" PUBLIC " + docType.getPublicId() + " "); } else { System.out.print(" SYSTEM "); } System.out.println(" " + docType.getSystemId() + ">"); break; default: break; } }
From source file:Main.java
/** * Searches the node for content of type Long. If non-long content is found, * it logs a warning and returns null.//from w ww.j av a 2 s . c o m */ public static String[] extractStringArrayFromElement(Node element) { // Get all the children NodeList children = element.getChildNodes(); List<String> output = new ArrayList<String>(); for (int n = 0; n < children.getLength(); n++) { Node child = children.item(n); if (child.getNodeType() == Node.ELEMENT_NODE) { output.add(extractStringFromElement(child)); } } return output.toArray(new String[output.size()]); }
From source file:Main.java
/** * Find an immediate child of the given name *//*from ww w . j a va 2s . c o m*/ public static Element findImmediateChildElement(Node node, String name) { Element found = null; if (node != null) { for (Node child = node.getFirstChild(); child != null && found == null; child = child.getNextSibling()) { if (child.getNodeType() == Node.ELEMENT_NODE) { Element tmp = (Element) child; if (tmp.getTagName().equals(name)) { return tmp; } } } } return found; }
From source file:Main.java
public static String findPrefixForNamespace(Element elm, String namespace) { while (elm != null) { NamedNodeMap attributes = elm.getAttributes(); for (int c = 0; c < attributes.getLength(); c++) { if (attributes.item(c).getNodeValue().equals(namespace) && attributes.item(c).getNodeName().startsWith("xmlns:")) { return attributes.item(c).getNodeName().substring(6); }// ww w . j a v a2 s . c o m } if (elm.getParentNode().getNodeType() != Node.ELEMENT_NODE) break; elm = (Element) elm.getParentNode(); } return null; }
From source file:org.shareok.data.documentProcessor.FileHandlerFactory.java
/** * * @param fileExtension//w w w .ja v a 2 s . c om * @return */ public static FileHandler getFileHandlerByFileExtension(String fileExtension) { FileHandler fh = null; String beanName = ""; try { String fileTypePath = DocumentProcessorUtil.getFilePathFromResources("filetypes.xml"); Document fileTypeDoc = loadXMLFromString(fileTypePath); fileTypeDoc.getDocumentElement().normalize(); Element docEle = fileTypeDoc.getDocumentElement(); NodeList nl = docEle.getChildNodes(); if (nl != null && nl.getLength() > 0) { for (int i = 0; i < nl.getLength(); i++) { if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) { Element el = (Element) nl.item(i); String nodeVal = el.getTextContent(); if (nodeVal.equals(fileExtension)) { beanName = el.getAttribute("bean"); break; } } } } ApplicationContext context = new ClassPathXmlApplicationContext("documentProcessorContext.xml"); fh = (FileHandler) context.getBean(beanName); } catch (Exception ex) { Logger.getLogger(DocumentProcessorUtil.class.getName()).log(Level.SEVERE, null, ex); } return fh; }