List of usage examples for org.w3c.dom Node getNodeName
public String getNodeName();
From source file:Main.java
/** * Namespace-aware equals comparison. Returns <code>true</code> if either * {@link Node#getLocalName} or {@link Node#getNodeName} equals <code>desiredName</code>, * otherwise returns <code>false</code>. *///from w w w . ja v a 2 s . co m public static boolean nodeNameEquals(Node node, String desiredName) { assert node != null : "Node must not be null"; assert desiredName != null : "Desired getName must not be null"; return desiredName.equals(node.getNodeName()) || desiredName.equals(node.getLocalName()); }
From source file:Main.java
public static Element findSingleElement(Element parent, String fullXPath) { Element elt = null;/*from w w w . j av a 2 s.c o m*/ if (parent != null) { if (parent.hasChildNodes()) { if (fullXPath.startsWith("/")) fullXPath = fullXPath.substring(1); int index = fullXPath.indexOf("/"); String childName = ((index != -1) ? fullXPath.substring(0, index) : fullXPath); NodeList list = parent.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node child = list.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { if (child.getNodeName().equalsIgnoreCase(childName)) { if (index == -1) { elt = (Element) child; break; } else { fullXPath = fullXPath.substring(index + 1); elt = findSingleElement((Element) child, fullXPath); } } } } } } return elt; }
From source file:Main.java
private static void print(Node e, String tab) { if (e.getNodeType() == Node.TEXT_NODE) { System.out.println(tab + e.getNodeValue()); return;// w ww.java 2 s .co m } System.out.print(tab + e.getNodeName()); NamedNodeMap as = e.getAttributes(); if (as != null && as.getLength() > 0) { System.out.print(" attributes=["); for (int i = 0; i < as.getLength(); i++) System.out.print((i == 0 ? "" : ", ") + as.item(i)); System.out.print("]"); } System.out.println(); if (e.getNodeValue() != null) System.out.println(tab + " " + e.getNodeValue()); NodeList childs = e.getChildNodes(); for (int i = 0; i < childs.getLength(); i++) print(childs.item(i), tab + " "); }
From source file:Main.java
/**prints the type of the input node * @param node node to print type of/*from w w w .ja v a2 s.c o m*/ * @param ident amount to indent*/ public static void printNodeType(Node node, int ident) { System.out.print("Node: " + node.getNodeName() + " "); switch (node.getNodeType()) { case Node.DOCUMENT_NODE: System.out.println("Document Node"); break; case Node.ELEMENT_NODE: System.out.println("Element Node"); for (int j = 0; j < 2 * ident; j++) System.out.print(" "); System.out.println("It has the following Children"); NodeList children = node.getChildNodes(); if (children != null) { for (int i = 0; i < children.getLength(); i++) { for (int j = 0; j < ident; j++) System.out.print(" "); System.out.print("Child " + ident + "." + i + " = "); printNodeType(children.item(i), ident + 1); } System.out.println(); } break; case Node.TEXT_NODE: System.out.println("->" + node.getNodeValue().trim() + "<-"); break; case Node.CDATA_SECTION_NODE: System.out.println("CData Node"); break; case Node.PROCESSING_INSTRUCTION_NODE: System.out.println("Proposing Instruction Node"); break; case Node.ENTITY_REFERENCE_NODE: System.out.println("Entity Node"); break; case Node.DOCUMENT_TYPE_NODE: System.out.println("Document Node"); break; default: } }
From source file:Main.java
static public Element findChildElement(Node first, Node last, String name) { while (first != last) { if (first.getNodeType() == Node.ELEMENT_NODE) { if (first.getNodeName().equals(name)) return (Element) first; }// w w w. j a v a2 s . c om first = first.getNextSibling(); } return null; }
From source file:Main.java
/** * Get the first found child element with the provided name that is a direct * child the provided element./*from w w w .j a v a 2s . c o m*/ * * @param parent * The parent element * @param name * The name of the child element to find * @return The first found child element, null if no matching children */ public static Element getFirstChildElementByName(Element parent, String name) { assertNotNull(parent); NodeList children = parent.getChildNodes(); Node node; for (int i = 0; i < children.getLength(); i++) { node = children.item(i); if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(name)) { return (Element) node; } } return null; }
From source file:Main.java
public static String findXslHref(Node node) { int type = node.getNodeType(); if (type == Node.PROCESSING_INSTRUCTION_NODE) { String nodeName = node.getNodeName(); if (nodeName.equalsIgnoreCase("xml-stylesheet")) { String nodeValue = node.getNodeValue(); try { int i = nodeValue.indexOf("href=\""); int j = nodeValue.indexOf("\"", i + 6); String result = nodeValue.substring(i + 6, j); return result; } catch (StringIndexOutOfBoundsException e) { return null; }//from ww w . j av a 2s .c o m } return null; } else { NodeList children = node.getChildNodes(); int len = children.getLength(); for (int i = 0; i < len; i++) { String result = findXslHref(children.item(i)); if (result != null) return result; } } return null; }
From source file:Main.java
public static void printNodesAndAttributes(String xmlStr) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try {//from w w w. j a v a 2 s . co m logger.info("Xml processing:"); DocumentBuilder db = dbf.newDocumentBuilder(); InputStream inStream = new ByteArrayInputStream(xmlStr.getBytes(StandardCharsets.UTF_8)); // or InputSource inputSource = new InputSource( new StringReader( // xmlStr ) ); Document doc = db.parse(inStream); DocumentTraversal dt = (DocumentTraversal) doc; NodeIterator i = dt.createNodeIterator(doc, NodeFilter.SHOW_ELEMENT, null, false); Node node = i.nextNode(); while (node != null) { logger.info("Node type: " + node.getNodeType() + " Node name: " + node.getNodeName()); logger.info(" Attributes: " + attributesStr(node)); node = i.nextNode(); } } catch (Exception ex) { logger.error(ex); } }
From source file:Main.java
/** * Remove all children with specified name from the specified node */// ww w . j a v a 2 s . c o m public static void removeChildren(Node node, String name) { Node currentChild = node.getFirstChild(); while (currentChild != null) { final Node nextChild = currentChild.getNextSibling(); if (currentChild.getNodeName().equals(name)) node.removeChild(currentChild); currentChild = nextChild; } }
From source file:Main.java
/** * This method will find all the parameters under this <code>paramsElement</code> and return them as * Map<String, String>. For example, * <pre>//from www. j a v a2 s . c o m * <result ... > * <param name="param1">value1</param> * <param name="param2">value2</param> * <param name="param3">value3</param> * </result> * </pre> * will returns a Map<String, String> with the following key, value pairs :- * <ul> * <li>param1 - value1</li> * <li>param2 - value2</li> * <li>param3 - value3</li> * </ul> * * @param paramsElement * @return */ public static Map getParams(Element paramsElement) { LinkedHashMap params = new LinkedHashMap(); if (paramsElement == null) { return params; } NodeList childNodes = paramsElement.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node childNode = childNodes.item(i); if ((childNode.getNodeType() == Node.ELEMENT_NODE) && "param".equals(childNode.getNodeName())) { Element paramElement = (Element) childNode; String paramName = paramElement.getAttribute("name"); String val = getContent(paramElement); if (val.length() > 0) { params.put(paramName, val); } } } return params; }