List of usage examples for org.w3c.dom NamedNodeMap item
public Node item(int index);
index
th item in the map. 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. j a v a 2s.c om 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:Main.java
protected static void print(PrintStream out, Node node) { if (node == null) return;/*from w w w . java2 s . co 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: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;// w w w . j a v a2 s . co m 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
protected static void print(PrintStream out, Node node) { if (node == null) return;//from w ww. j ava 2s . 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
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()); }/* w w w . j a v a2 s . c o m*/ 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; }
From source file:kenh.xscript.ScriptUtils.java
/** * Use <code>Node</code> to initial an <code>Element</code>. * @param node/* www. j a va2 s .c om*/ * @param env * @return */ private static final Element getElement(Node node, Environment env) throws UnsupportedScriptException { if (env == null) return null; String ns = node.getNamespaceURI(); // name space String name = node.getLocalName(); // name //String prefix = node.getPrefix(); // prefix Element element = env.getElement(ns, name); if (element == null) { throw new UnsupportedScriptException(null, "Could't find the element.[" + (StringUtils.isBlank(ns) ? name : ns + ":" + name) + "]"); } element.setEnvironment(env); NamedNodeMap attributes = node.getAttributes(); if (attributes != null) { for (int i = 0; i < attributes.getLength(); i++) { Node attr = attributes.item(i); String attrName = attr.getNodeName(); String attrValue = attr.getNodeValue(); if (attrName.equals("xmlns") || attrName.startsWith("xmlns:")) { if (attrName.startsWith("xmlns:")) { // to add function package String abbr = StringUtils.substringAfter(attrName, "xmlns:"); if (StringUtils.startsWithAny(abbr, "f.", "func.", "function.")) { abbr = StringUtils.substringAfter(abbr, "."); env.setFunctionPackage(abbr, attrValue); } } } else { element.setAttribute(attrName, attrValue); } } } if (includeTextNode(node)) { String text = node.getTextContent(); element.setText(text); } NodeList nodes = node.getChildNodes(); if (nodes != null) { for (int i = 0; i < nodes.getLength(); i++) { Node n = nodes.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { Element child = getElement(n, env); element.addChild(child); } } } return element; }
From source file:Main.java
private static String getMetaScriptType(List metaNodeList) { Node meta = null;/*from w ww . jav a2 s . co m*/ NamedNodeMap attributes = null; boolean httpEquiv = false; String contentScriptType = null; for (int i = metaNodeList.size() - 1; i >= 0; i--) { meta = (Node) metaNodeList.get(i); attributes = meta.getAttributes(); httpEquiv = false; contentScriptType = null; for (int j = 0; j < attributes.getLength(); j++) { if (attributes.item(j).getNodeName().equalsIgnoreCase(HTTP_EQUIV)) { httpEquiv = attributes.item(j).getNodeValue().equalsIgnoreCase(CONTENT_SCRIPT_TYPE); } else if (attributes.item(j).getNodeName().equalsIgnoreCase(CONTENT)) { contentScriptType = attributes.item(j).getNodeValue(); } } if (httpEquiv && (contentScriptType != null)) { return contentScriptType; } } return null; }
From source file:com.git.original.common.config.XMLFileConfigDocument.java
/** * XML??//from ww w .j a va2 s . c om * * @param elem * XML * @return ? */ static ConfigNode convertElement(Element elem) { if (elem == null) { return null; } ConfigNode cn = new ConfigNode(elem.getTagName(), null); NamedNodeMap attrNodeMap = elem.getAttributes(); if (attrNodeMap != null) { for (int i = 0; i < attrNodeMap.getLength(); i++) { Node node = attrNodeMap.item(i); cn.addAttribute(node.getNodeName(), node.getNodeValue()); } } NodeList nodeList = elem.getChildNodes(); if (nodeList != null && nodeList.getLength() > 0) { for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); switch (node.getNodeType()) { case Node.ATTRIBUTE_NODE: cn.addAttribute(node.getNodeName(), node.getNodeValue()); break; case Node.ELEMENT_NODE: ConfigNode child = convertElement((Element) node); cn.addChild(child); break; case Node.TEXT_NODE: cn.value = node.getNodeValue(); break; default: continue; } } } return cn; }
From source file:jp.go.nict.langrid.bpel.ProcessAnalyzer.java
private static void addNamespaceMappings(NamespaceContextImpl nsc, NamedNodeMap attrs) { for (int i = 0; i < attrs.getLength(); i++) { Attr attr = (Attr) attrs.item(i); String[] names = attr.getName().split(":", 2); if (names.length == 2) { if (names[0].equals("xmlns")) { nsc.addMapping(names[1], attr.getValue()); }/* w ww.j a v a 2 s . co m*/ } } }
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); }/* w ww .j a v a 2s. c o m*/ 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; }