当然可以啊~
你遍历所有的item就可以找到了。 NodeList list = root.getElementsByTagName("item");
//遍历item元素
for (int i=0; i < list.getLength() ; i++) {
//获得page的元素
Element element = (Element)list.item(i);
//获得ID属性
NodeList titleid = element.getElementsByTagName("id");
//获得id元素
Element idElement = (Element)titleid.item(0);
//获得id元素的第一个值
String id = idElement.getFirstChild().getNodeValue();
//获得name属性
NodeList titles = element.getElementsByTagName("name");
//获得name元素
Element titleElement = (Element)titles.item(0);
//获得name元素的第一个值
String title = titleElement.getFirstChild().getNodeValue();
System.out.println("ID :" + " " + id + " name:" + " " + name);
}
} catch (Exception e) {
e.printStackTrace();
}
你遍历所有的item就可以找到了。 NodeList list = root.getElementsByTagName("item");
//遍历item元素
for (int i=0; i < list.getLength() ; i++) {
//获得page的元素
Element element = (Element)list.item(i);
//获得ID属性
NodeList titleid = element.getElementsByTagName("id");
//获得id元素
Element idElement = (Element)titleid.item(0);
//获得id元素的第一个值
String id = idElement.getFirstChild().getNodeValue();
//获得name属性
NodeList titles = element.getElementsByTagName("name");
//获得name元素
Element titleElement = (Element)titles.item(0);
//获得name元素的第一个值
String title = titleElement.getFirstChild().getNodeValue();
System.out.println("ID :" + " " + id + " name:" + " " + name);
}
} catch (Exception e) {
e.printStackTrace();
}
解决方案 »
- c:foreach标签输出list的问题
- activemq丢失消息问题
- 关于jsp页面的显示
- 我在学SSH,有什么好的书推荐一下。
- 哪位高人给详解一下Struts2的框架结构和处理流程?
- 数据库连接问题
- 用c#调用java写的webservice问题
- The value of the local variable age is not used提示这个是什么意思
- Flash教程:JBoss Plugin for IntelliJ IDEA Tutorial
- Struts中配置application.properties文件,如果有中文,显示出来的是乱码,你们是怎么解决的呢??!!
- JSP的配置
- hibernate annotations双向一对多关系?
----------------
import java.io.File;
import java.io.IOException;import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.*;
import org.xml.sax.SAXException;public class CsdnQustionOne { public static void main(String[] args) {
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db=dbf.newDocumentBuilder();
Document d=db.parse(new File("csdn1.xml"));
NodeList nl=d.getElementsByTagName("id");
int len=nl.getLength();
for(int i=0;i<len;i++)
{
String a=nl.item(i).getFirstChild().getNodeValue();
if(a.equals("2017"))
{
Node nll=d.getElementsByTagName("name").item(i);
String b=nll.getFirstChild().getNodeValue();
System.out.println(b);
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} }}