List of usage examples for org.w3c.dom NamedNodeMap item
public Node item(int index);
index
th item in the map. From source file:Main.java
public static int getAllChildrenString(Node start, int offset) { //String spacers = " "; String tagOpen = "<"; String tagClose = ">"; String tagEndOpen = "</"; String tagEndClose = ">"; String tagName = start.getNodeName(); String tagValue = (start.getNodeValue() == null ? "" : start.getNodeValue()); if (start.getNodeName().trim().equals("#text")) { tagOpen = ""; tagClose = ""; tagName = ""; tagEndOpen = ""; tagEndClose = ""; }//from w w w . j av a2 s . c om if (offset == 0) HTMLString = ""; else { HTMLString += //spacers.substring(0, offset) + tagOpen + tagName; if (start.getNodeType() == Element.ELEMENT_NODE) { NamedNodeMap startAttr = start.getAttributes(); for (int i = 0; i < startAttr.getLength(); i++) { Node attr = startAttr.item(i); HTMLString += " " + attr.getNodeName() + "=\"" + attr.getNodeValue() + "\""; } } HTMLString += tagClose + tagValue; } for (Node child = start.getFirstChild(); child != null; child = child.getNextSibling()) { getAllChildrenString(child, offset + 1); } if (offset > 0 && tagName.length() > 0) { HTMLString += //spacers.substring(0, offset) + tagEndOpen + tagName + tagEndClose; } return ++offset; }
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. c o m 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 void printNote(NodeList nodeList) { for (int count = 0; count < nodeList.getLength(); count++) { Node tempNode = nodeList.item(count); // make sure it's element node. if (tempNode.getNodeType() == Node.ELEMENT_NODE) { // get node name and value System.out.println("\nNode Name =" + tempNode.getNodeName() + " [OPEN]"); System.out.println("Node Value =" + tempNode.getTextContent()); if (tempNode.hasAttributes()) { // get attributes names and values NamedNodeMap nodeMap = tempNode.getAttributes(); for (int i = 0; i < nodeMap.getLength(); i++) { Node node = nodeMap.item(i); System.out.println("attr name : " + node.getNodeName()); System.out.println("attr value : " + node.getNodeValue()); }//from w w w .j av a 2 s .co m } if (tempNode.hasChildNodes()) { // loop again if has child nodes printNote(tempNode.getChildNodes()); } System.out.println("Node Name =" + tempNode.getNodeName() + " [CLOSE]"); } } }
From source file:Main.java
private static Set<String> getTree(Node doc) { final Set<String> set = new TreeSet<String>(); if (doc == null) return set; String path = new String(); if (doc.getPrefix() != null && doc.getLocalName() != null) { path = new String(doc.getPrefix() + ":" + doc.getLocalName()); set.add(path);/*w w w . j av a2 s. com*/ } final NamedNodeMap attributes = doc.getAttributes(); for (int i = 0; attributes != null && i < attributes.getLength(); i++) { final Node attribute = attributes.item(i); String name = attribute.getLocalName(); if (attribute.getPrefix() != null) name = attribute.getPrefix() + ":" + name; set.add(path + "/@" + name); } final NodeList nodes = doc.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { final Node node = nodes.item(i); if (node instanceof Element) { final Set<String> children = getTree(node); for (final String child : children) { set.add(path + "/" + child); } } } return set; }
From source file:Main.java
private static void convert(Node toCopy, Node saveTo, Document doc) { Node newNode;//from w w w .ja v a 2s .c o m switch (toCopy.getNodeType()) { case Node.ELEMENT_NODE: Element newElement = doc.createElementNS(toCopy.getNamespaceURI(), toCopy.getNodeName()); newNode = newElement; Element baseElement = (Element) toCopy; NamedNodeMap children = baseElement.getAttributes(); for (int i = 0; i < children.getLength(); i++) { convertAttribute((Attr) children.item(i), newElement, doc); } break; case Node.TEXT_NODE: newNode = doc.createTextNode(toCopy.getTextContent()); break; default: newNode = null; } if (newNode != null) { NodeList children = toCopy.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { convert(children.item(i), newNode, doc); } saveTo.appendChild(newNode); } }
From source file:Main.java
/** * Extract default values of variables defined in the DOM subtree below node. * Default values are used to populate the variableDefs map. Variables * already defined in this map will NOT be modified. * * @param node root node of DOM subtree to extract default values from. * @param variableDefs map which default values will be added to. *//*from www . j av a 2 s .c o m*/ public static void extractVariableDefaults(final Node node, Map<String, String> variableDefs) { switch (node.getNodeType()) { case Node.ELEMENT_NODE: final Element element = (Element) node; final NamedNodeMap attrs = element.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Attr attr = (Attr) attrs.item(i); extractVariableDefaultsFromString(attr.getValue(), variableDefs); } break; case Node.CDATA_SECTION_NODE: String content = node.getTextContent(); extractVariableDefaultsFromString(content, variableDefs); break; default: break; } final NodeList children = node.getChildNodes(); for (int childIndex = 0; childIndex < children.getLength(); childIndex++) extractVariableDefaults(children.item(childIndex), variableDefs); }
From source file:Main.java
public static HashMap<String, String> convertXmlAttributeToHashMap(NamedNodeMap inputNodeMap) { HashMap<String, String> outputHashMap = new HashMap<String, String>(); for (int i = 0; i < inputNodeMap.getLength(); i++) { Attr inputNodeMapAttribute = (Attr) inputNodeMap.item(i); outputHashMap.put(inputNodeMapAttribute.getName().trim(), inputNodeMapAttribute.getValue().trim()); }// w w w .ja v a2 s . c o m return outputHashMap; }
From source file:Main.java
public static Element overrideXml(Element target, Element parent) { if (parent != null) { NamedNodeMap namedNodeMap = parent.getAttributes(); for (int i = 0; i < namedNodeMap.getLength(); i++) { Node attributeNode = namedNodeMap.item(i); String parentAttributeName = attributeNode.getNodeName(); String parentAttributeValue = attributeNode.getNodeValue(); // attribute override if (!target.hasAttribute(parentAttributeName)) { target.setAttribute(parentAttributeName, parentAttributeValue); }//from www . j a va 2s . c om // children override if (parent.getChildNodes().getLength() > 0) { if (target.getChildNodes().getLength() == 0) { for (int j = 0; j < target.getChildNodes().getLength(); j++) { target.appendChild(target.getChildNodes().item(j)); } } } } } return target; }
From source file:Main.java
private static Map attrbiuteToMap(NamedNodeMap attributes) { if (attributes == null) return new LinkedHashMap(); Map result = new LinkedHashMap(); for (int i = 0; i < attributes.getLength(); i++) { result.put(attributes.item(i).getNodeName(), attributes.item(i).getNodeValue()); }//from ww w .j av a2 s . c om return result; }
From source file:Main.java
/** * Get all attributes of the specified element *//*from w ww . j ava2 s.c o m*/ public static ArrayList<Attr> getAllAttributes(Element element) { final NamedNodeMap nodeMap = element.getAttributes(); final ArrayList<Attr> result = new ArrayList<Attr>(); for (int i = 0; i < nodeMap.getLength(); i++) result.add((Attr) nodeMap.item(i)); return result; }