org.w3c.dom.Element.getElementsByTagName()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(11.7k)|赞(0)|评价(0)|浏览(419)

本文整理了Java中org.w3c.dom.Element.getElementsByTagName()方法的一些代码示例,展示了Element.getElementsByTagName()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Element.getElementsByTagName()方法的具体详情如下:
包路径:org.w3c.dom.Element
类名称:Element
方法名:getElementsByTagName

Element.getElementsByTagName介绍

[英]Returns a NodeList of all descendant Elements with a given tag name, in document order.
[中]按文档顺序返回具有给定标记名的所有子体ElementsNodeList

代码示例

代码示例来源:origin: stackoverflow.com

  1. private String getTextValue(String def, Element doc, String tag) {
  2. String value = def;
  3. NodeList nl;
  4. nl = doc.getElementsByTagName(tag);
  5. if (nl.getLength() > 0 && nl.item(0).hasChildNodes()) {
  6. value = nl.item(0).getFirstChild().getNodeValue();
  7. }
  8. return value;
  9. }

代码示例来源:origin: libgdx/libgdx

  1. private void addToDependencyMapFromXML (Map<String, List<ExternalExtensionDependency>> dependencies, Element eElement, String platform) {
  2. if (eElement.getElementsByTagName(platform).item(0) != null) {
  3. Element project = (Element)eElement.getElementsByTagName(platform).item(0);
  4. ArrayList<ExternalExtensionDependency> deps = new ArrayList<ExternalExtensionDependency>();
  5. if (project.getTextContent().trim().equals("")) {
  6. // No dependencies required
  7. } else if (project.getTextContent().trim().equals("null")) {
  8. // Not supported
  9. deps = null;
  10. } else {
  11. NodeList nList = project.getElementsByTagName("dependency");
  12. for (int i = 0; i < nList.getLength(); i++) {
  13. Node nNode = nList.item(i);
  14. if (nNode.getNodeType() == Node.ELEMENT_NODE) {
  15. Element dependencyNode = (Element)nNode;
  16. boolean external = Boolean.parseBoolean(dependencyNode.getAttribute("external"));
  17. deps.add(new ExternalExtensionDependency(dependencyNode.getTextContent(), external));
  18. }
  19. }
  20. }
  21. dependencies.put(platform, deps);
  22. }
  23. }

代码示例来源:origin: stackoverflow.com

  1. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  2. DocumentBuilder db = dbf.newDocumentBuilder();
  3. Document dom = db.parse("file.xml");
  4. Element docEle = dom.getDocumentElement();
  5. NodeList nl = docEle.getChildNodes();
  6. if (nl != null) {
  7. int length = nl.getLength();
  8. for (int i = 0; i < length; i++) {
  9. if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
  10. Element el = (Element) nl.item(i);
  11. if (el.getNodeName().contains("staff")) {
  12. String name = el.getElementsByTagName("name").item(0).getTextContent();
  13. String phone = el.getElementsByTagName("phone").item(0).getTextContent();
  14. String email = el.getElementsByTagName("email").item(0).getTextContent();
  15. String area = el.getElementsByTagName("area").item(0).getTextContent();
  16. String city = el.getElementsByTagName("city").item(0).getTextContent();
  17. }
  18. }
  19. }
  20. }

代码示例来源:origin: marytts/marytts

  1. /**
  2. * @param xmlDescription
  3. * xmlDescription
  4. * @throws NullPointerException
  5. * NullPointerException
  6. * @throws MalformedURLException
  7. * MalformedURLException
  8. */
  9. public VoiceComponentDescription(Element xmlDescription) throws NullPointerException, MalformedURLException {
  10. super(xmlDescription);
  11. this.gender = xmlDescription.getAttribute("gender");
  12. this.type = xmlDescription.getAttribute("type");
  13. Element dependsElement = (Element) xmlDescription.getElementsByTagName("depends").item(0);
  14. this.dependsLanguage = dependsElement.getAttribute("language");
  15. this.dependsVersion = dependsElement.getAttribute("version");
  16. }

代码示例来源:origin: aragozin/jvm-tools

  1. public void generate(Writer output) throws IOException {
  2. Document doc = (Document) template.cloneNode(true);
  3. transformHead((Element) doc.getDocumentElement().getElementsByTagName("head").item(0));
  4. transformBody((Element) doc.getDocumentElement().getElementsByTagName("body").item(0));
  5. encodeDocument(doc, output);
  6. }

代码示例来源:origin: aws/aws-sdk-java

  1. private static String getChildElementValue(
  2. final String tagName,
  3. final Element element) {
  4. Node tagNode = element.getElementsByTagName(tagName).item(0);
  5. if ( tagNode == null )
  6. return null;
  7. NodeList nodes= tagNode.getChildNodes();
  8. Node node = (Node)nodes.item(0);
  9. return node.getNodeValue();
  10. }

代码示例来源:origin: AsyncHttpClient/async-http-client

  1. private void parse(Document document) {
  2. Element element = document.getDocumentElement();
  3. NodeList statusNode = element.getElementsByTagName("status");
  4. for (int i = 0; i < statusNode.getLength(); i++) {
  5. Node node = statusNode.item(i);
  6. String value = node.getFirstChild().getNodeValue();
  7. int statusCode = Integer.valueOf(value.substring(value.indexOf(" "), value.lastIndexOf(" ")).trim());
  8. String statusText = value.substring(value.lastIndexOf(" "));
  9. status = new HttpStatusWrapper(status, statusText, statusCode);
  10. }
  11. }

代码示例来源:origin: alibaba/cobar

  1. private static Element findPropertyByName(Element bean, String name) {
  2. NodeList propertyList = bean.getElementsByTagName("property");
  3. for (int j = 0, m = propertyList.getLength(); j < m; ++j) {
  4. Node node = propertyList.item(j);
  5. if (node instanceof Element) {
  6. Element p = (Element) node;
  7. if (name.equals(p.getAttribute("name"))) {
  8. return p;
  9. }
  10. }
  11. }
  12. return null;
  13. }

代码示例来源:origin: stackoverflow.com

  1. protected String getString(String tagName, Element element) {
  2. NodeList list = element.getElementsByTagName(tagName);
  3. if (list != null && list.getLength() > 0) {
  4. NodeList subList = list.item(0).getChildNodes();
  5. if (subList != null && subList.getLength() > 0) {
  6. return subList.item(0).getNodeValue();
  7. }
  8. }
  9. return null;
  10. }

代码示例来源:origin: marytts/marytts

  1. /**
  2. * @param xmlDescription
  3. * xmlDescription
  4. * @throws NullPointerException
  5. * NullPointerException
  6. * @throws MalformedURLException
  7. * MalformedURLException
  8. */
  9. public VoiceComponentDescription(Element xmlDescription) throws NullPointerException, MalformedURLException {
  10. super(xmlDescription);
  11. this.gender = xmlDescription.getAttribute("gender");
  12. this.type = xmlDescription.getAttribute("type");
  13. Element dependsElement = (Element) xmlDescription.getElementsByTagName("depends").item(0);
  14. this.dependsLanguage = dependsElement.getAttribute("language");
  15. this.dependsVersion = dependsElement.getAttribute("version");
  16. }

代码示例来源:origin: chanjarster/weixin-java-tools

  1. static String extractEncryptPart(String xml) {
  2. try {
  3. DocumentBuilder db = builderLocal.get();
  4. Document document = db.parse(new InputSource(new StringReader(xml)));
  5. Element root = document.getDocumentElement();
  6. return root.getElementsByTagName("Encrypt").item(0).getTextContent();
  7. } catch (Exception e) {
  8. throw new RuntimeException(e);
  9. }
  10. }

代码示例来源:origin: stanfordnlp/CoreNLP

  1. /**
  2. * Reconstructs the resource from the XML file
  3. */
  4. @SuppressWarnings("unchecked")
  5. public SsurgeonWordlist(Element rootElt) {
  6. id = rootElt.getAttribute("id");
  7. NodeList wordEltNL = rootElt.getElementsByTagName(WORD_ELT);
  8. for (int i=0; i<wordEltNL.getLength(); i++) {
  9. Node node = wordEltNL.item(i);
  10. if (node.getNodeType() == Node.ELEMENT_NODE) {
  11. String word = Ssurgeon.getEltText((Element) node);
  12. words.add(word);
  13. }
  14. }
  15. }

代码示例来源:origin: org.apache.hadoop/hadoop-common

  1. static String readFirstTagValue(Element e, String tag) {
  2. NodeList nodes = e.getElementsByTagName(tag);
  3. return (nodes.getLength() == 0)? null : nodes.item(0).getTextContent();
  4. }

代码示例来源:origin: spotbugs/spotbugs

  1. @ExpectWarning("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE")
  2. public String getFirstChildName(Element e) {
  3. NodeList list = e.getElementsByTagName("child");
  4. if(list != null) {
  5. return ((Element)list.item(0)).getAttribute("name");
  6. }
  7. return null;
  8. }
  9. }

代码示例来源:origin: stanfordnlp/CoreNLP

  1. public static List<Person> readXMLCharacterList(Document doc) {
  2. List<Person> personList = new ArrayList<>();
  3. NodeList characters = doc.getDocumentElement().getElementsByTagName("characters").item(0).getChildNodes();
  4. for(int i = 0; i < characters.getLength(); i++)
  5. {
  6. Node child = characters.item(i);
  7. if(child.getNodeName().equals("character")) {
  8. String name = child.getAttributes().getNamedItem("name").getNodeValue();
  9. char[] cName = name.toCharArray();
  10. cName[0] = Character.toUpperCase(cName[0]);
  11. name = new String(cName);
  12. List<String> aliases = Arrays.asList(child.getAttributes().getNamedItem("aliases").getNodeValue().split(";"));
  13. String gender = (child.getAttributes().getNamedItem("gender") == null) ? "" : child.getAttributes().getNamedItem("gender").getNodeValue();
  14. personList.add(new Person(child.getAttributes().getNamedItem("name").getNodeValue(), gender, aliases));
  15. }
  16. }
  17. return personList;
  18. }
  19. //write the character list to a file to work with the annotator

代码示例来源:origin: geoserver/geoserver

  1. /**
  2. * Reads "style" elements from the catalog.xml file.
  3. *
  4. * <p>For each style element read, an entry of <id,filename> is created in a map.
  5. *
  6. * @return A map containing style <id,filename> tuples.
  7. * @throws Exception If error processing "styles" element.
  8. */
  9. public Map<String, String> styles() throws Exception {
  10. Element stylesElement = ReaderUtils.getChildElement(catalog, "styles", true);
  11. NodeList styleElements = stylesElement.getElementsByTagName("style");
  12. Map styles = new HashMap();
  13. for (int i = 0; i < styleElements.getLength(); i++) {
  14. Element styleElement = (Element) styleElements.item(i);
  15. styles.put(styleElement.getAttribute("id"), styleElement.getAttribute("filename"));
  16. }
  17. return styles;
  18. }

代码示例来源:origin: pentaho/pentaho-kettle

  1. public String getSample( String strFunctionName, String strFunctionNameWithArgs ) {
  2. String sRC = "// Sorry, no Script available for " + strFunctionNameWithArgs;
  3. NodeList nl = dom.getElementsByTagName( "jsFunction" );
  4. for ( int i = 0; i < nl.getLength(); i++ ) {
  5. if ( nl.item( i ).getAttributes().getNamedItem( "name" ).getNodeValue().equals( strFunctionName ) ) {
  6. Node elSample = ( (Element) nl.item( i ) ).getElementsByTagName( "sample" ).item( 0 );
  7. if ( elSample.hasChildNodes() ) {
  8. return ( elSample.getFirstChild().getNodeValue() );
  9. }
  10. }
  11. }
  12. return sRC;
  13. }

代码示例来源:origin: libgdx/libgdx

  1. private void initData () throws ParserConfigurationException, IOException, SAXException {
  2. DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
  3. DocumentBuilder builder = dbFactory.newDocumentBuilder();
  4. Document doc = builder.parse(ExternalExtensionsDialog.class
  5. .getResourceAsStream("/com/badlogic/gdx/setup/data/extensions.xml"));
  6. doc.getDocumentElement().normalize();
  7. for (int i = 0; i < nList.getLength(); i++) {
  8. Node nNode = nList.item(i);
  9. if (nNode.getNodeType() == Node.ELEMENT_NODE) {
  10. String name = eElement.getElementsByTagName("name").item(0).getTextContent();
  11. String description = eElement.getElementsByTagName("description").item(0).getTextContent();
  12. String version = eElement.getElementsByTagName("version").item(0).getTextContent();
  13. String compatibility = eElement.getElementsByTagName("compatibility").item(0).getTextContent();
  14. String url = eElement.getElementsByTagName("website").item(0).getTextContent();
  15. NodeList inheritsNode = eElement.getElementsByTagName("inherit");
  16. gwtInherits = new String[inheritsNode.getLength()];
  17. for (int j = 0; j < inheritsNode.getLength(); j++)
  18. gwtInherits[j] = inheritsNode.item(j).getTextContent();

代码示例来源:origin: alibaba/cobar

  1. public static Map<String, Object> loadElements(Element parent) {
  2. Map<String, Object> map = new HashMap<String, Object>();
  3. NodeList children = parent.getChildNodes();
  4. for (int i = 0; i < children.getLength(); i++) {
  5. Node node = children.item(i);
  6. if (node instanceof Element) {
  7. Element e = (Element) node;
  8. String name = e.getNodeName();
  9. if ("property".equals(name)) {
  10. String key = e.getAttribute("name");
  11. NodeList nl = e.getElementsByTagName("bean");
  12. if (nl.getLength() == 0) {
  13. String value = e.getTextContent();
  14. map.put(key, StringUtil.isEmpty(value) ? null : value.trim());
  15. } else {
  16. map.put(key, loadBean((Element) nl.item(0)));
  17. }
  18. }
  19. }
  20. }
  21. return map;
  22. }

代码示例来源:origin: pentaho/pentaho-kettle

  1. public String getSample( String strFunctionName, String strFunctionNameWithArgs ) {
  2. String sRC = "// Sorry, no Script available for " + strFunctionNameWithArgs;
  3. NodeList nl = dom.getElementsByTagName( "jsFunction" );
  4. for ( int i = 0; i < nl.getLength(); i++ ) {
  5. if ( nl.item( i ).getAttributes().getNamedItem( "name" ).getNodeValue().equals( strFunctionName ) ) {
  6. Node elSample = ( (Element) nl.item( i ) ).getElementsByTagName( "sample" ).item( 0 );
  7. if ( elSample.hasChildNodes() ) {
  8. return ( elSample.getFirstChild().getNodeValue() );
  9. }
  10. }
  11. }
  12. return sRC;
  13. }

相关文章