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 addHandset(String file, String name, Hashtable<String, String> attri) throws Exception { DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance(); DocumentBuilder dombuilder = domfac.newDocumentBuilder(); FileInputStream is = new FileInputStream(file); Document doc = dombuilder.parse(is); NodeList nodeList = doc.getElementsByTagName("devices"); if (nodeList != null && nodeList.getLength() >= 1) { Node deviceNode = nodeList.item(0); Element device = doc.createElement("device"); device.setTextContent(name);/*from ww w.j a v a 2 s.c o m*/ for (Iterator itrName = attri.keySet().iterator(); itrName.hasNext();) { String attriKey = (String) itrName.next(); String attriValue = (String) attri.get(attriKey); device.setAttribute(attriKey, attriValue); } deviceNode.appendChild(device); } //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
@SuppressWarnings("finally") public static String getType(String relativePath, String nodeName) { try {//from ww w.ja va2s. c o m DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dFactory.newDocumentBuilder(); Document doc; doc = builder.parse(new File(path + relativePath)); NodeList nl = doc.getElementsByTagName(nodeName); Node classNode = nl.item(0).getFirstChild(); nodeString = classNode.getNodeValue().trim(); } catch (Exception e) { e.printStackTrace(); } finally { return nodeString; } }
From source file:Main.java
public static ArrayList<String> getArrayListTextValuesByDocument(Document document, String tag) { ArrayList<String> result = new ArrayList<String>(); NodeList nodeList = document.getElementsByTagName(tag); int length = nodeList.getLength(); for (int i = 0; i < length; i++) result.add(nodeList.item(i).getTextContent()); return result; }
From source file:Main.java
public static List<Element> getElementCollectionFromDocument(Document document, String tagName) { List<Element> res = new LinkedList<Element>(); NodeList entries = document.getElementsByTagName(tagName); for (int i = 0; i < entries.getLength(); i++) { Node entry = entries.item(i); if (entry.getNodeType() == Node.ELEMENT_NODE) { res.add((Element) entry); }// w ww. ja va2 s . c o m } return res; }
From source file:Main.java
public static void addApp(String file, String name, Hashtable<String, String> attri) throws Exception { DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance(); DocumentBuilder dombuilder = domfac.newDocumentBuilder(); FileInputStream is = new FileInputStream(file); Document doc = dombuilder.parse(is); NodeList nodeList = doc.getElementsByTagName("app"); if (nodeList != null && nodeList.getLength() >= 1) { Node deviceNode = nodeList.item(0); Element device = doc.createElement("aut"); device.setTextContent(name);//w w w .j ava 2s . c o m for (Iterator itrName = attri.keySet().iterator(); itrName.hasNext();) { String attriKey = (String) itrName.next(); String attriValue = (String) attri.get(attriKey); device.setAttribute(attriKey, attriValue); } deviceNode.appendChild(device); } //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
/** * Method to delete an element based on tag name . * @param doc//from w ww. j a va2s. c o m * @param tagName * @return */ private static Document deleteElement(Document doc, String tagName) { //only 2 of 3 were deleted ? NodeList nodes = doc.getElementsByTagName(tagName); for (int i = 0; i < nodes.getLength(); i++) { Element e = (Element) nodes.item(i); e.getParentNode().removeChild(e); } return doc; }
From source file:Main.java
public static Object getBean(String relativePath, String nodeName) { try {/*from w w w . j a v a2s .c om*/ DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dFactory.newDocumentBuilder(); Document doc; doc = builder.parse(new File(path + relativePath)); NodeList nl = doc.getElementsByTagName(nodeName); Node classNode = nl.item(0).getFirstChild(); String className = classNode.getNodeValue().trim(); Class clazz = Class.forName(className); Object obj = clazz.newInstance(); return obj; } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static void setIdentityAttribute(Document document, String attributeValue) { boolean isFound = false; NodeList propertyList = document.getElementsByTagName("Property"); for (int i = 0; i < propertyList.getLength(); i++) { Node node = propertyList.item(i); for (int j = 0; j < node.getAttributes().getLength(); j++) { Node attribute = node.getAttributes().item(j); if (attribute.getNodeName().equals("name") && attribute.getNodeValue().equals(attributeValue)) { Element element = (Element) node; element.setAttribute("isIdentity", "true"); isFound = true;//w w w .j av a 2 s. c om break; } } if (isFound) break; } }
From source file:Main.java
public static Object getBean() { try {//from w ww. j a v a2 s . c om DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dFactory.newDocumentBuilder(); Document document; document = builder.parse(new File("config.xml")); NodeList nl = document.getElementsByTagName("StrategyClassName"); Node classNode = nl.item(0).getFirstChild(); String cName = classNode.getNodeValue(); System.out.println(cName); Class c = Class.forName("com.seven.strategy.sort." + cName); Object object = c.newInstance(); return object; } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static void updateUserMgtXML(File userMgtXML, String jndiConfigNameUserDB) throws Exception { Document doc = initializeXML(userMgtXML); NodeList configNodeList = doc.getElementsByTagName("Configuration"); Node configNode = configNodeList.item(0); //get the 0 index, since only one available NodeList nodeList = configNode.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { if (node.getNodeName().equalsIgnoreCase("Property")) { if (node.hasAttributes()) { NamedNodeMap namedNodeMap = node.getAttributes(); Node attr = namedNodeMap.getNamedItem("name"); if (attr != null && attr.getNodeValue().equalsIgnoreCase("dataSource")) { node.setTextContent(jndiConfigNameUserDB); }/*w w w .ja v a 2s . co m*/ } } } } finalizeXML(userMgtXML, doc, 4); }