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:com.googlecode.jgenhtml.JGenHtmlTest.java
/** * Get elements which match the tagName and className specified. * @param document The document which contains the elements we are looking for. * @param tagName The tag name of the elements to match. * @param className The CSS class to match. If null will return all elements that match the tagName alone. * @return A collection of matching elements. *///from www .j a v a 2 s .c om private static List<Element> getElementsByTagAndClass(Document document, String tagName, String className) { NodeList candidates = document.getElementsByTagName(tagName); List<Element> result = new ArrayList<Element>(); for (int i = 0; i < candidates.getLength(); i++) { Element next = (Element) candidates.item(i); if (className == null || className.equals(next.getAttribute("class")))//todo find a space separated list { result.add(next); } } return result; }
From source file:ambit.data.qmrf.QMRFConverter.java
public static ArrayList findNodeIDRef(String name_node, String ref_name, String catalog_name, org.w3c.dom.Document source) throws Exception { source.getDocumentElement();//from w w w .j a v a 2 s . c o m ArrayList return_list = new ArrayList(); NodeList objCatNodes = source.getElementsByTagName(name_node); Node objNode = objCatNodes.item(0); NodeList objNodes = objNode.getChildNodes(); for (int i = 0; i < objNodes.getLength(); i = i + 1) { if (objNodes.item(i).getNodeName().equals(ref_name)) { NamedNodeMap objAttributes = objNodes.item(i).getAttributes(); Node attribute = objAttributes.getNamedItem("idref"); //attribute.getNodeValue() NodeList objNodesAuthor = source.getElementsByTagName(catalog_name); for (int j = 0; j < objNodesAuthor.getLength(); j = j + 1) { String id = objNodesAuthor.item(j).getAttributes().getNamedItem("id").getNodeValue(); if (id.equals(attribute.getNodeValue())) { //Node name = objNodesAuthor.item(j).getAttributes().getNamedItem(xml_attribute_name); return_list.add(objNodesAuthor.item(j)); } } } } return return_list; }
From source file:org.apache.lens.regression.util.Util.java
public static void changeConfig(HashMap<String, String> map, String remotePath) throws Exception { Path p = Paths.get(remotePath); String fileName = p.getFileName().toString(); backupFile = localFilePath + "backup-" + fileName; localFile = localFilePath + fileName; log.info("Copying " + remotePath + " to " + localFile); remoteFile("get", remotePath, localFile); Files.copy(new File(localFile).toPath(), new File(backupFile).toPath(), REPLACE_EXISTING); DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = docBuilder.parse(new FileInputStream(localFile)); doc.normalize();/*from w w w . java 2s . co m*/ NodeList rootNodes = doc.getElementsByTagName("configuration"); Node root = rootNodes.item(0); Element rootElement = (Element) root; NodeList property = rootElement.getElementsByTagName("property"); for (int i = 0; i < property.getLength(); i++) { //Deleting redundant properties from the document Node prop = property.item(i); Element propElement = (Element) prop; Node propChild = propElement.getElementsByTagName("name").item(0); Element nameElement = (Element) propChild; if (map.containsKey(nameElement.getTextContent())) { rootElement.removeChild(prop); i--; } } Iterator<Entry<String, String>> ab = map.entrySet().iterator(); while (ab.hasNext()) { Entry<String, String> entry = ab.next(); String propertyName = entry.getKey(); String propertyValue = entry.getValue(); System.out.println(propertyName + " " + propertyValue + "\n"); Node newNode = doc.createElement("property"); rootElement.appendChild(newNode); Node newName = doc.createElement("name"); Element newNodeElement = (Element) newNode; newName.setTextContent(propertyName); newNodeElement.appendChild(newName); Node newValue = doc.createElement("value"); newValue.setTextContent(propertyValue); newNodeElement.appendChild(newValue); } prettyPrint(doc); remoteFile("put", remotePath, localFile); }
From source file:com.wavemaker.tools.pws.install.PwsInstall.java
private static Document insertEntryKey(Document doc, File[] runtimeJarFiles, File[] toolsJarFiles, String partnerName) throws IOException { NodeList beans_list = doc.getElementsByTagName("beans"); Node beans_node = beans_list.item(0); // First delete old entries NodeList beansChildren = beans_node.getChildNodes(); Node bean_node = null, prop_node = null, map_node = null; for (int i = 0; i < beansChildren.getLength(); i++) { if (!beansChildren.item(i).getNodeName().equals("bean")) { continue; }/*from w ww . ja va 2s . c o m*/ bean_node = beansChildren.item(i); NodeList beanChildren = bean_node.getChildNodes(); for (int j = 0; j < beanChildren.getLength(); j++) { if (!beanChildren.item(j).getNodeName().equals("property")) { continue; } prop_node = beanChildren.item(j); break; } if (prop_node == null) { continue; } NodeList propChildren = prop_node.getChildNodes(); for (int k = 0; k < propChildren.getLength(); k++) { if (!propChildren.item(k).getNodeName().equals("map")) { continue; } map_node = propChildren.item(k); break; } if (map_node == null) { continue; } NodeList mapChildren = map_node.getChildNodes(); List<Node> oldEntryList = new ArrayList<Node>(); for (int l = 0; l < mapChildren.getLength(); l++) { if (!mapChildren.item(l).getNodeName().equals("entry")) { continue; } Node target = mapChildren.item(l); NamedNodeMap entry_attributes = target.getAttributes(); for (int m = 0; m < entry_attributes.getLength(); m++) { Node entryAttr = entry_attributes.item(m); if (entryAttr.getNodeName().equals("key") && entryAttr.getNodeValue().equals(partnerName)) { oldEntryList.add(target); break; } } } if (oldEntryList.size() > 0) { for (Node oldEntry : oldEntryList) { map_node.removeChild(oldEntry); } } // Now, add new entries NamedNodeMap bean_attributes = bean_node.getAttributes(); for (int m = 0; m < bean_attributes.getLength(); m++) { Node beanAttr = bean_attributes.item(m); if (beanAttr.getNodeName().equals("id") && beanAttr.getNodeValue().equals("pwsLoginManagerBeanFactory")) { if (classExistsInJar(runtimeJarFiles, partnerName, LOGIN_MANAGER) || classExistsInJar(toolsJarFiles, partnerName, LOGIN_MANAGER)) { Element newEntry = doc.createElement("entry"); newEntry.setAttribute("key", partnerName); newEntry.setAttribute("value-ref", partnerName + LOGIN_MANAGER); map_node.appendChild(newEntry); } break; } else if (beanAttr.getNodeName().equals("id") && beanAttr.getNodeValue().equals("pwsRestImporterBeanFactory")) { if (classExistsInJar(runtimeJarFiles, partnerName, REST_IMPORTER) || classExistsInJar(toolsJarFiles, partnerName, REST_IMPORTER)) { Element newEntry = doc.createElement("entry"); newEntry.setAttribute("key", partnerName); newEntry.setAttribute("value-ref", partnerName + REST_IMPORTER); map_node.appendChild(newEntry); } break; } else if (beanAttr.getNodeName().equals("id") && beanAttr.getNodeValue().equals("pwsRestWsdlGeneratorBeanFactory")) { if (classExistsInJar(runtimeJarFiles, partnerName, REST_WSDL_GENERATOR) || classExistsInJar(toolsJarFiles, partnerName, REST_WSDL_GENERATOR)) { Element newEntry = doc.createElement("entry"); newEntry.setAttribute("key", partnerName); newEntry.setAttribute("value-ref", partnerName + REST_WSDL_GENERATOR); map_node.appendChild(newEntry); } break; } else if (beanAttr.getNodeName().equals("id") && beanAttr.getNodeValue().equals("pwsServiceModifierBeanFactory")) { if (classExistsInJar(runtimeJarFiles, partnerName, SERVICE_MODIFIER) || classExistsInJar(toolsJarFiles, partnerName, SERVICE_MODIFIER)) { Element newEntry = doc.createElement("entry"); newEntry.setAttribute("key", partnerName); newEntry.setAttribute("value-ref", partnerName + SERVICE_MODIFIER); map_node.appendChild(newEntry); } break; } else if (beanAttr.getNodeName().equals("id") && beanAttr.getNodeValue().equals("pwsRestServiceGeneratorBeanFactory")) { if (classExistsInJar(runtimeJarFiles, partnerName, REST_SERVICE_IMPORTER) || classExistsInJar(toolsJarFiles, partnerName, REST_SERVICE_IMPORTER)) { Element newEntry = doc.createElement("entry"); newEntry.setAttribute("key", partnerName); newEntry.setAttribute("value-ref", partnerName + REST_SERVICE_IMPORTER); map_node.appendChild(newEntry); } break; } else if (beanAttr.getNodeName().equals("id") && beanAttr.getNodeValue().equals("pwsResponseProcessorBeanFactory")) { if (classExistsInJar(runtimeJarFiles, partnerName, RESPONSE_PROCESSOR) || classExistsInJar(toolsJarFiles, partnerName, RESPONSE_PROCESSOR)) { Element newEntry = doc.createElement("entry"); newEntry.setAttribute("key", partnerName); newEntry.setAttribute("value-ref", partnerName + RESPONSE_PROCESSOR); map_node.appendChild(newEntry); } break; } } } return doc; }
From source file:eu.europeana.uim.sugarcrmclient.internal.helpers.ClientUtils.java
/** * @param responseString/*from w ww .jav a2 s .c o m*/ * @return */ public static String extractSimpleResponse(String responseString) { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); DocumentBuilder documentBuilder; try { documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse(new InputSource(new StringReader(responseString))); String sessionID = null; NodeList nl = document.getElementsByTagName("id"); if (nl.getLength() > 0) { sessionID = nl.item(0).getTextContent(); } return sessionID; } catch (ParserConfigurationException e) { logger.error(e.getMessage()); } catch (SAXException e) { logger.error(e.getMessage()); } catch (IOException e) { logger.error(e.getMessage()); } return null; }
From source file:org.aectann.postage.TrackingStatusRefreshTask.java
public static TrackingInfo syncRequest(Context context, String tracking) throws FactoryConfigurationError { TrackingInfo result = null;/* ww w. ja v a 2 s .co m*/ if (tracking != null && tracking.length() > 0) { tracking = tracking.toUpperCase(); HttpClient client = new DefaultHttpClient(); try { HttpResponse response = client.execute(new HttpGet("http://prishlo.li/" + tracking + ".xml")); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { InputStream content = response.getEntity().getContent(); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse(content); String weight = getFirstVaueOrNull(document, "weight"); String from = getFirstVaueOrNull(document, "from"); String kind = getFirstVaueOrNull(document, "kind"); TrackingInfo old = TrackingStorageUtils.loadStoredTrackingInfo(tracking, context); result = new TrackingInfo(old != null ? old.getName() : null, tracking, weight, kind, from); NodeList checkpoints = document.getElementsByTagName("checkpoint"); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); for (int i = 0; i < checkpoints.getLength(); i++) { Node current = checkpoints.item(i); NamedNodeMap attributes = current.getAttributes(); Node state = attributes.getNamedItem("state"); Node attribute = attributes.getNamedItem("attribute"); Node date = attributes.getNamedItem("date"); String dateString = date.getNodeValue(); String attributeString = attribute.getNodeValue(); String stateString = state.getNodeValue(); String locationString = current.getFirstChild().getNodeValue(); result.addStatus(new TrackingStatus(stateString, dateFormat.parse(dateString), attributeString, locationString)); } } } catch (Exception e) { if (result == null) { result = new TrackingInfo(null, tracking, null, null, null); } } } return result; }
From source file:com.vmware.qe.framework.datadriven.utils.XMLUtil.java
/** * Returns the text value of a node where a sibling tag name and value is known. * <p>/*from w w w. j av a 2 s . c om*/ * For example, in the XML snippet below, if you pass in "cat" as the parentTagName, "name" as * the childTagName, and "MrBiggles" as the childTagValue, and specify "color" as the * siblingTagName, the method would return "Grey". * <p> * Example XML: * * <pre> * <cat> * <name>MrBiggles</name> * <color>Grey</color> * </cat> * <cat> * <name>Boris</name> * <color>Black</color> * </cat> * </pre> * * @param xmlDocAsString The XML document as a String * @param parentTagName The tag name of the parent elements that should contain the sibling * nodes * @param childTagName The tag name of the known child element * @param childTagValue The known text value of the child element * @param siblingTagName The tag name of the sibling node * @return String The text value of the sibling node * @throws Exception */ public static String findSiblingNodeValue(String xmlDocAsString, String parentTagName, String childTagName, String childTagValue, String siblingTagName) throws Exception { String siblingNodeValue = null; Document xmlDoc = XMLUtil.getXmlDocumentElement(xmlDocAsString); NodeList parentNodes = xmlDoc.getElementsByTagName(parentTagName); if (parentNodes != null) { for (int i = 0; i < parentNodes.getLength(); i++) { Node parentNode = parentNodes.item(i); HashMap<String, Node> childNodes = XMLUtil.getChildrenByTagNames(parentNode); Node childNode = childNodes.get(childTagName); String tmpChildNodeVal = XMLUtil.getNodeTextValue(childNode); if (childTagValue.equals(tmpChildNodeVal)) { log.info("Found node: " + childTagName + " with value: " + childTagValue); Node siblingNode = childNodes.get(siblingTagName); if (siblingNode != null) { siblingNodeValue = XMLUtil.getNodeTextValue(siblingNode); log.info("Found the sibling node: " + siblingTagName + " = " + siblingNodeValue); break; } else { log.warn("No sibling node named: " + siblingTagName + " was found."); } } } } else { log.warn("There were no elements with tag name: " + parentTagName); } return siblingNodeValue; }
From source file:com.vmware.qe.framework.datadriven.utils.XMLUtil.java
public static Vector<String> getValueOnTag(Document doc, String elementName) { Vector<String> elementNames = new Vector<String>(); NodeList list = doc.getElementsByTagName(elementName); log.info("XML Elements: "); for (int i = 0; i < list.getLength(); i++) { Element element = (Element) list.item(i); elementNames.add(element.getAttribute("name")); log.info("Element Name : " + element.getLocalName() + " Value :" + element.getAttribute("name")); }/*from w ww . j a va 2 s .c om*/ return elementNames; }
From source file:net.codjo.dataprocess.server.treatmenthelper.TreatmentHelper.java
public static void insertRepositoryContent(Connection con, int repositoryId, String content) throws SQLException, TreatmentException, TransformerException { Document doc; try {//from w w w . ja va2 s .c o m doc = XMLUtils.parse(content); } catch (Exception ex) { throw new TreatmentException(ex); } PreparedStatement pStmt = con.prepareStatement( "insert into PM_REPOSITORY_CONTENT (REPOSITORY_CONTENT_ID, REPOSITORY_ID, TREATMENT_ID, CONTENT) values (?, ?, ?, ?)"); try { NodeList nodes = doc.getElementsByTagName(DataProcessConstants.TREATMENT_ENTITY_XML); int nbNodes = nodes.getLength(); for (int i = 0; i < nbNodes; i++) { Node node = nodes.item(i); String treatmentId = node.getAttributes().getNamedItem("id").getNodeValue(); if (treatmentId.length() > 50) { throw new TreatmentException("La taille de l'identifiant d'un traitement ('" + treatmentId + "') dpasse 50 caractres."); } String contentNode = XMLUtils.nodeToString(node); pStmt.setInt(1, SQLUtil.getNextId(con, "PM_REPOSITORY_CONTENT", "REPOSITORY_CONTENT_ID")); pStmt.setInt(2, repositoryId); pStmt.setString(3, treatmentId); pStmt.setString(4, contentNode); pStmt.executeUpdate(); } } finally { pStmt.close(); } }
From source file:com.omertron.thetvdbapi.tools.TvdbParser.java
/** * Get all the episodes from the URL//from w ww .j ava 2 s .c o m * * @param urlString * @param season * @param bannerMirror * @return */ public static List<Episode> getAllEpisodes(String urlString, int season, String bannerMirror) { List<Episode> episodeList = new ArrayList<Episode>(); Episode episode; NodeList nlEpisode; Node nEpisode; Element eEpisode; try { Document doc = DOMHelper.getEventDocFromUrl(urlString); nlEpisode = doc.getElementsByTagName(EPISODE); for (int loop = 0; loop < nlEpisode.getLength(); loop++) { nEpisode = nlEpisode.item(loop); if (nEpisode.getNodeType() == Node.ELEMENT_NODE) { eEpisode = (Element) nEpisode; episode = parseNextEpisode(eEpisode, bannerMirror); if ((episode != null) && (season == -1 || episode.getSeasonNumber() == season)) { // Add the episode only if the season is -1 (all seasons) or matches the season episodeList.add(episode); } } } } catch (WebServiceException ex) { LOG.warn("All Episodes error: " + ex.getMessage(), ex); } return episodeList; }