本文整理了Java中org.w3c.dom.Element.getElementsByTagName()
方法的一些代码示例,展示了Element.getElementsByTagName()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Element.getElementsByTagName()
方法的具体详情如下:
包路径:org.w3c.dom.Element
类名称:Element
方法名:getElementsByTagName
[英]Returns a NodeList
of all descendant Elements
with a given tag name, in document order.
[中]按文档顺序返回具有给定标记名的所有子体Elements
的NodeList
。
代码示例来源:origin: stackoverflow.com
private String getTextValue(String def, Element doc, String tag) {
String value = def;
NodeList nl;
nl = doc.getElementsByTagName(tag);
if (nl.getLength() > 0 && nl.item(0).hasChildNodes()) {
value = nl.item(0).getFirstChild().getNodeValue();
}
return value;
}
代码示例来源:origin: libgdx/libgdx
private void addToDependencyMapFromXML (Map<String, List<ExternalExtensionDependency>> dependencies, Element eElement, String platform) {
if (eElement.getElementsByTagName(platform).item(0) != null) {
Element project = (Element)eElement.getElementsByTagName(platform).item(0);
ArrayList<ExternalExtensionDependency> deps = new ArrayList<ExternalExtensionDependency>();
if (project.getTextContent().trim().equals("")) {
// No dependencies required
} else if (project.getTextContent().trim().equals("null")) {
// Not supported
deps = null;
} else {
NodeList nList = project.getElementsByTagName("dependency");
for (int i = 0; i < nList.getLength(); i++) {
Node nNode = nList.item(i);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element dependencyNode = (Element)nNode;
boolean external = Boolean.parseBoolean(dependencyNode.getAttribute("external"));
deps.add(new ExternalExtensionDependency(dependencyNode.getTextContent(), external));
}
}
}
dependencies.put(platform, deps);
}
}
代码示例来源:origin: stackoverflow.com
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document dom = db.parse("file.xml");
Element docEle = dom.getDocumentElement();
NodeList nl = docEle.getChildNodes();
if (nl != null) {
int length = nl.getLength();
for (int i = 0; i < length; i++) {
if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
Element el = (Element) nl.item(i);
if (el.getNodeName().contains("staff")) {
String name = el.getElementsByTagName("name").item(0).getTextContent();
String phone = el.getElementsByTagName("phone").item(0).getTextContent();
String email = el.getElementsByTagName("email").item(0).getTextContent();
String area = el.getElementsByTagName("area").item(0).getTextContent();
String city = el.getElementsByTagName("city").item(0).getTextContent();
}
}
}
}
代码示例来源:origin: marytts/marytts
/**
* @param xmlDescription
* xmlDescription
* @throws NullPointerException
* NullPointerException
* @throws MalformedURLException
* MalformedURLException
*/
public VoiceComponentDescription(Element xmlDescription) throws NullPointerException, MalformedURLException {
super(xmlDescription);
this.gender = xmlDescription.getAttribute("gender");
this.type = xmlDescription.getAttribute("type");
Element dependsElement = (Element) xmlDescription.getElementsByTagName("depends").item(0);
this.dependsLanguage = dependsElement.getAttribute("language");
this.dependsVersion = dependsElement.getAttribute("version");
}
代码示例来源:origin: aragozin/jvm-tools
public void generate(Writer output) throws IOException {
Document doc = (Document) template.cloneNode(true);
transformHead((Element) doc.getDocumentElement().getElementsByTagName("head").item(0));
transformBody((Element) doc.getDocumentElement().getElementsByTagName("body").item(0));
encodeDocument(doc, output);
}
代码示例来源:origin: aws/aws-sdk-java
private static String getChildElementValue(
final String tagName,
final Element element) {
Node tagNode = element.getElementsByTagName(tagName).item(0);
if ( tagNode == null )
return null;
NodeList nodes= tagNode.getChildNodes();
Node node = (Node)nodes.item(0);
return node.getNodeValue();
}
代码示例来源:origin: AsyncHttpClient/async-http-client
private void parse(Document document) {
Element element = document.getDocumentElement();
NodeList statusNode = element.getElementsByTagName("status");
for (int i = 0; i < statusNode.getLength(); i++) {
Node node = statusNode.item(i);
String value = node.getFirstChild().getNodeValue();
int statusCode = Integer.valueOf(value.substring(value.indexOf(" "), value.lastIndexOf(" ")).trim());
String statusText = value.substring(value.lastIndexOf(" "));
status = new HttpStatusWrapper(status, statusText, statusCode);
}
}
代码示例来源:origin: alibaba/cobar
private static Element findPropertyByName(Element bean, String name) {
NodeList propertyList = bean.getElementsByTagName("property");
for (int j = 0, m = propertyList.getLength(); j < m; ++j) {
Node node = propertyList.item(j);
if (node instanceof Element) {
Element p = (Element) node;
if (name.equals(p.getAttribute("name"))) {
return p;
}
}
}
return null;
}
代码示例来源:origin: stackoverflow.com
protected String getString(String tagName, Element element) {
NodeList list = element.getElementsByTagName(tagName);
if (list != null && list.getLength() > 0) {
NodeList subList = list.item(0).getChildNodes();
if (subList != null && subList.getLength() > 0) {
return subList.item(0).getNodeValue();
}
}
return null;
}
代码示例来源:origin: marytts/marytts
/**
* @param xmlDescription
* xmlDescription
* @throws NullPointerException
* NullPointerException
* @throws MalformedURLException
* MalformedURLException
*/
public VoiceComponentDescription(Element xmlDescription) throws NullPointerException, MalformedURLException {
super(xmlDescription);
this.gender = xmlDescription.getAttribute("gender");
this.type = xmlDescription.getAttribute("type");
Element dependsElement = (Element) xmlDescription.getElementsByTagName("depends").item(0);
this.dependsLanguage = dependsElement.getAttribute("language");
this.dependsVersion = dependsElement.getAttribute("version");
}
代码示例来源:origin: chanjarster/weixin-java-tools
static String extractEncryptPart(String xml) {
try {
DocumentBuilder db = builderLocal.get();
Document document = db.parse(new InputSource(new StringReader(xml)));
Element root = document.getDocumentElement();
return root.getElementsByTagName("Encrypt").item(0).getTextContent();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
代码示例来源:origin: stanfordnlp/CoreNLP
/**
* Reconstructs the resource from the XML file
*/
@SuppressWarnings("unchecked")
public SsurgeonWordlist(Element rootElt) {
id = rootElt.getAttribute("id");
NodeList wordEltNL = rootElt.getElementsByTagName(WORD_ELT);
for (int i=0; i<wordEltNL.getLength(); i++) {
Node node = wordEltNL.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
String word = Ssurgeon.getEltText((Element) node);
words.add(word);
}
}
}
代码示例来源:origin: org.apache.hadoop/hadoop-common
static String readFirstTagValue(Element e, String tag) {
NodeList nodes = e.getElementsByTagName(tag);
return (nodes.getLength() == 0)? null : nodes.item(0).getTextContent();
}
代码示例来源:origin: spotbugs/spotbugs
@ExpectWarning("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE")
public String getFirstChildName(Element e) {
NodeList list = e.getElementsByTagName("child");
if(list != null) {
return ((Element)list.item(0)).getAttribute("name");
}
return null;
}
}
代码示例来源:origin: stanfordnlp/CoreNLP
public static List<Person> readXMLCharacterList(Document doc) {
List<Person> personList = new ArrayList<>();
NodeList characters = doc.getDocumentElement().getElementsByTagName("characters").item(0).getChildNodes();
for(int i = 0; i < characters.getLength(); i++)
{
Node child = characters.item(i);
if(child.getNodeName().equals("character")) {
String name = child.getAttributes().getNamedItem("name").getNodeValue();
char[] cName = name.toCharArray();
cName[0] = Character.toUpperCase(cName[0]);
name = new String(cName);
List<String> aliases = Arrays.asList(child.getAttributes().getNamedItem("aliases").getNodeValue().split(";"));
String gender = (child.getAttributes().getNamedItem("gender") == null) ? "" : child.getAttributes().getNamedItem("gender").getNodeValue();
personList.add(new Person(child.getAttributes().getNamedItem("name").getNodeValue(), gender, aliases));
}
}
return personList;
}
//write the character list to a file to work with the annotator
代码示例来源:origin: geoserver/geoserver
/**
* Reads "style" elements from the catalog.xml file.
*
* <p>For each style element read, an entry of <id,filename> is created in a map.
*
* @return A map containing style <id,filename> tuples.
* @throws Exception If error processing "styles" element.
*/
public Map<String, String> styles() throws Exception {
Element stylesElement = ReaderUtils.getChildElement(catalog, "styles", true);
NodeList styleElements = stylesElement.getElementsByTagName("style");
Map styles = new HashMap();
for (int i = 0; i < styleElements.getLength(); i++) {
Element styleElement = (Element) styleElements.item(i);
styles.put(styleElement.getAttribute("id"), styleElement.getAttribute("filename"));
}
return styles;
}
代码示例来源:origin: pentaho/pentaho-kettle
public String getSample( String strFunctionName, String strFunctionNameWithArgs ) {
String sRC = "// Sorry, no Script available for " + strFunctionNameWithArgs;
NodeList nl = dom.getElementsByTagName( "jsFunction" );
for ( int i = 0; i < nl.getLength(); i++ ) {
if ( nl.item( i ).getAttributes().getNamedItem( "name" ).getNodeValue().equals( strFunctionName ) ) {
Node elSample = ( (Element) nl.item( i ) ).getElementsByTagName( "sample" ).item( 0 );
if ( elSample.hasChildNodes() ) {
return ( elSample.getFirstChild().getNodeValue() );
}
}
}
return sRC;
}
代码示例来源:origin: libgdx/libgdx
private void initData () throws ParserConfigurationException, IOException, SAXException {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbFactory.newDocumentBuilder();
Document doc = builder.parse(ExternalExtensionsDialog.class
.getResourceAsStream("/com/badlogic/gdx/setup/data/extensions.xml"));
doc.getDocumentElement().normalize();
for (int i = 0; i < nList.getLength(); i++) {
Node nNode = nList.item(i);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
String name = eElement.getElementsByTagName("name").item(0).getTextContent();
String description = eElement.getElementsByTagName("description").item(0).getTextContent();
String version = eElement.getElementsByTagName("version").item(0).getTextContent();
String compatibility = eElement.getElementsByTagName("compatibility").item(0).getTextContent();
String url = eElement.getElementsByTagName("website").item(0).getTextContent();
NodeList inheritsNode = eElement.getElementsByTagName("inherit");
gwtInherits = new String[inheritsNode.getLength()];
for (int j = 0; j < inheritsNode.getLength(); j++)
gwtInherits[j] = inheritsNode.item(j).getTextContent();
代码示例来源:origin: alibaba/cobar
public static Map<String, Object> loadElements(Element parent) {
Map<String, Object> map = new HashMap<String, Object>();
NodeList children = parent.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node node = children.item(i);
if (node instanceof Element) {
Element e = (Element) node;
String name = e.getNodeName();
if ("property".equals(name)) {
String key = e.getAttribute("name");
NodeList nl = e.getElementsByTagName("bean");
if (nl.getLength() == 0) {
String value = e.getTextContent();
map.put(key, StringUtil.isEmpty(value) ? null : value.trim());
} else {
map.put(key, loadBean((Element) nl.item(0)));
}
}
}
}
return map;
}
代码示例来源:origin: pentaho/pentaho-kettle
public String getSample( String strFunctionName, String strFunctionNameWithArgs ) {
String sRC = "// Sorry, no Script available for " + strFunctionNameWithArgs;
NodeList nl = dom.getElementsByTagName( "jsFunction" );
for ( int i = 0; i < nl.getLength(); i++ ) {
if ( nl.item( i ).getAttributes().getNamedItem( "name" ).getNodeValue().equals( strFunctionName ) ) {
Node elSample = ( (Element) nl.item( i ) ).getElementsByTagName( "sample" ).item( 0 );
if ( elSample.hasChildNodes() ) {
return ( elSample.getFirstChild().getNodeValue() );
}
}
}
return sRC;
}
内容来源于网络,如有侵权,请联系作者删除!