当前位置: 技术问答>java相关
解析xml文件。
来源: 互联网 发布时间:2015-10-01
本文导语: //import javax.xml.parsers.*; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.*; import java.io.*; import org.xml.sax.*; /** *使用DOM提取XML内容的例子 */ public class BookParser{ public static void...
//import javax.xml.parsers.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.*;
import java.io.*;
import org.xml.sax.*;
/**
*使用DOM提取XML内容的例子
*/
public class BookParser{
public static void main(String[] args){
try{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document document=builder.parse(new File("books.xml"));
document.normalize();
Element root=document.getDocumentElement();
System.out.println("根原始的名称:"+root.getTagName());
NodeList books=root.getElementsByTagName("book");
System.out.println("书本列表");
for(int i=0;i
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.*;
import java.io.*;
import org.xml.sax.*;
/**
*使用DOM提取XML内容的例子
*/
public class BookParser{
public static void main(String[] args){
try{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document document=builder.parse(new File("books.xml"));
document.normalize();
Element root=document.getDocumentElement();
System.out.println("根原始的名称:"+root.getTagName());
NodeList books=root.getElementsByTagName("book");
System.out.println("书本列表");
for(int i=0;i