DOM是Document Object Model即文档对象模型的缩写,是一种与浏览器、平台、语言的接口,已经被W3C标准化。Java的DOM解析器负责解析xml文件。它容易理解,根据XML文件生成一个树结构,所以如果该xml文件很大,则会消耗很大的内存。
解析xml文件,首先需要创建DocumentBuilderFactory实例:
DocumentBuilderFactory factory = DocumentBuilderFacory.newInstance();
接下来创建DocumentBuilder实例:
DocumentBuilder builder = factory.newDocumentBuilder();
下面就可以导入xml文件了。DocumentBuilder支持File、URL和InputStream三种格式的参数:
Document doc = builder.parse(file);
Document doc = builder.parse(url);
Document doc = builder.parse(inputStream);
Document就是xml文档的树型结构在内存中的表现。它由实现Node接口及其多个子接口的类对象构成。
Element root = doc.getDocumentElement();
root就是xml的根元素(元素是xml文件中以“<”和“>”包裹起来的内容),通过root可以取得其元素名、属性名及值、文本内容等。
下面是一个解析xml文件的完整例子:
/** */
/**
* 解析XML文件
*
* @param file
* XML文件
*/
public void parseXML(File file) ... {
try...{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(file);
Element root = doc.getDocumentElement(); //取得根元素
parseElement(root);
} catch(IOException e)...{
JOptionPane.showMessageDialog(this, e);
} catch(ParserConfigurationException e)...{
JOptionPane.showMessageDialog(this, e);
} catch(SAXException e)...{
JOptionPane.showMessageDialog(this, e);
}
}
/** */ /**
* 解析元素
*
* @param e
* 元素
*/
public void parseElement(Element e) ... {
if(e != null) ...{
System.out.println("元素名:" + e.getTagName());
NamedNodeMap attributes = e.getAttributes(); //取得当前元素的属性列表
for(int i=0; i<attributes.getLength(); i++)...{
Node node = attributes.item(i);
if(node != null) ...{
System.out.println("属性:" + node.getNodeName() + "=" + node.getNodeValue());
}
}
NodeList list = e.getChildNodes(); //取得当前元素的所有子节点
if(list != null) ...{
for(int i=0; i<list.getLength(); i++) ...{
Node node = list.item(i);
if(node != null)...{
if(node instanceof Element) //子元素
parseElement((Element)node);
else if(node instanceof Text) //该元素的文本
System.out.println("文本:" + ((Text)node).getData());
};
}
}
}
}
* 解析XML文件
*
* @param file
* XML文件
*/
public void parseXML(File file) ... {
try...{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(file);
Element root = doc.getDocumentElement(); //取得根元素
parseElement(root);
} catch(IOException e)...{
JOptionPane.showMessageDialog(this, e);
} catch(ParserConfigurationException e)...{
JOptionPane.showMessageDialog(this, e);
} catch(SAXException e)...{
JOptionPane.showMessageDialog(this, e);
}
}
/** */ /**
* 解析元素
*
* @param e
* 元素
*/
public void parseElement(Element e) ... {
if(e != null) ...{
System.out.println("元素名:" + e.getTagName());
NamedNodeMap attributes = e.getAttributes(); //取得当前元素的属性列表
for(int i=0; i<attributes.getLength(); i++)...{
Node node = attributes.item(i);
if(node != null) ...{
System.out.println("属性:" + node.getNodeName() + "=" + node.getNodeValue());
}
}
NodeList list = e.getChildNodes(); //取得当前元素的所有子节点
if(list != null) ...{
for(int i=0; i<list.getLength(); i++) ...{
Node node = list.item(i);
if(node != null)...{
if(node instanceof Element) //子元素
parseElement((Element)node);
else if(node instanceof Text) //该元素的文本
System.out.println("文本:" + ((Text)node).getData());
};
}
}
}
}