我用的是javax解析包
可以这么用:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db=factory.newDocumentBuilder();
Document datadom=db.parse(FileName);//filename为xml文件//下面为一些常用的方法
NodeList rowset=datadom.getElementsByTagName(nodename);//nodename要访问的节点名Node row=rowset.item(x);//可以与rowset.getLength()配合使用NodeList cols=row.getChildNodes();//得到子节点列表NamedNodeMap fields=row.getAttributes();
Node fieldname=fields.getNamedItem("name");
fieldname.getNodeValue();
//上面三行获得row的属性值name
可以这么用:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db=factory.newDocumentBuilder();
Document datadom=db.parse(FileName);//filename为xml文件//下面为一些常用的方法
NodeList rowset=datadom.getElementsByTagName(nodename);//nodename要访问的节点名Node row=rowset.item(x);//可以与rowset.getLength()配合使用NodeList cols=row.getChildNodes();//得到子节点列表NamedNodeMap fields=row.getAttributes();
Node fieldname=fields.getNamedItem("name");
fieldname.getNodeValue();
//上面三行获得row的属性值name
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<publish_date>
<year>2000</year>
<month>10</month>
<day>1</day>
</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<publish_date>
<year>2000</year>
<month>12</month>
<day>16</day>
</publish_date>
<description>A former architect battles corporate zombies.</description>
</book>
<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<publish_date>
<year>2000</year>
<month>11</month>
<day>17</day>
</publish_date>
<description>After the collapse of a nanotechnology society in England.</description>
</book>
</catalog>使用DOM解析 --BookParser.javaimport javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;/**
*使用DOM提取XML内容的例子
*/
class BookParser{
public static void main(String[] args){
try{
//获取一个XML解析器
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
//解析XML文件
Document document=builder.parse(new File("books.xml"));
//去掉XML文档中空白部分
document.normalize();
//获取根节点并打印根节点的名称
Element root=document.getDocumentElement();
System.out.println("根原始的名称:"+root.getTagName());
//获取所有的"book"标记,它是一个NodeList对象
NodeList books=root.getElementsByTagName("book");
//遍历NodeList
System.out.println("书本列表");
for(int i=0;i<books.getLength();i++){
//获取books中的每一个元素
Element book=(Element)books.item(i);
//获取每个"book"标记的"id"属性
String book_id=book.getAttribute("id");//或book.getAttributeNode("id").getValue();
System.out.println("序号:"+book_id);
//获取"book"标记下的每个元素
System.out.print("作者:");
System.out.println(book.getElementsByTagName("author").item(0).getFirstChild().getNodeValue());
System.out.print("标题:");
System.out.println(book.getElementsByTagName("title").item(0).getFirstChild().getNodeValue());
//获取"publish_date"元素
Element publishDate=(Element)book.getElementsByTagName("publish_date").item(0);
//获取"publish_date"元素下的每个子元素
String year=publishDate.getElementsByTagName("year").item(0).getFirstChild().getNodeValue();
String month=publishDate.getElementsByTagName("month").item(0).getFirstChild().getNodeValue();
String day=publishDate.getElementsByTagName("day").item(0).getFirstChild().getNodeValue();
System.out.println("出版日期:"+year+"年"+month+"月"+day+"日");
System.out.print("描述:");
System.out.println(book.getElementsByTagName("description").item(0).getFirstChild().getNodeValue());
System.out.println("--------------------");
}
}
catch(Exception e2){}
}
}