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
/** * Get the first child Element of the supplied node. * * @param node The DOM Node.//www . jav a 2s. com * @return The first child element */ public static Element getFirstChildElement(Node node) { NodeList children = node.getChildNodes(); int childCount = children.getLength(); for (int i = 0; i < childCount; i++) { Node child = children.item(i); if (child != null && child.getNodeType() == Node.ELEMENT_NODE) { return (Element) child; } } return null; }
From source file:Main.java
public static void walkNodes(Node nodeIn, StringBuffer sb, String sPad) { if (nodeIn == null) return;// w ww . ja v a2 s.com NamedNodeMap map = nodeIn.getAttributes(); if (map != null) for (int i = 0; i < map.getLength(); i++) { Node n = map.item(i); if (n.getNodeType() == Node.ATTRIBUTE_NODE) { sb.append(sPad + " Attribute:" + n.getNodeName() + " = " + n.getNodeValue() + '\n'); } } NodeList nodes = nodeIn.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node n = nodes.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { sb.append(sPad + "Element: " + n.getNodeName() + '\n'); } if (n.getNodeType() == Node.TEXT_NODE) { sb.append(sPad + " Value: = " + n.getNodeValue() + '\n'); } if (n.getNodeType() == Node.ELEMENT_NODE) { walkNodes(n, sb, sPad + " "); } } }
From source file:Main.java
private static String getTextContent(final Node node) { switch (node.getNodeType()) { case Node.ELEMENT_NODE: case Node.ATTRIBUTE_NODE: case Node.ENTITY_NODE: case Node.ENTITY_REFERENCE_NODE: case Node.DOCUMENT_FRAGMENT_NODE: return mergeTextContent(node.getChildNodes()); case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: case Node.COMMENT_NODE: case Node.PROCESSING_INSTRUCTION_NODE: return node.getNodeValue(); case Node.DOCUMENT_NODE: case Node.DOCUMENT_TYPE_NODE: case Node.NOTATION_NODE: default://from w w w .j av a 2 s. c o m return null; } }
From source file:Main.java
public static List<Map<String, String>> ReadFolderItemsFromFile(String path, String filename, String folderID) { List<Map<String, String>> results = new ArrayList<Map<String, String>>(); try {//from w ww. j a va 2 s . com File fXmlFile = new File(path, filename); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(fXmlFile); doc.getDocumentElement().normalize(); NodeList nList = null; if (folderID == null) { // get all the children of the root nList = doc.getChildNodes().item(0).getChildNodes(); } else { //Log.d("ReadItemsFromFile", "Current FolderID: " + folderID); NodeList folderList = doc.getElementsByTagName("folder"); for (int i = 0; i < folderList.getLength(); i++) { Node curNode = folderList.item(i); if (curNode.getNodeType() == Node.ELEMENT_NODE) { String curNodeId = ((Element) curNode).getElementsByTagName("id").item(0).getTextContent(); //Log.d("ReadItemsFromFile", "Number of items: " + curNodeId); if (curNodeId.equals(folderID)) { //Log.d("ReadItemsFromFile", "Found the folder"); NodeList folderChildren = curNode.getChildNodes(); for (int j = 0; j < folderChildren.getLength(); j++) { //Log.d("ReadItemsFromFile", "node name: " + folderChildren.item(j).getNodeName()); if (folderChildren.item(j).getNodeName().equals("contents")) { //Log.d("ReadItemsFromFile", "found the contents child"); nList = folderChildren.item(j).getChildNodes(); break; } } break; } } } } if (nList != null) { Log.d("ReadItemsFromFile", "-----------------------"); Log.d("ReadItemsFromFile", "Number of items: " + nList.getLength()); for (int temp = 0; temp < nList.getLength(); temp++) { Node nNode = nList.item(temp); //Log.d("ReadItemsFromFile", temp + ". node type: " + nNode.getNodeType()); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; Node elementNameNode = eElement.getElementsByTagName("name").item(0); String elementNameString = elementNameNode.getTextContent(); //Log.d("ReadItemsFromFile", "Name: " + elementNameString); HashMap<String, String> mapElement = new HashMap<String, String>(); mapElement.put("name", elementNameString); mapElement.put("id", eElement.getElementsByTagName("id").item(0).getTextContent()); mapElement.put("type", nNode.getNodeName()); results.add(mapElement); } } } } catch (Exception e) { e.printStackTrace(); return null; } return results; }
From source file:Main.java
/** * Gets the elements./*ww w. ja va2 s . com*/ * * @param topElm * the top elm * @return the elements */ public static List<Element> getElements(Element topElm) { List<Element> retVals = new ArrayList<Element>(); NodeList childNodes = topElm.getChildNodes(); for (int i = 0, ll = childNodes.getLength(); i < ll; i++) { Node n = childNodes.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { retVals.add((Element) n); } } return retVals; }
From source file:Main.java
public static <T> Map<String, T> loadBeans(Node parent, Class<T> beanClass) throws IntrospectionException, InstantiationException, IllegalAccessException, IllegalArgumentException, DOMException, InvocationTargetException { Map<String, T> store = new HashMap<String, T>(); Node node = parent.getFirstChild(); while (node != null) { if (node.getNodeType() == Node.ELEMENT_NODE) { String id = getId(node); T bean = loadBean(node, beanClass); store.put(id, bean);//from w w w .j ava2s .c o m } node = node.getNextSibling(); } return store; }
From source file:Main.java
public static void stripDuplicateAttributes(Node node, Node parent) { // The output depends on the type of the node switch (node.getNodeType()) { case Node.DOCUMENT_NODE: { Document doc = (Document) node; Node child = doc.getFirstChild(); while (child != null) { stripDuplicateAttributes(child, node); child = child.getNextSibling(); }/* www . j a v a 2 s .c om*/ break; } case Node.ELEMENT_NODE: { Element elt = (Element) node; NamedNodeMap attrs = elt.getAttributes(); ArrayList nodesToRemove = new ArrayList(); int nodesToRemoveNum = 0; for (int i = 0; i < attrs.getLength(); i++) { final Node a = attrs.item(i); for (int j = 0; j < attrs.getLength(); j++) { final Node b = attrs.item(j); //if there are two attributes with same name if (i != j && (a.getNodeName().equals(b.getNodeName()))) { nodesToRemove.add(b); nodesToRemoveNum++; } } } for (int i = 0; i < nodesToRemoveNum; i++) { Attr nodeToDelete = (Attr) nodesToRemove.get(i); Element nodeToDeleteParent = (Element) node; // nodeToDelete.getParentNode(); nodeToDeleteParent.removeAttributeNode(nodeToDelete); } nodesToRemove.clear(); Node child = elt.getFirstChild(); while (child != null) { stripDuplicateAttributes(child, node); child = child.getNextSibling(); } break; } default: //do nothing break; } }
From source file:Main.java
/** * Checks if is element exists by tag name. * @param element the element//w ww. j a va2 s . co m * @param tagName the tag name * @return true, if is element exists by tag name */ public static boolean isElementExistsByTagName(final Element element, final String tagName) { if (element == null || tagName == null || tagName.isEmpty()) { return false; } // Extract all children on element final NodeList res = element.getChildNodes(); for (int i = 0; i < res.getLength(); i++) { final Node node = res.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { final Element elem = (Element) node; // Check matching with tagname expected if (elem.getTagName().equals(tagName)) { return true; } } } return false; }
From source file:Main.java
/** Prints the specified node, recursively. * * @param node Node to be printed/*from w w w . j ava2 s . com*/ */ public static void print(Node node) { // is there anything to do? if (node == null) { return; } System.out.println(""); int type = node.getNodeType(); switch (type) { // print document case Node.DOCUMENT_NODE: { /* if (!canonical) { if (Encoding.equalsIgnoreCase("DEFAULT")) Encoding = "UTF-8"; else if(Encoding.equalsIgnoreCase("Unicode")) Encoding = "UTF-16"; else Encoding = MIME2Java.reverse(Encoding); out.println("<?xml version=\"1.0\" encoding=\"" + Encoding + "\"?>"); } */ print(((Document) node).getDocumentElement()); out.flush(); break; } // print element with attributes case Node.ELEMENT_NODE: { out.print('<'); out.print(node.getNodeName()); Attr attrs[] = sortAttributes(node.getAttributes()); for (int i = 0; i < attrs.length; i++) { Attr attr = attrs[i]; out.print(' '); out.print(attr.getNodeName()); out.print("=\""); out.print(normalize(attr.getNodeValue())); out.print('"'); } out.print('>'); NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) { print(children.item(i)); } } break; } // handle entity reference nodes case Node.ENTITY_REFERENCE_NODE: { if (canonical) { NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) { print(children.item(i)); } } } else { out.print('&'); out.print(node.getNodeName()); out.print(';'); } break; } // print cdata sections case Node.CDATA_SECTION_NODE: { if (canonical) { out.print(normalize(node.getNodeValue())); } else { out.print("<![CDATA["); out.print(node.getNodeValue()); out.print("]]>"); } break; } // print DocumentType sections case Node.DOCUMENT_TYPE_NODE: { out.print("<!DOCTYPE "); out.print(((DocumentType) node).getName()); out.print(" SYSTEM "); out.print(((DocumentType) node).getSystemId()); out.print(">"); break; } // print text case Node.TEXT_NODE: { out.print(normalize(node.getNodeValue())); break; } // print processing instruction case Node.PROCESSING_INSTRUCTION_NODE: { out.print("<?"); out.print(node.getNodeName()); String data = node.getNodeValue(); if (data != null && data.length() > 0) { out.print(' '); out.print(data); } out.print("?>"); break; } } if (type == Node.ELEMENT_NODE) { out.print("</"); out.print(node.getNodeName()); out.print('>'); } out.flush(); }
From source file:Main.java
/** * Used for debuging//from w w w . j a v a2s . c o m * * @param parent * Element * @param out * PrintStream * @param deep * boolean * @param prefix * String */ public static void printChildElements(Element parent, PrintStream out, boolean deep, String prefix) { out.print(prefix + "<" + parent.getNodeName()); NamedNodeMap attrs = parent.getAttributes(); Node node; for (int i = 0; i < attrs.getLength(); i++) { node = attrs.item(i); out.print(" " + node.getNodeName() + "=\"" + node.getNodeValue() + "\""); } out.println(">"); // String data = getElementTextValueDeprecated(parent); String data = parent.getNodeValue(); if (data != null && data.trim().length() > 0) { out.println(prefix + "\t" + data); } data = getElementCDataValue(parent); if (data != null && data.trim().length() > 0) { out.println(prefix + "\t<![CDATA[" + data + "]]>"); } NodeList nodes = parent.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { node = nodes.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { if (deep) { printChildElements((Element) node, out, deep, prefix + "\t"); } else { out.println(prefix + node.getNodeName()); } } } out.println(prefix + "</" + parent.getNodeName() + ">"); }