<h2 class="l">《<a href="http://www.imooc.com/learn/171" target="_blank">Java眼中的XML---文件读取》
XML 数据以纯文本格式进行存储,因此提供了一种独立于软件和硬件的数据存储方法。这让创建不同应用程序可以共享的数据变得更加容易。XML 简化数据传输,通过 XML,可以在不兼容的系统之间轻松地交换数据。
<span style="color: #0000ff;">import<span style="color: #000000;"> javax.xml.parsers.DocumentBuilder;
<span style="color: #0000ff;">import<span style="color: #000000;"> javax.xml.parsers.DocumentBuilderFactory;
<span style="color: #0000ff;">import<span style="color: #000000;"> javax.xml.parsers.ParserConfigurationException;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.w3c.dom.Document;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.w3c.dom.Element;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.w3c.dom.NamedNodeMap;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.w3c.dom.Node;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.w3c.dom.NodeList;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.xml.sax.SAXException;
<span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> DomTest {
<span style="color: #0000ff;">public <span style="color: #0000ff;">static <span style="color: #0000ff;">void<span style="color: #000000;"> main(String[] args) {
DocumentBuilderFactory dbf =<span style="color: #000000;"> DocumentBuilderFactory.newInstance();
<span style="color: #0000ff;">try<span style="color: #000000;"> {
DocumentBuilder db =<span style="color: #000000;"> dbf.newDocumentBuilder();
Document doc = db.parse("books.xml"<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 通过Tag获取节点,获得所有book节点的集合</span>
NodeList bookList = doc.getElementsByTagName("book"<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 2个节点 代表2本书</span>
System.out.println("一共有" + bookList.getLength() + "本书。"<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 获取文档根节点</span>
Element root =<span style="color: #000000;"> doc.getDocumentElement();
NodeList bookList1 </span>=<span style="color: #000000;"> root.getChildNodes();
System.out.println(</span>"有" + bookList1.getLength() + "个节点"<span style="color: #000000;">);
</span><span style="color: #008000;">/*</span><span style="color: #008000;"> 5个节点 因为text也是节点
<bookstore>
1.(text)
2.(<book>...</book>)
3.(text)
4.(<book>...</book>)
5.(text)
</bookstore>
</span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">int</span> index = 0<span style="color: #000000;">;
</span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i < bookList1.getLength(); i++<span style="color: #000000;">) {
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 通过item(i)方法获取第i个节点</span>
Node node =<span style="color: #000000;"> bookList1.item(i);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 获取所有名字为book的节点</span>
<span style="color: #0000ff;">if</span> ("book"<span style="color: #000000;">.equals(node.getNodeName())) {
System.out.println(</span>"———第" + (++index) + "本书的信息————"<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 通过getAttributes()获取节点的所有属性</span>
NamedNodeMap attrs =<span style="color: #000000;"> node.getAttributes();
System.out.print(</span>"所有属性:"<span style="color: #000000;">);
</span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> j = 0; j < attrs.getLength(); j++<span style="color: #000000;">) {
Node attr </span>=<span style="color: #000000;"> attrs.item(j);
</span><span style="color: #0000ff;">if</span> (j != 0) System.out.print(","<span style="color: #000000;">);
System.out.print(attr.getNodeName() </span>+ "(" + attr.getNodeValue() + ")"<span style="color: #000000;">);
}
System.out.println();
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 也可以直接通过属性名获取属性值
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 将node强制转换为Element</span>
Element book =<span style="color: #000000;"> (Element)node;
System.out.println(</span>"属性之种类: " + book.getAttribute("category"<span style="color: #000000;">));
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 继续获取book节点的子节点</span>
NodeList childNodes =<span style="color: #000000;"> node.getChildNodes();
</span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> k = 0; k < childNodes.getLength(); k++<span style="color: #000000;">) {
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 只获取element类型的node,不要text</span>
Node childNode =<span style="color: #000000;"> childNodes.item(k);
</span><span style="color: #0000ff;">if</span> (childNode.getNodeType() ==<span style="color: #000000;"> Node.ELEMENT_NODE) {
System.out.print(childNode.getNodeName() </span>+ ":"<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 只有<node>text</node>的时候才能获取node的值 否则是null</span>
<span style="color: #000000;"> System.out.print(childNode.getFirstChild().getNodeValue());
<span style="color: #008000;">//<span style="color: #008000;"> 获取节点之间所有的文本内容
System.out.println("(" + childNode.getTextContent() + ")"<span style="color: #000000;">);
}
}
}
}
} </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (ParserConfigurationException e) {
e.printStackTrace();
} </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (SAXException e) {
e.printStackTrace();
} </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (IOException e) {
e.printStackTrace();
}
}
}
2.SAX方式解析XML文件
SAX,全称Simple API for XML,既是一种接口,也是一种软件包。它是一种XML解析的替代方法。SAX不同于DOM解析,它逐行扫描文档,一边扫描一边解析。由于应用程序只是在读取数据时检查数据,因此不需要将数据存储在内存中,这对于大型文档的解析是个巨大优势。
<span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> Book {
<span style="color: #0000ff;">private<span style="color: #000000;"> String id;
<span style="color: #0000ff;">private<span style="color: #000000;"> String category;
<span style="color: #0000ff;">private<span style="color: #000000;"> String title;
<span style="color: #0000ff;">private<span style="color: #000000;"> String author;
<span style="color: #0000ff;">private<span style="color: #000000;"> String year;
<span style="color: #0000ff;">private<span style="color: #000000;"> String price;
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String toString() {
</span><span style="color: #0000ff;">return</span> "id=" + id + "ncategory=" + category + "ntitle=" +<span style="color: #000000;"> title
</span>+ "nauthor=" + author + "nyear=" + year + "nprice=" +<span style="color: #000000;"> price;
}
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String getId() {
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> id;
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setId(String id) {
</span><span style="color: #0000ff;">this</span>.id =<span style="color: #000000;"> id;
}
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String getCategory() {
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> category;
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setCategory(String category) {
</span><span style="color: #0000ff;">this</span>.category =<span style="color: #000000;"> category;
}
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String getTitle() {
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> title;
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setTitle(String title) {
</span><span style="color: #0000ff;">this</span>.title =<span style="color: #000000;"> title;
}
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String getAuthor() {
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> author;
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setAuthor(String author) {
</span><span style="color: #0000ff;">this</span>.author =<span style="color: #000000;"> author;
}
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String getYear() {
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> year;
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setYear(String year) {
</span><span style="color: #0000ff;">this</span>.year =<span style="color: #000000;"> year;
}
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String getPrice() {
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> price;
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setPrice(String price) {
</span><span style="color: #0000ff;">this</span>.price =<span style="color: #000000;"> price;
}
}
<span style="color: #0000ff;">import<span style="color: #000000;"> java.util.ArrayList;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.xml.sax.Attributes;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.xml.sax.SAXException;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.xml.sax.helpers.DefaultHandler;
<span style="color: #0000ff;">public <span style="color: #0000ff;">class SAXParserHandler <span style="color: #0000ff;">extends<span style="color: #000000;"> DefaultHandler {
Book book </span>= <span style="color: #0000ff;">null</span><span style="color: #000000;">;
ArrayList</span><Book> bookList = <span style="color: #0000ff;">new</span> ArrayList<><span style="color: #000000;">();
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 全局变量标识第几本书</span>
<span style="color: #0000ff;">int</span> bookIndex = 0<span style="color: #000000;">;
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 全局变量 记录节点的值</span>
String nodeValue = <span style="color: #0000ff;">null</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">public</span> ArrayList<Book><span style="color: #000000;"> getBookList() {
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> bookList;
}
</span><span style="color: #008000;">/**</span><span style="color: #008000;">
* 用来标识文档开始
</span><span style="color: #008000;">*/</span><span style="color: #000000;">
@Override
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> startDocument() <span style="color: #0000ff;">throws</span><span style="color: #000000;"> SAXException {
</span><span style="color: #0000ff;">super</span><span style="color: #000000;">.startDocument();
System.out.println(</span>"SAX解析开始"<span style="color: #000000;">);
}
</span><span style="color: #008000;">/**</span><span style="color: #008000;">
* 用来标识文档结束
</span><span style="color: #008000;">*/</span><span style="color: #000000;">
@Override
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> endDocument() <span style="color: #0000ff;">throws</span><span style="color: #000000;"> SAXException {
</span><span style="color: #0000ff;">super</span><span style="color: #000000;">.endDocument();
System.out.println(</span>"SAX解析结束"<span style="color: #000000;">);
}
</span><span style="color: #008000;">/**</span><span style="color: #008000;">
* 解析XML元素开始
</span><span style="color: #008000;">*/</span><span style="color: #000000;">
@Override
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> startElement(String uri,String localName,String qName,Attributes attributes)
</span><span style="color: #0000ff;">throws</span><span style="color: #000000;"> SAXException {
</span><span style="color: #0000ff;">super</span><span style="color: #000000;">.startElement(uri,localName,qName,attributes);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 如果标签名为book</span>
<span style="color: #0000ff;">if</span> ("book"<span style="color: #000000;">.equals(qName)) {
System.out.println(</span>"第" + (++bookIndex) + "本书:"<span style="color: #000000;">);
book </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Book();
</span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i < attributes.getLength(); i++<span style="color: #000000;">) {
System.out.print(</span>"属性:" +<span style="color: #000000;"> attributes.getQName(i));
System.out.println(</span>"=" +<span style="color: #000000;"> attributes.getValue(i));
</span><span style="color: #0000ff;">if</span> ("category"<span style="color: #000000;">.equals(attributes.getQName(i))) {
book.setCategory(attributes.getValue(i));
} </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> ("id"<span style="color: #000000;">.equals(attributes.getQName(i))) {
book.setId(attributes.getValue(i));
}
}
} </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (!"bookstore"<span style="color: #000000;">.equals(qName)) {
System.out.print(</span>"节点名:" +<span style="color: #000000;"> qName);
}
}
</span><span style="color: #008000;">/**</span><span style="color: #008000;">
* 解析XML元素结束
</span><span style="color: #008000;">*/</span><span style="color: #000000;">
@Override
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> endElement(String uri,String qName) <span style="color: #0000ff;">throws</span><span style="color: #000000;"> SAXException {
</span><span style="color: #0000ff;">super</span><span style="color: #000000;">.endElement(uri,qName);
</span><span style="color: #0000ff;">if</span> ("book"<span style="color: #000000;">.equals(qName)) {
System.out.println(</span>"本书遍历结束"<span style="color: #000000;">);
bookList.add(book);
book </span>= <span style="color: #0000ff;">null</span><span style="color: #000000;">;
} </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> ("title"<span style="color: #000000;">.equals(qName)) {
book.setTitle(nodeValue);
} </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> ("author"<span style="color: #000000;">.equals(qName)) {
book.setAuthor(nodeValue);
} </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> ("year"<span style="color: #000000;">.equals(qName)) {
book.setYear(nodeValue);
} </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> ("price"<span style="color: #000000;">.equals(qName)) {
book.setPrice(nodeValue);
}
}
@Override
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> characters(<span style="color: #0000ff;">char</span>[] ch,<span style="color: #0000ff;">int</span> start,<span style="color: #0000ff;">int</span> length) <span style="color: #0000ff;">throws</span><span style="color: #000000;"> SAXException {
</span><span style="color: #0000ff;">super</span><span style="color: #000000;">.characters(ch,start,length);
nodeValue </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> String(ch,length);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 去掉空白文本部分</span>
<span style="color: #0000ff;">if</span> (!""<span style="color: #000000;">.equals(nodeValue.trim()))
System.out.println(</span>"=" +<span style="color: #000000;"> nodeValue);
}
}
<span style="color: #0000ff;">import<span style="color: #000000;"> java.io.IOException;
<span style="color: #0000ff;">import<span style="color: #000000;"> java.util.ArrayList;
<span style="color: #0000ff;">import<span style="color: #000000;"> javax.xml.parsers.ParserConfigurationException;
<span style="color: #0000ff;">import<span style="color: #000000;"> javax.xml.parsers.SAXParser;
<span style="color: #0000ff;">import<span style="color: #000000;"> javax.xml.parsers.SAXParserFactory;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.xml.sax.SAXException;
<span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> SAXTest {
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) {
SAXParserFactory factory </span>=<span style="color: #000000;"> SAXParserFactory.newInstance();
</span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
SAXParser parser </span>=<span style="color: #000000;"> factory.newSAXParser();
SAXParserHandler handler </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> SAXParserHandler();
parser.parse(</span>"books.xml"<span style="color: #000000;">,handler);
ArrayList</span><Book> bookList =<span style="color: #000000;"> handler.getBookList();
</span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (Book book: bookList) {
System.out.println(</span>"-------"<span style="color: #000000;">);
System.out.println(book);
}
} </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (ParserConfigurationException e) {
e.printStackTrace();
} </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (SAXException e) {
e.printStackTrace();
} </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (IOException e) {
e.printStackTrace();
}
}
}
3.JDOM方式解析XML文件
这个比较简单了,不过需要导入外部jar文件,地址:http://www.jdom.org/dist/binary/
<span style="color: #0000ff;">import<span style="color: #000000;"> org.jdom2.Attribute;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.jdom2.Document;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.jdom2.Element;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.jdom2.JDOMException;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.jdom2.input.SAXBuilder;
<span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> JDOMTest {
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) {
SAXBuilder saxBuilder </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> SAXBuilder();
</span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
InputStream in </span>= <span style="color: #0000ff;">new</span> FileInputStream("xmls/books.xml"<span style="color: #000000;">);
Document document </span>=<span style="color: #000000;"> saxBuilder.build(in);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 获取xml文件根节点</span>
Element element =<span style="color: #000000;"> document.getRootElement();
List</span><Element> bookList =<span style="color: #000000;"> element.getChildren();
</span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (Element book: bookList) {
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 直接根据属性名获取属性值
</span><span style="color: #008000;">//</span><span style="color: #008000;"> book.getAttributeValue("id");</span>
List<Attribute> attrList =<span style="color: #000000;"> book.getAttributes();
</span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (Attribute attr: attrList) {
System.out.println(</span>"属性" + attr.getName() + "=" +<span style="color: #000000;"> attr.getValue());
}
List</span><Element> childList =<span style="color: #000000;"> book.getChildren();
</span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (Element child: childList) {
System.out.println(</span>"子节点" + child.getName() + "=" +<span style="color: #000000;"> child.getValue());
}
}
} </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (FileNotFoundException e) {
e.printStackTrace();
} </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (JDOMException e) {
e.printStackTrace();
} </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (IOException e) {
e.printStackTrace();
}
}
}
4.DOM4J方式解析XML文件
下载地址:http://www.dom4j.org/dom4j-1.6.1/
<span style="color: #0000ff;">import<span style="color: #000000;"> org.dom4j.Attribute;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.dom4j.Document;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.dom4j.DocumentException;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.dom4j.Element;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.dom4j.io.SAXReader;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.w3c.dom.Attr;
<span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> DOM4JTest {
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) {
SAXReader reader </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> SAXReader();
Document document;
</span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
document </span>= reader.read(<span style="color: #0000ff;">new</span> File("xmls/books.xml"<span style="color: #000000;">));
Element bookStore </span>=<span style="color: #000000;"> document.getRootElement();
Iterator it </span>=<span style="color: #000000;"> bookStore.elementIterator();
</span><span style="color: #0000ff;">while</span><span style="color: #000000;"> (it.hasNext()) {
System.out.println(</span>"开始遍历某本书>>>"<span style="color: #000000;">);
Element book </span>=<span style="color: #000000;"> (Element)it.next();
List</span><Attribute> bookAttrs =<span style="color: #000000;"> book.attributes();
</span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (Attribute attr: bookAttrs) {
System.out.println(</span>"属性" + attr.getName() + "=" +<span style="color: #000000;"> attr.getValue());
}
Iterator childIt </span>=<span style="color: #000000;"> book.elementIterator();
</span><span style="color: #0000ff;">while</span><span style="color: #000000;"> (childIt.hasNext()) {
Element bookChild </span>=<span style="color: #000000;"> (Element) childIt.next();
System.out.println(</span>"节点" + bookChild.getName() + "=" +<span style="color: #000000;"> bookChild.getStringValue());
}
System.out.println(</span>"结束遍历>>>"<span style="color: #000000;">);
}
} </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (DocumentException e) {
</span><span style="color: #008000;">//</span><span style="color: #008000;"> TODO Auto-generated catch block</span>
<span style="color: #000000;"> e.printStackTrace();
}
}
}
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!