List of usage examples for org.w3c.dom Document getElementsByTagName
public NodeList getElementsByTagName(String tagname);
NodeList
of all the Elements
in document order with a given tag name and are contained in the document. From source file:Main.java
public static void setValueByElementName(Document document, String name, String value) { if (document == null) { return;//from w w w. j a va 2 s .co m } NodeList nodeList = document.getElementsByTagName(name); if (nodeList != null && nodeList.getLength() > 0) { for (int i = 0; i < nodeList.getLength(); i++) { Element element = (Element) nodeList.item(i); ((Text) element.getFirstChild()).setData(value); } } }
From source file:Main.java
public static Node[] findNodesByTagName(Document document, String tagName) { ArrayList<Node> nodes = new ArrayList<Node>(); NodeList foundNodes = document.getElementsByTagName(tagName); for (int i = 0; i < foundNodes.getLength(); i++) nodes.add(foundNodes.item(i));/*from w w w . j a va2 s . co m*/ Node[] returnNodes = new Node[nodes.size()]; return nodes.toArray(returnNodes); }
From source file:Main.java
public static String getAttributeInXML(org.w3c.dom.Document doc, String tagName, String attributeName) { String output = ""; org.w3c.dom.NodeList ontologyTypes = doc.getElementsByTagName(tagName); output = ((Element) ontologyTypes.item(0)).getAttribute(attributeName); return output; }
From source file:Main.java
/** * Get the Value of a node that has an embedded text node as a child * @param doc the Document to search for the node name * @param nodeName the name of the node to get the value * @return the value of the text node child *//*from w w w .j av a 2 s .c om*/ public static String getNodeTextValue(Document doc, String nodeName) { String value = null; NodeList nodes = doc.getElementsByTagName(nodeName); if (nodes.getLength() > 0) { Node node = nodes.item(0); if (node.hasChildNodes()) { Node text = node.getFirstChild(); value = text.getNodeValue(); } } return replaceSpecialCharacters(value); }
From source file:Main.java
/** * Returns an array of strings representing all values for provided * attribute prefixed by 2 levels of parents, all nodes (e.g.: * parenetNodeAtrValue.childNodeAtrValue.childNodeAtrValue) * @param xsdDoc/*www. j a va2 s . c o m*/ * @param atrName * @return */ public static ArrayList<String> getAllValuesWithParentForAttribute(Document xsdDoc, String atrName) { try { NodeList allNodes = xsdDoc.getElementsByTagName("*"); return getValuesWithParentForAttribute(atrName, allNodes); } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:Main.java
/** * Searches below the supplied Node for a "list" tag and then retrieves the contents of the "value" tag(s) under that. * Note that if there is more than one "list" under the node then we will take the values from the first one only. * /* w w w. j a va2 s .c o m*/ * @param node * acts as the root for the seach * * @return list of Strings corresponding to the contents of the "value" tag(s) */ public static ArrayList getElementListValues(Node node) { ArrayList values = new ArrayList(); // search for list tag Document doc = node.getOwnerDocument(); NodeList list = doc.getElementsByTagName("list"); if (list.getLength() == 0) return values; // search under that for value tag(s) doc = list.item(0).getOwnerDocument(); NodeList vals = doc.getElementsByTagName("value"); // for each one we get the text contents for (int i = 0; i < vals.getLength(); i++) { Node v = vals.item(i); NodeList text = v.getChildNodes(); if (text == null) { values.add(""); continue; } // there should be only text inside the value tag Node value = text.item(0); if (value == null) values.add(""); else values.add(value.getNodeValue()); } return values; }
From source file:Main.java
public static boolean validProcess(String xml) { try {/*from www.j a v a2 s . c o m*/ Set<String> classifiers = new HashSet<String>(); InputStream is = new ByteArrayInputStream(xml.getBytes()); DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document document = docBuilder.parse(is); // iterate over all operators NodeList nodes = document.getElementsByTagName("operator"); for (int i = 0; i < nodes.getLength(); i++) { Element element = (Element) nodes.item(i); String className = element.getAttribute("class"); if (classifiers.contains(className)) { return false; } classifiers.add(className); } return true; } catch (Exception e) { return false; } }
From source file:Main.java
public static Object getClassFromXML() throws Exception { //create DOM object DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document document = docBuilder.parse(new File("property.xml")); //get the node name and get the class name NodeList nodeList = document.getElementsByTagName("className"); Node node = nodeList.item(0).getFirstChild(); String className = node.getNodeValue(); //return an object through the class name Class tempClass = Class.forName(className); Object object = tempClass.newInstance(); return object; }
From source file:Main.java
/** * Find an element/*from w w w . j av a 2 s.c o m*/ * @param doc XML document * @param tagName Tag name * @param props Properties corresponding to attributes in tag * @return Element or null if not found */ public static Element findElement(Document doc, String tagName, Properties props) { Element elmt = null; NodeList nlist = doc.getElementsByTagName(tagName); for (int i = 0; i < nlist.getLength() && elmt == null; i++) { Node node = nlist.item(i); if (node instanceof Element) { Element temp = (Element) node; boolean matches = true; for (Enumeration en = props.keys(); en.hasMoreElements() && matches;) { String name = (String) en.nextElement(); String value = props.getProperty(name); String attValue = temp.getAttribute(name); if (attValue == null) matches = false; else if (!value.equals(attValue)) matches = false; } if (matches) elmt = temp; } } return elmt; }
From source file:Main.java
/** * //from w w w.j ava 2 s .co m * @param doc * @param strTagName * @return */ public static ArrayList<String> getArrayListFromNodes(Document doc, String strTagName) { ArrayList<String> alValues = new ArrayList<String>(); NodeList nl = doc.getElementsByTagName(strTagName); // if there are tags if (nl != null) { // cycles on all of them for (int i = 0; i < nl.getLength(); i++) { // if it is a field Node if (nl.item(i).getNodeType() == 1) { // gets the i-th element Element e = (Element) nl.item(i); // if it has a value adds it to the AL if (e.getFirstChild() != null) alValues.add(e.getFirstChild().getNodeValue()); } } } return alValues; }