XML系列:使用Java和JAXP对XML文档进行访问和操作
一,Java访问XML文档
导入java.xml.parsers包和org.w3c.dom包
org.w3c.dom包包含了DOM解析器接口类型的定义。
1,获取java.xml.parsers.DocumentBuilder类:为加载和分析XML文档提供接口,也就是XML分析程序接口。
可以把JAXP配置成不同XML分析程序,java.xml.parsers.DocumentBuilderFactory用于获取一个分析程序的示例。
使用DocumentBuilderFactory为当前的XML分析程序实例化一个DocumentBuilder对象。
DocumentBuilderFactory factory = DocumentBuilderFactory.newInsrance();
DocumentBuilder builder = factory.newDocumentBuilder();
2,加载和分析XML文档,如果加载成功则返回一个org.w3c.dom.Document对象,
该对象是XML文档树形结构在内存中的表现。如果失败则抛出SAXException。
Document对象由实现Node接口及其多个子接口的类的对象构成。
File file = new File("type.xml");
Document doc = builder.parse(file);
3,Document对象的getDocumentElement方法获取根节点对象,getTagName方法返回节点的标签名。
Element root = doc.getDocumentElement();
String rootName = doc.getTagName();
4,Element对象的getChildNodes方法获取元素的子元素,返回结果为NodeList集合。
getElementByTagName或getElementById,通过指定元素名或ID来获取该元素的子元素集合,返回结果为NodeList集合。
NodeList children = root.getChildNodes();
或
NodeList children = root.getElementByTagName("type");
使用item方法遍历集合,getLength提供集合元素的总数。
遍历NodeList集合:
Element e = null;
for(int i = 0; i < children.getLength(); i ++)
{
Node child = children.item(i);
e = (Element) child;
System.out.println(e.getTagName() + " : " + e.getFirstChild().getNodeValue());
}
遍历NodeList集合:只得到子元素,使用instanceof运算符进行类型检查。
for(int i = 0; i < children.getLength(); i ++)
{
Node child = children.item(i);
if (child instanceof Element)
{
Element e = (Element) child;
System.out.println(e.getTagName() + " : " + e.getNodeValue());
}
}
遍历NodeList集合:
获取当前节点的第一个getFirstChild方法和下一个子节点getNextChild方法,没有节点的时候返回null。
for (Node childNode = children.getFirstChild(); childNode != null;
childeNode = childNode.getNextChild) {
if(childNode instanceof Element){
.....;
}
}
5,Element元素的getAttributes方法获取元素的的属性集合,返回结果为NamedNodeMap对象。
可以像使用遍历NodeList集合一样遍历NamedNodeMap
NamedNodeMap attributes = root.getAttributes();
遍历NamedNodeMap集合:
for (int i = 0; i < attributes.getLength(); i++)
{
Node attribute = attributes.item(i);
String attributeName = attribute.getNodeName();
String attributeValue = attribute.getNodeValue();
}
例子:
XML文档(type.xml):
<?xml version="1.0" encoding="UTF-8"?>
<type xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="type1.xsd">
<student state="true">
<info>
<name>张三</name>
<sex>男</sex>
</info>
<grede>
<chinese>1</chinese>
<math>119</math>
</grede>
</student>
<teacher state="true">
<name>李四</name>
<sex>女</sex>
<subject>数学</subject>
</teacher>
</type>
java文件:
package com.qu.read;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.*;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class ReadXML {
public ReadXML()
{
}
public Document readXMLFile(String fileName)
{
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
Document doc = null;
try {
DocumentBuilder builder = factory.newDocumentBuilder();
File file = new File(fileName);
doc = builder.parse(file);
} catch (SAXException e) {
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}catch (ParserConfigurationException e) {
e.printStackTrace();
}
return doc;
}
public void readXMLElements(NodeList nodeList)
{
NamedNodeMap attributes = null;
for (int i = 0; i < nodeList.getLength(); i++)
{
Node childNode = nodeList.item(i);
String str = null;
if (childNode instanceof Element){
Element child =(Element) childNode;
if( child.getChildNodes().getLength() > 1){
str = "Element :" + child.getTagName() + ";";
}else{
str = "Element :" + child.getTagName();
str += " : " + child.getFirstChild().getNodeValue() + " ";
}
attributes = child.getAttributes();
for (int j = 0; j < attributes.getLength(); j ++)
{
Node attr = attributes.item(j);
if (attr instanceof Attr){
String attrName = attr.getNodeName();
String attrValue = attr.getNodeValue();
str += " Attribute " + attrName + " : " + attrValue + ";";
}
}
System.out.println(str);
}
}
}
public static void main(String[] args) {
ReadXML readXML = new ReadXML();
Document doc = readXML.readXMLFile("E:/workspace/ReadXML/src/com/qu/xml/type.xml");
NodeList nodeList = doc.getElementsByTagName("*");
readXML.readXMLElements(nodeList);
}
}