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
/** * This method returns the first element from the document. * @param document The document.//from w w w . java 2 s . c o m * @param tagName The element name. * @return The element object. */ public static Element getFirstElement(Document document, String tagName) { NodeList nodes = document.getElementsByTagName(tagName); if (nodes != null && nodes.getLength() != 0) { return (Element) nodes.item(0); } else { return null; } }
From source file:Main.java
public static Object getBean() { try {//from w w w . j a v a2 s.co m DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dFactory.newDocumentBuilder(); Document document; document = builder.parse(new File("config.xml")); NodeList nl = document.getElementsByTagName("TemplateClassName"); Node classNode = nl.item(0).getFirstChild(); String cName = classNode.getNodeValue(); System.out.println(cName); Class c = Class.forName("com.seven.templatemethod.one." + cName); Object object = c.newInstance(); return object; } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static NodeList getDomNodes(String file, String xmlTag) { NodeList nodes = null;/*from w ww . j av a2 s. com*/ try { File xml = new File(file); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(xml); doc.getDocumentElement().normalize(); nodes = doc.getElementsByTagName(xmlTag); } catch (Exception e) { e.printStackTrace(); nodes = null; } logger.trace("Returning {} nodes", nodes.getLength()); return nodes; }
From source file:Main.java
/** * Determines if the specified Document (DOM Tree) contains the specified Element * /*from w w w . j av a2 s . c o m*/ * @param document * @param elementName * @return true: if the Document(DOM Tree) contains the Element, false: otherwise */ public static boolean contains(Document document, String elementName) { boolean contains = false; NodeList nodes = document.getElementsByTagName(elementName); if (nodes != null && nodes.getLength() > 0) { contains = true; } return contains; }
From source file:Main.java
/** * Check to see if a node exists in the document with the node name passed in. * //w w w . j av a 2s .c om * @param doc * @param nodeName * @return true if it exists */ public static boolean getNodeExists(Document doc, String nodeName) { if (doc == null || nodeName == null) return false; NodeList nodes = doc.getElementsByTagName(nodeName); if (nodes == null) return false; return nodes.getLength() > 0; }
From source file:Main.java
public static NodeList getXMLNodes(final String xmlFileName, final String tagName) { NodeList nList = null;/*w w w . j a v a2 s.c o m*/ try { File xmlFile = new File(xmlFileName); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(xmlFile); doc.getDocumentElement().normalize(); nList = doc.getElementsByTagName(tagName); } catch (Exception e) { e.printStackTrace(); } return nList; }
From source file:Main.java
/** * Return all values associated with attributeName inside element specified by tag * @param document doc we work with/*from w ww. ja v a 2s.c o m*/ * @param elementTag element containing desired attribute * @param attributeName name of attribute * @return List of values specified in XML array */ public static List<String> getListOfTagAttributeValues(Document document, String elementTag, String attributeName) { NodeList usesPermissionList = document.getElementsByTagName(elementTag); List<String> result = new ArrayList<String>(); for (int i = 0; i < usesPermissionList.getLength(); i++) { Node nNode = usesPermissionList.item(i); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; String value = eElement.getAttribute(attributeName); if (value != null && !value.isEmpty()) { result.add(value); } } } return result; }
From source file:Main.java
/** * Creates an empty node list. Creates an empty document then selects nodes * using a random UUID to ensure an empty result. * * @return an empty Node list// www . ja v a2 s . c o m */ public static NodeList createEmptyNodeList() { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; try { builder = dbf.newDocumentBuilder(); } catch (ParserConfigurationException e) { throw new IllegalStateException("Problem creating document", e); } Document document = builder.newDocument(); assert document != null; final NodeList emptyNodesList = document.getElementsByTagName(UUID.randomUUID().toString()); return emptyNodesList; }
From source file:Main.java
public static Element getFirstElementByTagName(Document document, String tag) { // Get all elements matching the tagname NodeList nodeList = document.getElementsByTagName(tag); if (nodeList == null) { p("no list of elements with tag=" + tag); return null; }/*from www. j a va 2 s .co m*/ // Get the first if any. Element element = (Element) nodeList.item(0); if (element == null) { p("no element for tag=" + tag); return null; } return element; }
From source file:Main.java
/** * Method to check difference between actual time and saved time in xml file. * /* w ww.j a v a2 s . c o m*/ * @param target document * @return time difference as integer * @throws ParseException */ public static int checkDateDifference(Document target) throws ParseException { int diff = 0; NodeList parents = target.getElementsByTagName("g:options"); Element parent = (Element) parents.item(0); //g:options - only 1 element DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.ENGLISH); //date xml String time = parent.getAttribute("time"); Date oldT = format.parse(time); //date now Date newT = new Date(); //compare diff = (int) ((newT.getTime() - oldT.getTime()) / 1000); return diff; }