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
/** * Method to change update date in xml files - used for reloadable issue and auto change on start up. * /*w ww. ja v a 2s . co m*/ * @param target document * @return updated document * @throws ParseException */ private static Document changeDate(Document target) throws ParseException { 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 newT = new Date(); String time = format.format(newT); parent.setAttribute("time", time); return target; }
From source file:Main.java
public static void removeHandset(String file, String name) throws Exception { DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance(); DocumentBuilder dombuilder = domfac.newDocumentBuilder(); FileInputStream is = new FileInputStream(file); Document doc = dombuilder.parse(is); NodeList devices = doc.getElementsByTagName("devices"); NodeList nodeList = doc.getElementsByTagName("device"); for (int i = 0; i < nodeList.getLength(); i++) { Node deviceNode = nodeList.item(i); if (deviceNode.getTextContent().equals(name)) { devices.item(0).removeChild(deviceNode); }// ww w .j a v a 2 s . co m } //save TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); Properties props = t.getOutputProperties(); props.setProperty(OutputKeys.ENCODING, "GB2312"); t.setOutputProperties(props); DOMSource dom = new DOMSource(doc); StreamResult sr = new StreamResult(file); t.transform(dom, sr); }
From source file:Main.java
/** * Add XML IDs to a Document. This method is one of the hearts of the xseq * system. It runs through a DOM Document tree, and for each element found * adds (updates) an ID attribute called "id". * /*from w ww . java 2 s. co m*/ * <P> * For now the value is a simple sequence. * * @param doc * The DOM Document to which IDs are added */ public static void addIDs(Document doc) { // * means all elements NodeList nodelist = doc.getElementsByTagName("*"); // does this need to be thread protected? for (int i = 0; i < nodelist.getLength(); i++) { Element element = (Element) nodelist.item(i); element.setAttribute("id", "n" + i); } }
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 www .ja v a 2s.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
public static String getContent(Document doc, String tagName) { //http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/ return doc.getElementsByTagName(tagName).item(0).getTextContent(); }
From source file:Main.java
public static String getDefaultNamespaceUri(URL xmlResource, String rootElementName) throws IOException, ParserConfigurationException, SAXException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false);/* w w w . j av a 2 s . co m*/ DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(xmlResource.openStream()); NodeList nodes = document.getElementsByTagName(rootElementName); if (nodes == null || nodes.getLength() == 0) { throw new IllegalArgumentException("Root element \"" + rootElementName + "\" not found in xml \"" + xmlResource.toExternalForm() + "\"."); } for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); Node xmlns = node.getAttributes().getNamedItem("xmlns"); if (xmlns != null) { String value = xmlns.getNodeValue(); return value.substring(value.indexOf("=") + 1); } } return null; }
From source file:Main.java
public static Object getBean(String filePath) { try {//from ww w .j a v a 2 s .co m DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dFactory.newDocumentBuilder(); Document doc; doc = builder.parse(new File(filePath)); NodeList nl = doc.getElementsByTagName("className"); Node classNode = nl.item(0).getFirstChild(); String cName = classNode.getNodeValue(); System.out.println(cName); Class c = Class.forName(cName); Object obj = c.newInstance(); return obj; } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:hoot.services.controllers.osm.ChangesetUploadXmlValidator.java
/** * Determines whether a changeset DOM has any elements in it. (Strangely, I * couldn't figure out how to do this correctly with XPath). * //ww w .j ava2 s . c o m * @param changesetDiffDoc * the changeset to check for elements * @return true if the changeset has elements; false otherwise */ private static boolean changesetHasElements(Document changesetDiffDoc) { return (changesetDiffDoc.getElementsByTagName("node").getLength() + changesetDiffDoc.getElementsByTagName("way").getLength() + changesetDiffDoc.getElementsByTagName("relation").getLength()) > 0; }
From source file:Main.java
/** * Returns a list of all the tags in an XML document * /*ww w .java2 s . com*/ * @param doc * @return list of all tags */ public static List<String> getAllTags(Document doc) { List<String> tags = new ArrayList<String>(); NodeList ns = doc.getElementsByTagName("*"); for (int i = 0; i < ns.getLength(); i++) { Element element = (Element) ns.item(i); tags.add(element.getTagName()); } return tags; }
From source file:Main.java
public static ArrayList<Element> getElements(String elementName, Document doc) { ArrayList<Element> result = new ArrayList<Element>(); NodeList list = doc.getElementsByTagName(elementName); for (int i = 0; i < list.getLength(); i++) { Node nNode = list.item(i); if (nNode.getNodeType() == Node.ELEMENT_NODE) { result.add((Element) nNode); }/*w ww. java 2 s . c om*/ } return result; }