List of usage examples for org.w3c.dom NamedNodeMap getLength
public int getLength();
From source file:com.tascape.qa.th.android.model.UIA.java
public static UIANode parseNode(Node node) { if (!node.getNodeName().equals(UIANode.TAG_NAME)) { return null; }//from ww w . j a v a2s . c o m NamedNodeMap map = node.getAttributes(); String klass = map.getNamedItem("class").getNodeValue(); UIANode uiNode = newNode(klass); for (int i = 0, j = map.getLength(); i < j; i++) { Node attr = map.item(i); uiNode.setAttribute(attr.getNodeName(), attr.getNodeValue()); } NodeList nl = node.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { UIANode n = parseNode(nl.item(i)); if (n == null) { continue; } uiNode.addNode(n); } return uiNode; }
From source file:Main.java
protected static void print(PrintStream out, Node node) { if (node == null) return;/*from w w w . ja v a 2s . c om*/ short type = node.getNodeType(); switch (type) { case Node.DOCUMENT_NODE: { out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); //out.println("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"); NodeList nodelist = node.getChildNodes(); int size = nodelist.getLength(); for (int i = 0; i < size; i++) print(out, nodelist.item(i)); break; } case Node.DOCUMENT_TYPE_NODE: { DocumentType docType = (DocumentType) node; out.print("<!DOCTYPE " + getDocumentTypeData(docType) + ">\n"); break; } case Node.ELEMENT_NODE: { out.print('<'); out.print(node.getNodeName()); NamedNodeMap map = node.getAttributes(); if (map != null) { int size = map.getLength(); for (int i = 0; i < size; i++) { Attr attr = (Attr) map.item(i); out.print(' '); out.print(attr.getNodeName()); out.print("=\""); out.print(normalize(attr.getNodeValue())); out.print('"'); } } if (!node.hasChildNodes()) out.print("/>"); else { out.print('>'); NodeList nodelist = node.getChildNodes(); int numChildren = nodelist.getLength(); for (int i = 0; i < numChildren; i++) print(out, nodelist.item(i)); out.print("</"); out.print(node.getNodeName()); out.print('>'); } break; } case Node.ENTITY_REFERENCE_NODE: { NodeList nodelist = node.getChildNodes(); if (nodelist != null) { int size = nodelist.getLength(); for (int i = 0; i < size; i++) print(out, nodelist.item(i)); } break; } case Node.CDATA_SECTION_NODE: { out.print(normalize(node.getNodeValue())); break; } case Node.TEXT_NODE: { out.print(normalize(node.getNodeValue())); break; } case Node.PROCESSING_INSTRUCTION_NODE: { out.print("<?"); out.print(node.getNodeName()); String s = node.getNodeValue(); if (s != null && s.length() > 0) { out.print(' '); out.print(s); } out.print("?>"); break; } case Node.COMMENT_NODE: { out.print("<!--"); out.print(node.getNodeValue()); out.print("-->"); break; } default: { out.print(normalize(node.getNodeValue())); break; } } out.flush(); }
From source file:Main.java
/** * Copies all attributes from one element to another in the official way. *//*from ww w.ja va 2 s .c o m*/ public static void copyAttributes(Element elementFrom, Element elementTo) { NamedNodeMap nodeList = elementFrom.getAttributes(); if (nodeList == null) { // No attributes to copy: just return return; } Attr attrFrom = null; Attr attrTo = null; // Needed as factory to create attrs Document documentTo = elementTo.getOwnerDocument(); int len = nodeList.getLength(); // Copy each attr by making/setting a new one and // adding to the target element. for (int i = 0; i < len; i++) { attrFrom = (Attr) nodeList.item(i); // Create an set value attrTo = documentTo.createAttribute(attrFrom.getName()); attrTo.setValue(attrFrom.getValue()); // Set in target element elementTo.setAttributeNode(attrTo); } }
From source file:Main.java
protected static void print(PrintStream out, Node node) { if (node == null) return;//from w ww.j a va2 s . c o m short type = node.getNodeType(); switch (type) { case Node.DOCUMENT_NODE: { out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); // out.println("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"); NodeList nodelist = node.getChildNodes(); int size = nodelist.getLength(); for (int i = 0; i < size; i++) print(out, nodelist.item(i)); break; } case Node.DOCUMENT_TYPE_NODE: { DocumentType docType = (DocumentType) node; out.print("<!DOCTYPE " + getDocumentTypeData(docType) + ">\n"); break; } case Node.ELEMENT_NODE: { out.print('<'); out.print(node.getNodeName()); NamedNodeMap map = node.getAttributes(); if (map != null) { int size = map.getLength(); for (int i = 0; i < size; i++) { Attr attr = (Attr) map.item(i); out.print(' '); out.print(attr.getNodeName()); out.print("=\""); out.print(normalize(attr.getNodeValue())); out.print('"'); } } if (!node.hasChildNodes()) out.print("/>"); else { out.print('>'); NodeList nodelist = node.getChildNodes(); int numChildren = nodelist.getLength(); for (int i = 0; i < numChildren; i++) print(out, nodelist.item(i)); out.print("</"); out.print(node.getNodeName()); out.print('>'); } break; } case Node.ENTITY_REFERENCE_NODE: { NodeList nodelist = node.getChildNodes(); if (nodelist != null) { int size = nodelist.getLength(); for (int i = 0; i < size; i++) print(out, nodelist.item(i)); } break; } case Node.CDATA_SECTION_NODE: { out.print(normalize(node.getNodeValue())); break; } case Node.TEXT_NODE: { out.print(normalize(node.getNodeValue())); break; } case Node.PROCESSING_INSTRUCTION_NODE: { out.print("<?"); out.print(node.getNodeName()); String s = node.getNodeValue(); if (s != null && s.length() > 0) { out.print(' '); out.print(s); } out.print("?>"); break; } case Node.COMMENT_NODE: { out.print("<!--"); out.print(node.getNodeValue()); out.print("-->"); break; } default: { out.print(normalize(node.getNodeValue())); break; } } out.flush(); }
From source file:Main.java
/** * Performs the actual recursive dumping of a DOM tree to a given * <CODE>PrintStream</CODE>. Note that dump is intended to be a detailed * debugging aid rather than pretty to look at. * /*from w w w.jav a 2 s .c o m*/ * @param out The <CODE>PrintStream</CODE> to write to. * @param node The <CODE>Node</CODE> under consideration. * @param indent The level of indentation. * @see #dump(Node) * @see #dump(PrintStream, Node) * @since TFP 1.0 */ private static void doDump(PrintStream out, final Node node, int indent) { if (node != null) { for (int index = 0; index < indent; ++index) out.write(' '); switch (node.getNodeType()) { case Node.DOCUMENT_NODE: { Document document = (Document) node; out.println("DOCUMENT:"); doDump(out, document.getDoctype(), indent + 1); doDump(out, document.getDocumentElement(), indent + 1); break; } case Node.DOCUMENT_TYPE_NODE: { DocumentType type = (DocumentType) node; out.println("DOCTYPE: [" + "name=" + format(type.getName()) + "," + "publicId=" + format(type.getPublicId()) + "," + "systemId=" + format(type.getSystemId()) + "]"); break; } case Node.ELEMENT_NODE: { Element element = (Element) node; out.println("ELEMENT: [" + "ns=" + format(element.getNamespaceURI()) + "," + "name=" + format(element.getLocalName()) + "]"); NamedNodeMap attrs = element.getAttributes(); for (int index = 0; index < attrs.getLength(); ++index) doDump(out, attrs.item(index), indent + 1); for (Node child = element.getFirstChild(); child != null;) { doDump(out, child, indent + 1); child = child.getNextSibling(); } break; } case Node.ATTRIBUTE_NODE: { Attr attr = (Attr) node; out.println("ATTRIBUTE: [" + "ns=" + format(attr.getNamespaceURI()) + "," + "prefix=" + format(attr.getPrefix()) + "," + "name=" + format(attr.getLocalName()) + "," + "value=" + format(attr.getNodeValue()) + "]"); break; } case Node.TEXT_NODE: { Text text = (Text) node; out.println("TEXT: [" + format(text.getNodeValue()) + "]"); for (Node child = text.getFirstChild(); child != null;) { doDump(out, child, indent + 1); child = child.getNextSibling(); } break; } case Node.CDATA_SECTION_NODE: { CDATASection data = (CDATASection) node; out.println("CDATA: [" + format(data.getNodeValue()) + "]"); break; } case Node.COMMENT_NODE: { Comment comm = (Comment) node; out.println("COMMENT: [" + format(comm.getNodeValue()) + "]"); break; } default: out.println("UNKNOWN: [type=" + node.getNodeType() + "]"); break; } } }
From source file:Main.java
/** * Performs the actual recursive dumping of a DOM tree to a given * <CODE>PrintWriter</CODE>. Note that dump is intended to be a detailed * debugging aid rather than pretty to look at. * /*from w ww. j a v a 2s . c o m*/ * @param out The <CODE>PrintWriter</CODE> to write to. * @param node The <CODE>Node</CODE> under consideration. * @param indent The level of indentation. * @see #dump(PrintWriter, Node) * @since TFP 1.0 */ private static void doDump(PrintWriter out, final Node node, int indent) { if (node != null) { for (int index = 0; index < indent; ++index) out.write(' '); switch (node.getNodeType()) { case Node.DOCUMENT_NODE: { Document document = (Document) node; out.println("DOCUMENT:"); doDump(out, document.getDoctype(), indent + 1); doDump(out, document.getDocumentElement(), indent + 1); break; } case Node.DOCUMENT_TYPE_NODE: { DocumentType type = (DocumentType) node; out.println("DOCTYPE: [" + "name=" + format(type.getName()) + "," + "publicId=" + format(type.getPublicId()) + "," + "systemId=" + format(type.getSystemId()) + "]"); break; } case Node.ELEMENT_NODE: { Element element = (Element) node; out.println("ELEMENT: [" + "ns=" + format(element.getNamespaceURI()) + "," + "name=" + format(element.getLocalName()) + "]"); NamedNodeMap attrs = element.getAttributes(); for (int index = 0; index < attrs.getLength(); ++index) doDump(out, attrs.item(index), indent + 1); for (Node child = element.getFirstChild(); child != null;) { doDump(out, child, indent + 1); child = child.getNextSibling(); } break; } case Node.ATTRIBUTE_NODE: { Attr attr = (Attr) node; out.println("ATTRIBUTE: [" + "ns=" + format(attr.getNamespaceURI()) + "," + "prefix=" + format(attr.getPrefix()) + "," + "name=" + format(attr.getLocalName()) + "," + "value=" + format(attr.getNodeValue()) + "]"); break; } case Node.TEXT_NODE: { Text text = (Text) node; out.println("TEXT: [" + format(text.getNodeValue()) + "]"); for (Node child = text.getFirstChild(); child != null;) { doDump(out, child, indent + 1); child = child.getNextSibling(); } break; } case Node.CDATA_SECTION_NODE: { CDATASection data = (CDATASection) node; out.println("CDATA: [" + format(data.getNodeValue()) + "]"); break; } case Node.COMMENT_NODE: { Comment comm = (Comment) node; out.println("COMMENT: [" + format(comm.getNodeValue()) + "]"); break; } default: out.println("UNKNOWN: [type=" + node.getNodeType() + "]"); break; } } }
From source file:net.roboconf.agent.internal.misc.UserDataUtils.java
private static String getSpecificAttributeOfTagInXMLFile(String filePath, String tagName, String attrName) throws ParserConfigurationException, SAXException, IOException { File fXmlFile = new File(filePath); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(fXmlFile); doc.getDocumentElement().normalize(); NodeList nList = doc.getElementsByTagName(tagName); Node aNode = nList.item(2);// www . ja v a 2s. c o m NamedNodeMap attributes = aNode.getAttributes(); String attrValue = ""; for (int a = 0; a < attributes.getLength(); a++) { Node theAttribute = attributes.item(a); if (attrName.equals(theAttribute.getNodeName())) attrValue = theAttribute.getTextContent().split(":")[0]; } return attrValue; }
From source file:DOMTreeFull.java
/** Returns a sorted list of attributes. */ static protected Attr[] sortAttributes(NamedNodeMap attrs) { int len = (attrs != null) ? attrs.getLength() : 0; Attr array[] = new Attr[len]; for (int i = 0; i < len; i++) { array[i] = (Attr) attrs.item(i); }/*from w ww . j a v a 2 s.c om*/ for (int i = 0; i < len - 1; i++) { String name = array[i].getNodeName(); int index = i; for (int j = i + 1; j < len; j++) { String curName = array[j].getNodeName(); if (curName.compareTo(name) < 0) { name = curName; index = j; } } if (index != i) { Attr temp = array[i]; array[i] = array[index]; array[index] = temp; } } return array; }
From source file:fr.gouv.finances.dgfip.xemelios.utils.XmlUtils.java
public static String getXmlDataSubstituteNode(Node node, String substituteWith, String substituteInWhat) { StringBuilder sb = new StringBuilder(); switch (node.getNodeType()) { case Node.COMMENT_NODE: case Node.ENTITY_NODE: case Node.ENTITY_REFERENCE_NODE: case Node.NOTATION_NODE: case Node.PROCESSING_INSTRUCTION_NODE: break;//from ww w . j a v a 2 s . com case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: case Node.ELEMENT_NODE: { String nodeName = node.getNodeName(); if (!substituteInWhat.equals(nodeName)) { sb.append("<").append(nodeName); StringBuilder attrs = new StringBuilder(); StringBuilder children = new StringBuilder(); NamedNodeMap nnm = node.getAttributes(); if (nnm != null) { for (int i = 0; i < nnm.getLength(); i++) { Node attr = nnm.item(i); attrs.append(" ").append(getXmlDataSubstituteNode(attr, substituteWith, substituteInWhat)); } } NodeList nl = node.getChildNodes(); if (nl != null) { for (int i = 0; i < nl.getLength(); i++) { Node child = nl.item(i); if (child.getNodeType() == Node.ATTRIBUTE_NODE) { attrs.append(" ") .append(getXmlDataSubstituteNode(child, substituteWith, substituteInWhat)); } else { children.append(getXmlDataSubstituteNode(child, substituteWith, substituteInWhat)); } } } sb.append(attrs.toString()); if (children.length() > 0) { sb.append(">").append(children.toString()).append("</").append(nodeName).append(">"); } else { sb.append("/>"); } } else { sb.append(substituteWith); } break; } case Node.ATTRIBUTE_NODE: { sb.append(node.getNodeName()).append("=\"").append(StringEscapeUtils.escapeXml(node.getNodeValue())) .append("\""); break; } case Node.CDATA_SECTION_NODE: { sb.append("<![CDATA[").append(StringEscapeUtils.escapeXml(node.getNodeValue())).append("]]>"); } case Node.TEXT_NODE: { sb.append(StringEscapeUtils.escapeXml(node.getNodeValue())); } } return sb.toString(); }
From source file:Main.java
private static Element copyNode(Document destDocument, Element dest, Element src) { NamedNodeMap namedNodeMap = src.getAttributes(); for (int i = 0; i < namedNodeMap.getLength(); i++) { Attr attr = (Attr) namedNodeMap.item(i); dest.setAttribute(attr.getName(), attr.getValue()); }/*from w w w. jav a2s. c om*/ NodeList childNodeList = src.getChildNodes(); for (int i = 0; i < childNodeList.getLength(); i++) { Node child = childNodeList.item(i); if (child.getNodeType() == Node.TEXT_NODE) { Text text = destDocument.createTextNode(child.getTextContent()); dest.appendChild(text); } else if (child.getNodeType() == Node.ELEMENT_NODE) { Element element = destDocument.createElement(((Element) child).getTagName()); element = copyNode(destDocument, element, (Element) child); dest.appendChild(element); } } return dest; }