List of usage examples for org.w3c.dom Node hasChildNodes
public boolean hasChildNodes();
From source file:Main.java
public static List<String> getValuesFromDocumentByTagAndAttribute(Node parentNode, String tagName, String attributeName, String attributeValue) { ArrayList<String> values = new ArrayList<String>(); for (int i = 0; i < parentNode.getChildNodes().getLength(); i++) { Node childNode = parentNode.getChildNodes().item(i); if (childNode != null) { if (childNode.hasAttributes()) { for (int j = 0; j < childNode.getAttributes().getLength(); j++) { Node attribute = childNode.getAttributes().item(j); if (attribute.getNodeName().equals(attributeName) && attribute.getNodeValue().equals(attributeValue)) { values.add(childNode.getTextContent().trim()); }/* www .jav a2 s . c o m*/ } } if (childNode.hasChildNodes()) { values.addAll(getValuesFromDocumentByTagAndAttribute(childNode, tagName, attributeName, attributeValue)); } } } return values; }
From source file:Main.java
public static String getpathToRoot(final Node nodeIn) { String path = ""; Node parent = null;/* w w w. j av a 2s .c o m*/ int y = 0; if (nodeIn != null) { Node locNode = nodeIn; while (locNode.getParentNode() != null) { parent = locNode.getParentNode(); if (y > 0) { path = ":" + path; } y++; int i = getCurrentPosition(locNode); i = i - 1; // Check to see it has children as otherwise the htmltree has a // problem with expanding leaves. if (nodeIn.hasChildNodes()) { path = i + path; } locNode = parent; } } if (path == null || path.length() == 0) { path = "0"; } return path; }
From source file:DocWriter.java
private static String nodeWithAttrs(Node node, String indent) { StringBuffer sb = new StringBuffer(); // indent bug - leave out //sb.append( indent ); sb.append("<"); sb.append(node.getNodeName());//from w w w. ja va 2 s . co m NamedNodeMap attrs = node.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { sb.append(" "); Node attrNode = attrs.item(i); sb.append(attrNode.getNodeName()); sb.append("=\""); sb.append(attrNode.getNodeValue()); sb.append("\""); } if (!node.hasChildNodes()) { sb.append("/>"); } else { sb.append(">"); } return sb.toString(); }
From source file:Main.java
private static String logXMLSubNode(Node node, int deepth) { int i;/*from w w w.ja v a2s . c o m*/ String nodeStr = new String(); String interStr = new String(); for (i = 0; i < deepth; i++) interStr += "\t"; nodeStr += interStr + "<" + node.getNodeName() + internal; if (node.hasAttributes()) { NamedNodeMap attrs = node.getAttributes(); // add the attrubite name-value pairs for (i = 0; i < attrs.getLength(); i++) { Node a = attrs.item(i); nodeStr += a.getNodeName() + "=" + a.getNodeValue() + internal; } } if (node.hasChildNodes()) { nodeStr += ">\n"; NodeList ns = node.getChildNodes(); for (i = 0; i < ns.getLength(); i++) { nodeStr += logXMLSubNode(ns.item(i), deepth + 1); } nodeStr += interStr + "</" + node.getNodeName() + ">\n"; } else { if (node.getNodeValue() != null) { nodeStr += ">" + node.getNodeValue() + "<" + node.getNodeName(); } nodeStr += "/>\n"; } return nodeStr; }
From source file:Main.java
private static Object getElementValue(Node node) { NodeList nodes = node.getChildNodes(); int childCount = nodes.getLength(); int childElementCount = 0; for (int i = 0; i < childCount; i++) { Node child = nodes.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { childElementCount++;/* w w w .j a va 2 s .c o m*/ } } if (childElementCount == 0) { return node.getTextContent(); } Map<String, Object> map = new LinkedHashMap<>(childElementCount); for (int i = 0; i < childCount; i++) { Node child = nodes.item(i); if (child.getNodeType() != Node.ELEMENT_NODE) { continue; } String childName = child.getNodeName(); Object childValue = child.hasChildNodes() ? toObject(child) : null; // auto detect repeating elements if (map.containsKey(childName)) { Object temp = map.get(childName); if (temp instanceof List) { List list = (List) temp; list.add(childValue); } else { List list = new ArrayList(childCount); map.put(childName, list); list.add(temp); list.add(childValue); } } else { map.put(childName, childValue); } } return map; }
From source file:Main.java
public static boolean compareNode(Node nQ, Node nN, Boolean considerLength, Map<String, Node> qvars) throws Exception { /*System.out.println("current query tree:"); try {/*from w w w.j a va 2s.c o m*/ System.out.println(printDocument(nQ)); }catch (Exception e) { e.printStackTrace(); } System.out.println("current comp tree:"); try { System.out.println(printDocument(nN)); }catch (Exception e) { e.printStackTrace(); }// END OF DEBUG output XML */ if (qvars == null) { throw new Exception("qvars array must not be null"); } if (nQ.hasChildNodes()) { int nQChildLength = nQ.getChildNodes().getLength(); if (nN.hasChildNodes() && (!considerLength || nQChildLength == nN.getChildNodes().getLength())) { //loop through all childnodes for (int i = 0; i < nQChildLength; i++) { //System.out.println("recurse to "+ nQ.getChildNodes().item( i )+"vs"+nN.getChildNodes().item( i )); //DEBUG output XML if (!compareNode(nQ.getChildNodes().item(i), nN.getChildNodes().item(i), considerLength, qvars)) { return false; } } } } //check for qvar descendant, add to qvar hashmap for checking (required for checking multiple qvars) if (nQ.getNodeName().equals("mws:qvar")) { String qvarName = nQ.getAttributes().getNamedItem("name").getNodeValue(); if (qvars.containsKey(qvarName)) { return compareNode(qvars.get(qvarName), nN, considerLength, qvars); } else { qvars.put(qvarName, nN); return true; } } else { //Attributes are ignored; child nodelists are not equal in length and considerlength is false OR reached lowest level: therefore check nodevalue if (nQ.getNodeName().equals(nN.getNodeName())) { try { return nQ.getNodeValue().trim().equals(nN.getNodeValue().trim()); } catch (NullPointerException e) { //NodeValue does not exist return true; } } else { return false; } } }
From source file:Main.java
private static void printNote(NodeList nodeList, int depth) { for (int count = 0; count < nodeList.getLength(); count++) { Node tempNode = nodeList.item(count); if (tempNode.getNodeType() == Node.ELEMENT_NODE) { System.out.println(depth + "Node Name =" + tempNode.getNodeName()); System.out.println(depth + "Node Value =" + tempNode.getTextContent()); if (tempNode.hasAttributes()) { 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 a v a 2s . co m*/ } if (tempNode.hasChildNodes()) { printNote(tempNode.getChildNodes(), depth + 1); } System.out.println(depth + "Node Name =" + tempNode.getNodeName()); } } }
From source file:Main.java
private static ArrayList<SimpleEntry<String, String>> traverseNode(Node n, String p) { ArrayList<SimpleEntry<String, String>> output = new ArrayList<>(); String nName;//from w w w. j ava 2s.c om if (n.getNodeType() != Node.TEXT_NODE) { nName = n.getNodeName(); if (nName.startsWith("m:")) nName = nName.substring(2); if (nName.equals("mws:qvar")) return new ArrayList<>(); p += "/" + nName; } String nValue = n.getNodeValue(); if (nValue != null) { nValue = nValue.trim(); if (nValue.length() == 0) { return new ArrayList<>(); } } else { nValue = ""; } if (!n.hasChildNodes()) { output.add(new SimpleEntry<>(p, nValue)); } else { for (int i = 0; i < n.getChildNodes().getLength(); i++) { output.addAll(traverseNode(n.getChildNodes().item(i), p)); } } return output; }
From source file:ching.icecreaming.action.ResourceDescriptors.java
private static String getTagValue(String tagName1, Element element1) { String tagValue1 = null;/*from w ww .j a v a 2 s . co m*/ Node node1 = null; Element element2 = null; NodeList nodeList1 = element1.getElementsByTagName(tagName1); if (nodeList1 != null && nodeList1.getLength() > 0) { node1 = nodeList1.item(0); if (node1.hasChildNodes()) { if (node1.getNodeType() == Node.ELEMENT_NODE) { element2 = (Element) node1; tagValue1 = element2.getFirstChild().getNodeValue(); } } } return tagValue1; }
From source file:Main.java
/** * Output a DOM node including children using a log4j logger. * If logger is null it will just output to system out. * It will indent by the number of tabs passed in. * This method recursively calls itself to output * children nodes.//w w w .j a v a2 s . c om * * @param logger * @param n * @param tabs */ public static void outputNode(Logger logger, Node n, int tabs) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < tabs; i++) { sb.append("\t"); } sb.append("<" + n.getNodeName()); if (n.hasAttributes()) { NamedNodeMap nnMap = n.getAttributes(); for (int i = 0; i < nnMap.getLength(); i++) { Node att = nnMap.item(i); sb.append(" " + att.getNodeName() + "=\"" + att.getNodeValue() + "\""); } } sb.append(">"); sb = printBuffer(logger, sb, true); for (int i = 0; i < tabs + 1; i++) { sb.append("\t"); } sb.append(n.getNodeValue()); sb = printBuffer(logger, sb, true); if (n.hasChildNodes()) { NodeList nodes = n.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { outputNode(nodes.item(i), tabs + 1); } } for (int i = 0; i < tabs; i++) { sb.append("\t"); } sb.append("</" + n.getNodeName() + ">"); sb = printBuffer(logger, sb, true); }