List of usage examples for org.w3c.dom NodeList item
public Node item(int index);
index
th item in the collection. From source file:Main.java
public static String getElementValue(String elementName, Element element) throws Exception { String value = null;/*from w ww. j a va 2 s . com*/ NodeList childNodes = element.getElementsByTagName(elementName); Element cn = (Element) childNodes.item(0); value = cn.getFirstChild().getNodeValue().trim(); //value = childNodes.item(0).getNodeValue().trim(); if (value == null) { throw new Exception("No element found with given name:" + elementName); } return value; }
From source file:Main.java
/** * Returns the text value of the specified node. The returned value is the node value of the first child of <code>node</code> which type * is <code>Document.TEXT_NODE</code>. * /*from ww w . j av a2 s. c o m*/ * @param node * the node which text value has to be retrieved. * @return the text value of the node. */ public static String getTextValue(Node node) { NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { if (list.item(i).getNodeType() == Document.TEXT_NODE) { return list.item(i).getNodeValue(); } } return null; }
From source file:Main.java
private static Node getSingleNodeElementByXpath(String xpath) throws Exception { NodeList list = getNodeListByXpath(xpath); Node node = null;/*from w ww . ja v a 2s . com*/ if (list.getLength() > 0 && list.item(0).getNodeType() == Node.ELEMENT_NODE) { node = list.item(0); } else { throw new Exception("Xpath Query did not result in a Node element. Check your Xpath expression"); } return node; }
From source file:Main.java
public static String getChildAsString(Element e, String child) { NodeList nodes = e.getElementsByTagName(child); if (nodes.getLength() > 0) { Element i = (Element) nodes.item(0); return i.getTextContent().trim(); } else//ww w.j a v a 2 s .c o m return null; }
From source file:Main.java
public static String getChildTagValue(Node node, String tag) { Element element = (Element) node; NodeList childrenWithTag = element.getElementsByTagName(tag).item(0).getChildNodes(); Node valueNode = (Node) childrenWithTag.item(0); return valueNode.getNodeValue(); }
From source file:Main.java
/** * Get the value of the first tag found with the given name in the given document<br/>. * * @param doc/*from w ww .j a v a 2s . co m*/ * @param tagName * @return the value of the node or the empty string, if no node with this name exits in this document */ public static String getTagValue(Document doc, String tagName) { NodeList tagList = doc.getElementsByTagName(tagName); if (tagList.getLength() > 0) { return tagList.item(0).getFirstChild().getNodeValue().trim(); } return ""; }
From source file:Main.java
public static Element getElement(Element root, String name) throws Exception { NodeList elements = root.getElementsByTagName(name); if (elements.getLength() > 0) { return (Element) elements.item(0); }//www .j a v a 2 s . c o m throw new Exception("Child element '" + name + "' to parent " + root.getNodeName() + " not found."); }
From source file:Main.java
public static String getFirstElementText(Document parent, String tag) { NodeList nl = parent.getElementsByTagName(tag); if (nl.getLength() > 0) { return ((Element) nl.item(0)).getTextContent(); }/*from w w w .j av a 2s. c o m*/ return null; }
From source file:Main.java
public static String getFirstElementText(Element parent, String tag) { NodeList nl = parent.getElementsByTagName(tag); if (nl.getLength() > 0) { return ((Element) nl.item(0)).getTextContent(); }// w w w. j ava 2 s . c o m return null; }
From source file:Main.java
/** * Sets the text for a node/* w ww. j a va 2s. com*/ */ public synchronized static void setText(Node n, String text) { if (n == null) throw new IllegalArgumentException("Node argument cannot be null"); if (text == null) throw new IllegalArgumentException("Node text argument cannot be null"); NodeList nl = n.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { if (nl.item(i).getNodeType() == Node.TEXT_NODE) { nl.item(i).setNodeValue(text); return; } } Node textNode = n.getOwnerDocument().createTextNode(text); n.appendChild(textNode); }