List of usage examples for org.w3c.dom Node getFirstChild
public Node getFirstChild();
From source file:Main.java
public static String getSubTagValue(Element root, String tagName, String subTagName) { String returnString = ""; NodeList list = root.getElementsByTagName(tagName); for (int loop = 0; loop < list.getLength(); loop++) { Node node = list.item(loop); if (node != null) { NodeList children = node.getChildNodes(); for (int innerLoop = 0; innerLoop < children.getLength(); innerLoop++) { Node child = children.item(innerLoop); if ((child != null) && (child.getNodeName() != null) && child.getNodeName().equals(subTagName)) { Node grandChild = child.getFirstChild(); if (grandChild.getNodeValue() != null) return grandChild.getNodeValue(); }//from ww w.ja v a2s. c o m } // end inner loop } } return returnString; }
From source file:Main.java
public static String getSubTagValue(Element root, String tagName, String subTagName) { String returnString = ""; NodeList list = root.getElementsByTagName(tagName); for (int loop = 0; loop < list.getLength(); loop++) { Node node = list.item(loop); if (node != null) { NodeList children = node.getChildNodes(); for (int innerLoop = 0; innerLoop < children.getLength(); innerLoop++) { Node child = children.item(innerLoop); if ((child != null) && (child.getNodeName() != null) && (child.getNodeName().equals(subTagName))) { Node grandChild = child.getFirstChild(); if (grandChild.getNodeValue() != null) { return grandChild.getNodeValue(); }/*w w w. j av a 2 s. c o m*/ } } } } return returnString; }
From source file:DomUtil.java
/** * Get the first child's content ( ie it's included TEXT node ). *///from w w w .j ava 2 s .c o m public static String getChildContent(Node parent, String name) { Node first = parent.getFirstChild(); if (first == null) return null; for (Node node = first; node != null; node = node.getNextSibling()) { // System.out.println("getNode: " + name + " " + node.getNodeName()); if (name.equals(node.getNodeName())) { return getContent(node); } } return null; }
From source file:Main.java
public static List<Map<String, String>> readXMLFile(String outFile) { DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance(); List<Map<String, String>> returnlist = new ArrayList<Map<String, String>>(); try {//from w w w. j a v a 2 s. com DocumentBuilder dombuilder = domfac.newDocumentBuilder(); InputStream is = new FileInputStream(outFile); Document doc = dombuilder.parse(is); NodeList nl = doc.getElementsByTagName("row"); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); NodeList fileds = node.getChildNodes(); Map<String, String> map = new HashMap<String, String>(); for (int j = 0; j < fileds.getLength(); j++) { Node filed = fileds.item(j); if (filed.getNodeType() == Node.ELEMENT_NODE) { map.put(filed.getAttributes().getNamedItem("name").getNodeValue(), filed.getFirstChild().getNodeValue()); } } returnlist.add(map); } } catch (Exception e) { e.printStackTrace(); } return returnlist; }
From source file:Main.java
/** * Returns the concatenated child text of the specified node. * This method only looks at the immediate children of type * Node.TEXT_NODE or the children of any child * node that is of type Node.CDATA_SECTION_NODE * for the concatenation./* www .j a v a2 s . c om*/ * * @param node The node to look at. */ public static String getChildText(Node node) { // is there anything to do? if (node == null) { return null; } // concatenate children text StringBuffer str = new StringBuffer(); Node child = node.getFirstChild(); while (child != null) { short type = child.getNodeType(); if (type == Node.TEXT_NODE) { str.append(child.getNodeValue()); } else if (type == Node.CDATA_SECTION_NODE) { str.append(getChildText(child)); } child = child.getNextSibling(); } // return text value return str.toString(); }
From source file:Main.java
/** * @param colorNode// w w w . j a v a 2 s . c om * @return the hex color */ public static String toHexColor(Node colorNode) { String value = "#000000"; if (colorNode == null) return value; try {// FIXME no good to grab 1st child and then node val if (colorNode.getFirstChild() == null) return value; String nodeVal = colorNode.getFirstChild().getNodeValue(); String[] components = nodeVal.split(", "); StringBuffer sb = new StringBuffer(100); sb.append("#"); for (int i = 0; i < components.length - 1; i++) { String uglyHack = Integer.toHexString(Integer.parseInt(components[i])); uglyHack = uglyHack.length() == 1 ? "0" + uglyHack : uglyHack; sb.append(uglyHack); } value = sb.toString(); } catch (Exception e) { e.printStackTrace(); } return value; }
From source file:Main.java
public static String getNodeValue(Node node) { if (node == null) { return null; } else if (node instanceof Text) { return node.getNodeValue().trim(); } else if (node instanceof Element) { node.normalize();//from w w w . j a va 2s . c o m Node temp = node.getFirstChild(); if (temp != null && (temp instanceof Text)) return temp.getNodeValue().trim(); else return ""; } else { return node.getNodeValue().trim(); } }
From source file:DOMUtil.java
/** * Find the first text descendent node of an element. * This recursively looks more than one level to search * for text in font nodes, etc./*from ww w . j a v a 2 s . c om*/ * * @param node The starting node for the search. * @return The text node or null if not found. */ public static Text findFirstText(Node node) { if (node instanceof Text) return (Text) node; for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { Text text = findFirstText(child); if (text != null) return text; } return null; }
From source file:Main.java
public static final String getValueOf(Node node) { if (node == null) { return null; } else if (node instanceof Text) { return node.getNodeValue().trim(); } else if (node instanceof Element) { ((Element) node).normalize(); Node temp = node.getFirstChild(); if (temp != null && (temp instanceof Text)) return temp.getNodeValue().trim(); else/*from w ww . j a v a 2 s . c om*/ return ""; } else { return node.getNodeValue().trim(); } }
From source file:Main.java
/** * Returns all nodes at the bottom of path from node. * If element begins with '@', indicates an attribute, eg "@id" * The '#text' element indicates that the node has a single text child. * @param node Node to apply path to// ww w .ja va2 s . c o m * @param path Path to apply * @return All Nodes at bottom of path. List may be empty, but not null. */ static public List<Node> extractNodes(Node node, String path) { if (node == null) return new ArrayList<Node>(); List<Node> result = new ArrayList<Node>(); NodeList list = node.getChildNodes(); if (path.equals("#text")) result.add(node.getFirstChild()); else if (path.charAt(0) == '@') result.add(node.getAttributes().getNamedItem(path.substring(1))); else for (int j = 0; j < list.getLength(); j++) if (list.item(j).getNodeType() == Node.ELEMENT_NODE && list.item(j).getNodeName().equals(path)) result.add(list.item(j)); return result; }