package MyXML;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 DOMStudentsInfo { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
System.out.println(dbf.getClass().getName());
try {
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File("students.xml"));
NodeList nl = doc.getElementsByTagName("studnet");
int len = nl.getLength(); System.out.println(len); //输出的len的值怎么会是0啊??? for (int i = 0; i <len; i++) {
//System.out.println("into for");
Element eltStu = (Element)nl.item(i);
Node eltName = eltStu.getElementsByTagName("name").item(0);
Node eltAge = eltStu.getElementsByTagName("age").item(0); String name = eltName.getFirstChild().getNodeValue();
String age = eltAge.getFirstChild().getNodeValue(); System.out.println("This name is " + name);
System.out.println("This age is " + age);
System.out.println(".................");
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}xml文档如下:
<?xml version="1.0" encoding="gb2312"?>
<?xml-stylesheet type= "text/xsl" href = "students.xsl"?>
<studnets>
<student sn="01">
<name>张三</name>
<age>21</age>
</student>
<student sn="02">
<name>李四</name>
<age>22</age>
</student>
<student sn="03">
<name>王五</name>
<age>22</age>
</student>
</studnets>
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 DOMStudentsInfo { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
System.out.println(dbf.getClass().getName());
try {
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File("students.xml"));
NodeList nl = doc.getElementsByTagName("studnet");
int len = nl.getLength(); System.out.println(len); //输出的len的值怎么会是0啊??? for (int i = 0; i <len; i++) {
//System.out.println("into for");
Element eltStu = (Element)nl.item(i);
Node eltName = eltStu.getElementsByTagName("name").item(0);
Node eltAge = eltStu.getElementsByTagName("age").item(0); String name = eltName.getFirstChild().getNodeValue();
String age = eltAge.getFirstChild().getNodeValue(); System.out.println("This name is " + name);
System.out.println("This age is " + age);
System.out.println(".................");
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}xml文档如下:
<?xml version="1.0" encoding="gb2312"?>
<?xml-stylesheet type= "text/xsl" href = "students.xsl"?>
<studnets>
<student sn="01">
<name>张三</name>
<age>21</age>
</student>
<student sn="02">
<name>李四</name>
<age>22</age>
</student>
<student sn="03">
<name>王五</name>
<age>22</age>
</student>
</studnets>
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货